简体   繁体   中英

Selecting(clicking not SQL Select) a specific result from a Result Set

I'll do my best to explain.

I have a doPost that returns all the employees from a specific department.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = null; 
Connection conn = null;
Statement stmt = null;
ResultSet res = null;

String department = request.getParameter("department");

try
{

    response.setContentType("text/html");

    out = response.getWriter();

    out.println("<BODY>");
    out.println("<TABLE BORDER=1>");
    out.println("<TR>");
    out.println("<TD>Id</TD>");
    out.println("<TD>Name</TD>");


    conn = ConnectionManager.getConnection();
    stmt = conn.createStatement();

    res = stmt.executeQuery("select * from employees where department ='" + request.getParameter("department") + "'");

    if(department.isEmpty())
    {
        String message = "Employee Name Invalid";
        request.setAttribute("message", message);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/test.jsp");
        dispatcher.forward(request, response);
    }
    if (department.matches(department))

    {

        while(res.next())
        {                   
            out.println("<TR>");
            out.println("<TD>" + res.getString(1) + "</TD>");   
            out.println("<TD><a href='employeeDetail.jsp'>"+ res.getString(3) + "  " + res.getString(2) +"</a></TD>");      

            HttpSession session = request.getSession();
            session.setAttribute("name", res.getString(3));
            session.setAttribute("lname", res.getString(2));
            session.setAttribute("id", res.getString(1));
            session.setAttribute("dob", res.getString(6));
            session.setAttribute("address", res.getString(8));
            session.setAttribute("city", res.getString(9));
            session.setAttribute("state", res.getString(10));
            session.setAttribute("zip", res.getString(11));
            session.setAttribute("phone", res.getString(13));

            session.setAttribute("hd", res.getString(7));
            session.setAttribute("ext", res.getString(14));
            session.setAttribute("dept", res.getString(18));
            session.setAttribute("notes", res.getString(16));

        }
    }


    out.println("</TABLE>");
    out.println("</BODY>");
}         
catch (Exception e)
{
    throw new ServletException(e.toString());
}

When the sales department is selected the result is:

Id Name

1 Nancy D..... 2 Andrew F..... 3 Janet L.....

each name is hyperlinked to an employeeDetail.jsp. For some reason it doesnt show in the list above.

When I click on any of the links (Nancy, Andrew, Janet) I only get the results for the last element (Janet).

My question is how can I get the specific details for the employee that I click?

I've tried looking this up but I dont think I'm asking the question correctly.

Also, any pointers on changes I can make on my servlet would be greatly appreciated.

Thanks.

Your loop here:

while(res.next()) {
  ...
}

Is continually updating the client's session state to the next result. You would need the client to provide some form of information (perhaps the employee's ID) to determine which bit of employee information should actually be saved to the client's session state.

In terms of suggestions for changing your servlet, I'd say you should move away from using session state entirely for this use. For example, something along the lines of:

/departmentServlet?department={DEPARTMENT_ID}
=> returns a list of employees (without saving anything to the session state)
=> each link should be of the form "/employeeDetails?id={CURRENT_EMPLOYEE_ID}"

/employeeDetails?id={EMPLOYEE_ID}
=> calls the SQL again but only for the provided employee ID
=> returns whatever information required by the client for the given employee

What is the condition of the select query in the employeeDetail.jsp? The link what this code creates does not contains any information from the selected employee (for example db id). I think you use the saved session information, but this code overwrites the session in every for cycle - that's why the session will contain the information of the last employee.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM