Jakarta ee 会话传递失败-servlet

Jakarta ee 会话传递失败-servlet,jakarta-ee,servlets,session-variables,Jakarta Ee,Servlets,Session Variables,下面是我希望通过会话的两个servlet 问题是会话传递是在导航到SuccessPage servlet时完成的,而不是在导航到失败servlet时完成的 登录servletdoGet()方法: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ PrintWriter out=response.ge

下面是我希望通过会话的两个servlet

问题是会话传递是在导航到SuccessPage servlet时完成的,而不是在导航到失败servlet时完成的

登录servlet
doGet()
方法:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    
ServletException, IOException{
    PrintWriter out=response.getWriter();
    response.setContentType("text/html");
    String userName=request.getParameter("userName");
    String userPass=request.getParameter("userPassword");
    String userRePass=request.getParameter("userRePassword");
    try{
        String query="Select VendorName from vendorinfo where VendorPass=?";
        connection1=Connection_Class.getInstance().getConnection();
        ptmt=connection1.prepareStatement(query);
        ptmt.setString(1,userPass);
        rs=ptmt.executeQuery();
        if(rs.next()&& userName.equalsIgnoreCase(rs.getString("VendorName"))){
        HttpSession session=request.getSession(true);
        session.setAttribute("loggedVendor",rs.getString(1));
        //this is working fine...im able to get the userName in the next servlet
        ServletContext context = getServletContext();
        RequestDispatcher dispatcher=context.getRequestDispatcher("/SuccessPage");
        dispatcher.forward(request,response);
        }
        else{  //this is not working .....whats the problem here ?
            request.setAttribute("wrongUser",userName);
            ServletContext context=getServletContext();
            RequestDispatcher dispatcher=context.getRequestDispatcher("/Failure");
            dispatcher.forward(request,response);
        }
    }
    catch(SQLException e){
        e.printStackTrace();
    }
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  
ServletException, IOException {
/*all i want to do here is that I want to get the userName from the previous servlet but its 
not displaying that and its displaying null */
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    out.println("<body bgcolor=#F3EEF0><h1>");
    out.println("<center>");
    HttpSession session=request.getSession(false);
    out.println("This is a failure page");
    out.println(session.getAttribute("wrongUser"));
    out.println("</center></h1></body>");
}
失败servlet
doGet()
方法:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    
ServletException, IOException{
    PrintWriter out=response.getWriter();
    response.setContentType("text/html");
    String userName=request.getParameter("userName");
    String userPass=request.getParameter("userPassword");
    String userRePass=request.getParameter("userRePassword");
    try{
        String query="Select VendorName from vendorinfo where VendorPass=?";
        connection1=Connection_Class.getInstance().getConnection();
        ptmt=connection1.prepareStatement(query);
        ptmt.setString(1,userPass);
        rs=ptmt.executeQuery();
        if(rs.next()&& userName.equalsIgnoreCase(rs.getString("VendorName"))){
        HttpSession session=request.getSession(true);
        session.setAttribute("loggedVendor",rs.getString(1));
        //this is working fine...im able to get the userName in the next servlet
        ServletContext context = getServletContext();
        RequestDispatcher dispatcher=context.getRequestDispatcher("/SuccessPage");
        dispatcher.forward(request,response);
        }
        else{  //this is not working .....whats the problem here ?
            request.setAttribute("wrongUser",userName);
            ServletContext context=getServletContext();
            RequestDispatcher dispatcher=context.getRequestDispatcher("/Failure");
            dispatcher.forward(request,response);
        }
    }
    catch(SQLException e){
        e.printStackTrace();
    }
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  
ServletException, IOException {
/*all i want to do here is that I want to get the userName from the previous servlet but its 
not displaying that and its displaying null */
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    out.println("<body bgcolor=#F3EEF0><h1>");
    out.println("<center>");
    HttpSession session=request.getSession(false);
    out.println("This is a failure page");
    out.println(session.getAttribute("wrongUser"));
    out.println("</center></h1></body>");
}
protectedvoid doGet(HttpServletRequest请求,HttpServletResponse响应)抛出
ServletException,IOException{
/*这里我想做的就是从前面的servlet中获取用户名,但是它是
不显示它,并且它的显示为空*/
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“”);
HttpSession session=request.getSession(false);
out.println(“这是一个失败页面”);
out.println(session.getAttribute(“错误用户”);
out.println(“”);
}

代码中是否有错误?

您正在请求中放置键为“错误用户”的数据,而不是在第一个servlet中放置会话:

 request.setAttribute("wrongUser",userName);
并从失败Servlet中的会话中检索它:

session.getAttribute("wrongUser");
在两个位置使用“会话”或在两个位置使用“请求”。因此,如果使用request.setAttribute(),请使用request.getAttribute()。如果使用session.setAttribute(),请使用session.getAttribute()

建议:使用request,这样您就不会开始不必要地加载带有大量任意命令的会话。您不需要超出此请求/响应周期范围的此值