如果我在创建会话对象的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);
}
}
我有几个问题:
HttpSession session = request.getSession();
创建会话对象时,这是在第一次为请求创建会话时,或者在请求第一次进入时(在调用{{之前)容器是否已创建请求1}})getSession()
时,会话对象在response.sendRedirect("LoginSuccess.jsp");
中的访问权限如何?我可以在LoginSuccess.jsp
中session.getAttribute("user")
,但我不确定会话如何传递给LoginSuccess.jsp
?LoginSuccess.jsp
也是如此。在Cookie userName = new Cookie("user", user);
我可以LoginSuccess.jsp
。 Cookie对象是如何传递给cookie.getName().equals("user"))
的?那是因为LoginSuccess.jsp
吗?