Java 会话getAttribute为空

Java 会话getAttribute为空,java,session,Java,Session,我正在尝试使用会话进行登录/注销验证 下面是一个场景 我有一个login.html,它向login.java提交请求以验证用户名和密码 验证后,它将转到main.jsp,其中包含注销按钮和表单。 我想确保在使用back按钮注销后,用户不应该看到main.jsp或对表单执行任何操作 这是我在Login.java中看到的 public class Login extends HttpServlet { private ServletContext context=null; priv

我正在尝试使用会话进行登录/注销验证

下面是一个场景 我有一个login.html,它向login.java提交请求以验证用户名和密码 验证后,它将转到main.jsp,其中包含注销按钮和表单。 我想确保在使用back按钮注销后,用户不应该看到main.jsp或对表单执行任何操作

这是我在Login.java中看到的

public class Login extends HttpServlet {
    private ServletContext context=null;
    private RequestDispatcher dispatcher = null;
    private String toDo = "";  

    public void doPost(HttpServletRequest request,
              HttpServletResponse response)
                        throws IOException, ServletException  {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(PasswordUtilities.isValidLogin(username,password)) {
            toDo = "/jsp/main.jsp";

            HttpSession session = request.getSession();          
        session.setAttribute("username", "abc"); 
        System.out.println("login"+(String)request.getAttribute("username"));
            }
        else
            toDo = "/error.html";
        context = getServletContext();       
        dispatcher = context.getRequestDispatcher(toDo);
        dispatcher.forward(request, response);                  
    }    
}
但我得到的属性为空


请提供帮助,如有任何帮助,我们将不胜感激。

您正在
会话中设置属性
,并从
请求

System.out.println("login"+(String)request.getAttribute("username"));
    session.setAttribute("username", "abc"); 
    System.out.println("login"+(String)request.getAttribute("username"));

您正在将其设置为
会话
并从
请求

System.out.println("login"+(String)request.getAttribute("username"));
    session.setAttribute("username", "abc"); 
    System.out.println("login"+(String)request.getAttribute("username"));

此代码不会在任何地方调用
session.getAttribute()