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.
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.
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.
Joined: Jan 31, 2013
is this what you had in mind? excuse the html in the servlet...just for quick viewing purposes
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).
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.
Joined: Jan 31, 2013
YES!!! Thanks BB...got the results I was looking for...