tags:

views:

29

answers:

1

I have DAO bean rows retrieved in a List. In my JSP I am accessing the List to iterate thru to populate my page. My JSP can't access the List because it says it must be a String when I execute a request.getParameter. How I convert this to String eventually populate my page?

public List getAccessRequest()
{
    List accessRequesttList = new ArrayList());  // parse List to string
    //AccessRequest accessrequest = null;
    AccessRequest accessRequest = new AccessRequest());

    try
    {
        System.out.println("Try statement begins AccessRequestDAO");
        PreparedStatement accrqststmt = super.getConnection().prepareStatement(AccRqstSqlStmt);

        ResultSet resultSet = accrqststmt.executeQuery();

        while (resultSet.next())
        {
            // Creating an instant of job follows
            accessRequest = new Accessrequest();

            accessRequest.setJOB_NAME(resultSet.getString("job_name"));
            accessRequest.setRequest_ts(resultSet.getTime("request_ts"));
            accessRequestList.add(accessRequest);
            Iterator iterator = accessRequestList.iterator();

            while (iterator.hasNext())
            {
                accessRequest = (Accessrequest) iterator.next();
            }
        }
        return (accessRequestList);

My JSP look like below:

        <%
            List jobList = request.getParameter("acccessrequests"); // parse List to String

            Iterator iterator = jobList.iterator();
            while (iterator.hasNext())
            {
                accessRequest = (AccessRequest) iterator.next());
        %>
                <tr>
                <td><input type="checkbox" name="<%accessRequest.getApproval_ind(); %>"></td>
                <td><input type="text" id="jobname' name="accessRequests" value="job_name"></td>
A: 

HttpServletRequest#getParameter() returns a String, not a List. So the compiler is right.

I am not sure how you have ever set the List as a request parameter, there's no such method like HttpServletRequest#setParameter(). So you're probably misinterpreting something. The normal approach is to set the list as request attribute by HttpServletRequest#setAttribute() and access it in JSP by EL (expression language) like as ${attributeName}. You also normally iterate over the list using JSTL <c:forEach> tag.

Assuming that you've set the list in the request scope using a Servlet like follows...

request.setAttribute("list", list);

...here's a kickoff example how to iterate over the list:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${list}" var="item">
        <tr>
            <td>${item.property1}</td>
            <td>${item.property2}</td>
            <td>${item.property3}</td>
        </tr>
    </c:forEach>
</table>
BalusC
This is my code that retrieves rows from a databaase in a Servlet, my business logic:
Scott
Scott
That's your choice, but I strongly recommend to read [this answer](http://stackoverflow.com/q/3177733) with regard to those old fashioned scriptlets. Further, did you fix your problem? You've included some code, but you didn't have stored the list as request **attribute** and you're still attempting to access the list from your DAO as a request **parameter**. Do you for instance understand my answer? If not, then please elaborate which parts not. I'd suggest to get yourself through [a bit decent](http://stackoverflow.com/tags/servlets/info) JSP/Servlet tutorial/book in the meanwhile.
BalusC
Scott
The key point is: store it as request attribute and get it as request attribute. Right now it's not clear how you're storing the list but yet you're attempting to access it as request parameter. Since request parameters are supposed to be set by the client (webbrowser), this makes no utter sense.
BalusC
I think it is stored as a request attribute:
Scott
List accessRequests = accessRequestDAO.getAccessRequest(); request.setAttribute("accessRequests", accessRequests);
Scott
This is done in my servlet.
Scott
Insstead of request.getParameter use request.getAttribute?
Scott
I think method names are self-explaining enough? If in vain, consult your senior :)
BalusC
Similar problem. List jobList = (List) request.getAttribute("accessrequest"); Error says list should be parametized -Type mismatch: cannot convert from Object to List.
Scott
The first one is not an error, but a warning. [Learn Generics](http://download.oracle.com/javase/tutorial/extra/generics/index.html). The second one shouldn't occur on the given example. It can only happen if you remove the cast.
BalusC
Thanks Balus! I am able to see my data. Am I to close this question? You have been very helpful.
Scott