It's not a secret anymore!*
The moose likes Servlets and the fly likes Passing multiple rows from Servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing multiple rows from Servlet to JSP" Watch "Passing multiple rows from Servlet to JSP" New topic
Author

Passing multiple rows from Servlet to JSP

Robert Legnosky
Greenhorn

Joined: Jan 31, 2013
Posts: 8
I'm working on a simple web application as a personal project to keep learning new things in Java.

So, I have a web page that allows the user to view employees in the department that he/she selects (ddl...you can select sales dept for example.)

A list of employees are returned. No problem so far. I would like the user then to be able to click on the any employee's name to get the employee's information. I'm using result set to return the list of employees. I am creating a session object to pass the pertinent employee info. The problem I'm running into is that if you click any of the ee's, you get the last ee's on the list info every the time. I suspect its because i'm using session.

code below...

EmployeeDDLServlet.java



DDLResults.jsp



EmployeeResults.jsp

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Welcome to the Ranch!

Before you do anything else, a major refactor is needed.

Firstly, it has nothing to do with the choice of the session (but more on that below). The problem is you are overwriting your values on each pass of the loop.

Create a bean to contain the information for a specific entry. Adding individual scoped variables for each property of an item is egregiously messy and problematic. Create one bean for each item, and put the beans in a List. The list is what you pass to the JSP.

Right now you are sort of trying to create a list, but then you ignore it when setting values.

Then, answer why you think you need to use session scope rather than request scope.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Also, please follow Java conventions. Variables begin with lowercase letters: rows not Rows.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

In fact, here's a refactor that might help conceptually.

Divorce the database activity completely from the servlet. Create a class that has a method that will return a List<Employee>. This new class will import NO servlet classes (such as session).

See how that helps to create a proper separation of concerns.
Robert Legnosky
Greenhorn

Joined: Jan 31, 2013
Posts: 8
Thanks BB,

I just want to make sure that I understand you....your are suggesting that I create a new bean object and save the results of the loop to it?



i'm pretty new to this so excuse my greenhorn ignorance.

And then in my jsp do I need do call the bean using <jsp:useBean>?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Let's put aside the JSP for now and concentrate on creating something that's suitable to pass to the JSP.

Each row in the table represent an employee, right? So you need a bean that represents one employee. To represent multiple employees you need multiple instances of the bean. You'd collect these into a List.

In your new code you have two lists. Why? You only need to end up with one list of employees.

Here's a line of code to kick start:


See where that takes you.
Robert Legnosky
Greenhorn

Joined: Jan 31, 2013
Posts: 8
is this what you had in mind? excuse the html in the servlet...just for quick viewing purposes

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Not in the least. Why did you ditch the ideal of a JSP and put HTML generation in the servlet? That's a really bad practice.

The idea is that you collect a List of employees and send it to the JSP.
Robert Legnosky
Greenhorn

Joined: Jan 31, 2013
Posts: 8
I didn't ditch the idea of the jsp. I added the HTML in the servlet just to test it quickly to see results.

Thanks for your help. I'll figure it out.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

It's actually very very simple:
  • Create the List of employees.
  • Set is as a scoped variable in request scope.
  • Forward to the JSP.
  • Use JSTL and EL in the JSP (<c:forEach> for iteration)


  • The important part is creating the List of Employee instances properly.
    Robert Legnosky
    Greenhorn

    Joined: Jan 31, 2013
    Posts: 8
    am i getting any closer BB?


    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61103
        
      66

    Yes, that's getting the idea. Of course the list creation should be outside any loop that creates the beans from the rows of the results.
    Robert Legnosky
    Greenhorn

    Joined: Jan 31, 2013
    Posts: 8
    now I'm just stuck




    To answer your earlier questions about having two loops, the inner loop



    gave me this on result on the page...

    [1, Davolio, Nancy, Sales Representative]

    The first four columns of my database (ID, LastName, FirstName, Title)

    The outer loop gave me this result...the rows.

    1, Davolio, Nancy, Sales Representative]
    [2, Fuller, Andrew, Vice President, Sales]
    [3, Leverling, Janet, Sales Representative]
    [4, Peacock, Margaret, Sales Representative]
    [5, Buchanan, Steven, Sales Manager]
    [6, Suyama, Michael, Sales Representative]
    [7, King, Robert, Sales Representative]
    [8, Callahan, Laura, Inside Sales Coordinator]
    [9, Dodsworth, Anne, Sales Representative]

    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61103
        
      66

    OK, so now you've got a scoped variable named results available to the JSP which contains a list of employee beans.

    If you need to iterate over them, you'd use the JSTL's <c:forEach> tag:

    Within the body, each iteration is available as employee to do as you will with. For example:
    Robert Legnosky
    Greenhorn

    Joined: Jan 31, 2013
    Posts: 8
    Thanks BB. Iterating over the list of employees was not the problem I had. Maybe I just wasn't clear.

    My issue was that once I have the list, how can I click on one employee and get their pertinent info?

    Is the code below possible...


    My initial code gave me a list of the employees....

    [1, Davolio, Nancy, Sales Representative] --- these are all hyperliked
    [2, Fuller, Andrew, Vice President, Sales]
    [3, Leverling, Janet, Sales Representative]
    [4, Peacock, Margaret, Sales Representative]
    [5, Buchanan, Steven, Sales Manager]
    [6, Suyama, Michael, Sales Representative]
    [7, King, Robert, Sales Representative]
    [8, Callahan, Laura, Inside Sales Coordinator]
    [9, Dodsworth, Anne, Sales Representative]

    However when I clicked on anyone I was only getting the profile info for #9. I attributed it to me creating the session object and only the last complete row of information was being sent to my employeesDetail.jsp (see my first post).

    Am I making sense?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61103
        
      66

    Two things about your link:
  • If it goes directly to a JSP, how can the controller grab the employee information to send it to the JSP for view? Never submit to a JSP. Submit to the servlet controller which can fetch the information for the employee, and send it to the JSP as a scoped variable.
  • You need to identify which employee to fetch the details for. This would customarily be with a request parameter (e.g. employeeId=4) on the URL.


  • If you have not read this article yet, I'd recommend doing so.
    Robert Legnosky
    Greenhorn

    Joined: Jan 31, 2013
    Posts: 8
    YES!!! Thanks BB...got the results I was looking for...

    EmployeeDDLServlet.java



    DDLResults.jsp



    ViewEmployeeServlet.java

    Mandar Rdongare
    Greenhorn

    Joined: Feb 12, 2013
    Posts: 7
    Hello i am trying the same but not getting output.

    This is my servlet code.


    loginjsp.jsp paage is as follows..



    plis help me for the same..

    Mandar
    Mandar Rdongare
    Greenhorn

    Joined: Feb 12, 2013
    Posts: 7
    Hello , I got the answer .I have made following changes in my code .

    Servlet code:

    else{
    String message="username or password is incorrect";
    request.setAttribute("message", message);
    request.getRequestDispatcher("/loginjsp.jsp").forward(request, response);
    }

    JSP Code:

    <td bgcolor="yellow">
    note: <c:if test="${not empty message}">
    ${message}
    </c:if>
    </td>


    The problem was regarding following or anything else???
    1. Scope of variable message.
    2. The string which is written request.setAttribute(); method.

    Actually i wanted to know why my code was not giving desired output in previously.
    Because i think i haven't made any considerable change to my code.....

    And why there is need to make the two parameters in request.setAttribute() method similar.
    What is in doble quotes? (is this string variable ?).

    And what is the meaning of message in jsp code <c:if test="${not empty message}">
    ${message} .

    I am new to this concept. And will be happy if anyone elaborate it.


    Mandar Dongare.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61103
        
      66

    The problem wit the first example is that you named the scoped variable Message, but tried to reference it as msg.

    The expression ${not empty message} tests to see if the message scoped variable is empty (null or the mnpty string) or not. Both empty and not are EL operators.
    Mandar Rdongare
    Greenhorn

    Joined: Feb 12, 2013
    Posts: 7
    Thanks a lot!!!
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Passing multiple rows from Servlet to JSP