Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从结果集中选择(单击而不是SQL选择)特定结果_Java_Jsp_Servlets - Fatal编程技术网

Java 从结果集中选择(单击而不是SQL选择)特定结果

Java 从结果集中选择(单击而不是SQL选择)特定结果,java,jsp,servlets,Java,Jsp,Servlets,我会尽力解释的 我有一个doPost,它返回特定部门的所有员工 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = null; Connection conn = null; Statement stmt = null; ResultSet res = null; Str

我会尽力解释的

我有一个doPost,它返回特定部门的所有员工

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());
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
PrintWriter out=null;
连接conn=null;
语句stmt=null;
ResultSet res=null;
字符串department=request.getParameter(“department”);
尝试
{
response.setContentType(“text/html”);
out=response.getWriter();
out.println(“”);
out.println(“”);
out.println(“”);
out.println(“Id”);
out.println(“名称”);
conn=ConnectionManager.getConnection();
stmt=conn.createStatement();
res=stmt.executeQuery(“从员工中选择*,其中department='”+request.getParameter(“department”)+“”);
if(department.isEmpty())
{
String message=“员工姓名无效”;
setAttribute(“消息”,消息);
RequestDispatcher=getServletContext().getRequestDispatcher(“/test.jsp”);
转发(请求、响应);
}
if(部门匹配(部门))
{
while(res.next())
{                   
out.println(“”);
out.println(“+res.getString(1)+”);
out.println(“”);
HttpSession session=request.getSession();
setAttribute(“name”,res.getString(3));
setAttribute(“lname”,res.getString(2));
session.setAttribute(“id”,res.getString(1));
session.setAttribute(“dob”,res.getString(6));
setAttribute(“地址”,res.getString(8));
session.setAttribute(“城市”,res.getString(9));
setAttribute(“state”,res.getString(10));
setAttribute(“zip”,res.getString(11));
session.setAttribute(“phone”,res.getString(13));
session.setAttribute(“hd”,res.getString(7));
setAttribute(“ext”,res.getString(14));
session.setAttribute(“dept”,res.getString(18));
session.setAttribute(“notes”,res.getString(16));
}
}
out.println(“”);
out.println(“”);
}         
捕获(例外e)
{
抛出新的ServletException(例如toString());
}
选择销售部门时,结果为:

身份证名称

1南希D。。。。。 2安德鲁F。。。。。 3珍妮特L

每个名称都超链接到employeeDetail.jsp。由于某些原因,它不会显示在上面的列表中

当我点击任何链接(Nancy、Andrew、Janet)时,我只得到最后一个元素(Janet)的结果

我的问题是如何获得我所单击的员工的具体详细信息

我试过查这个,但我认为我问的问题不对

另外,我非常感谢任何关于我可以在servlet上进行的更改的指针

谢谢。

您的循环在此:

while(res.next()) {
  ...
}
正在不断将客户端的会话状态更新为下一个结果。您需要客户端提供某种形式的信息(可能是员工ID),以确定哪些员工信息应实际保存到客户端的会话状态

关于更改servlet的建议,我想说的是,您应该完全放弃使用会话状态进行此用途

/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

employeeDetail.jsp中select查询的条件是什么? 此代码创建的链接不包含来自所选员工的任何信息(例如db id)。 我认为您使用保存的会话信息,但此代码会在每个for周期中覆盖会话-这就是为什么会话将包含最后一名员工的信息