This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing multiple rows from Servlet to JSP

 
Robert Legnosky
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please follow Java conventions. Variables begin with lowercase letters: rows not Rows.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this what you had in mind? excuse the html in the servlet...just for quick viewing purposes

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    am i getting any closer BB?


     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64833
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64833
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64833
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    YES!!! Thanks BB...got the results I was looking for...

    EmployeeDDLServlet.java



    DDLResults.jsp



    ViewEmployeeServlet.java

     
    Mandar Rdongare
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64833
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot!!!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic