Session对象如何在页面之间传递?

时间:2017-02-28 22:52:25

标签: java session tomcat

如果我在创建会话对象的doGet()中有以下代码:

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

        // get request parameters for userID and password
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");

        if(userID.equals(user) && password.equals(pwd)){
            HttpSession session = request.getSession();
            session.setAttribute("user", "Pankaj");
            //setting session to expiry in 30 mins
            session.setMaxInactiveInterval(30*60);
            Cookie userName = new Cookie("user", user);
            userName.setMaxAge(30*60);
            response.addCookie(userName);
            response.sendRedirect("LoginSuccess.jsp");
        }else{
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
            PrintWriter out= response.getWriter();
            out.println("<font color=red>Either user name or password is wrong.</font>");
            rd.include(request, response);
        }
    }

我有几个问题:

  1. 当我在HttpSession session = request.getSession();创建会话对象时,这是在第一次为请求创建会话时,或者在请求第一次进入时(在调用{{之前)容器是否已创建请求1}})
  2. 当我致电getSession()时,会话对象在response.sendRedirect("LoginSuccess.jsp");中的访问权限如何?我可以在LoginSuccess.jspsession.getAttribute("user"),但我不确定会话如何传递给LoginSuccess.jsp
  3. 创建的Cookie LoginSuccess.jsp也是如此。在Cookie userName = new Cookie("user", user);我可以LoginSuccess.jsp。 Cookie对象是如何传递给cookie.getName().equals("user"))的?那是因为LoginSuccess.jsp吗?

0 个答案:

没有答案