3

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Scott
  • 1
  • 1
  • 4

2 Answers2

2

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This is my code that retrieves rows from a databaase in a Servlet, my business logic: – Scott Sep 20 '10 at 13:44
  • I'm not using JSTL. I'm using iterator & while(next) to read infinite number of rows fetched. Then populate my webpage with the row data. – Scott Sep 20 '10 at 14:45
  • 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 Sep 20 '10 at 15:06
  • My senior is directing what scriptlets I use but, it may not be set in stone. No, I haven't fixed my problem. You answer uses 'forEach' & I'm trying to follow my senior's 'while (resultSet.next())' to loop thru the rows. If I shouldn't access my list with request parameter, how should I access my list? My SQL query retrieves 18 rows and I want the jobname list on my webpage. Will your suggestion of forEach loop thru my list and stop when no rows are left? – Scott Sep 20 '10 at 15:07
  • 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 Sep 20 '10 at 15:07
  • I think it is stored as a request attribute: – Scott Sep 20 '10 at 15:12
  • List accessRequests = accessRequestDAO.getAccessRequest(); request.setAttribute("accessRequests", accessRequests); – Scott Sep 20 '10 at 15:13
  • Insstead of request.getParameter use request.getAttribute? – Scott Sep 20 '10 at 15:15
  • I think method names are self-explaining enough? If in vain, consult your senior :) – BalusC Sep 20 '10 at 15:19
  • Similar problem. List jobList = (List) request.getAttribute("accessrequest"); Error says list should be parametized -Type mismatch: cannot convert from Object to List. – Scott Sep 20 '10 at 15:29
  • 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 Sep 20 '10 at 15:44
  • Thanks Balus! I am able to see my data. Am I to close this question? You have been very helpful. – Scott Sep 22 '10 at 19:59
0

Alhamdulillah, thanks God!

This help me a lot. I try to build my own java Web framework. Before reading this QA, I don't know how to access an object (say, row of table) from a JSP. I just redirect it, and leave the database code in JSP, generated by DreamWeaver. Now I know how to do it. For example, this is a BiroController, which display data from Biro Table :

 public void index() throws IOException, ServletException {
    List list=new ArrayList();
    list.add(BiroModel.create("1", "SDM"));
    list.add(BiroModel.create("2", "Keuangan"));
    request.setAttribute("list", list);        
    super.index();
}

firstly, I populate an array (subsequently, this will come from database table). and then set request attribute, then call superclass index method :

 public void index() throws IOException, ServletException {        
    RequestDispatcher rd = request.getRequestDispatcher(viewPage);
    if(rd!=null){
        rd.forward(request, response);
    }else{
       PrintWriter out = response.getWriter();
       out.print("can not dispatch to " + viewPage);
    }
    //OLD Code : response.sendRedirect(ServletUtil.getBaseUrl(request) + viewPage)
}

And, I did as you instructed in the JSP :

   <c:forEach items="${list}" var="item">
    <tr>
        <td>${item.idbiro}</td>
        <td>${item.biro}</td>
    </tr>
</c:forEach>

I use Netbeans, so I can easily pick JSTL library from the list of available library

It works charmingly.. :) tq

swdev
  • 4,997
  • 8
  • 64
  • 106