现在的位置: 首页 > 综合 > 正文

Jsp页面获取不到Servlet中session保存的值

2013年08月01日 ⁄ 综合 ⁄ 共 2577字 ⁄ 字号 评论关闭

登录页面login.jsp

Code:
  1. <body>   
  2.    <%   
  3.       String[] userInfo={"",""};   
  4.       Cookie[] cookie=request.getCookies();   
  5.       if(cookie!=null) {   
  6.          for(int i=0; i<cookie.length; i++) {   
  7.            if(cookie[i].getName().equals("loginInfo")) {   
  8.              userInfo = cookie[i].getValue().split("#");   
  9.            }   
  10.          }   
  11.       }   
  12.   %>   
  13.     
  14.     <form action="servlet/Login" method="post">   
  15.         账号:<input type="text" class="txt" name="username" value=<%=userInfo[0] %>/><br/>   
  16.         密码:<input type="password" class="txt" name="password" value=<%=userInfo[1] %>/><br/>   
  17.         允许写入Cookie:<input type="radio" name="agree" value="yes" checked/>是<input type="radio" name="agree" value="no"/>否<br/>   
  18.         <input type="submit" value="提交" />        <input type="reset" value="取消" />   
  19.     </form>   
  20.  </body>  

Servlet中doPost方法

如果doPost方法中均使用response.sendRedirect(request.getContextPath()+"/login.jsp");在result.jsp中始终得到的是null

就是说session里面的值始终得不到,在网上找了很久也没找到解决的方法。

Code:
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)   
  2.             throws ServletException, IOException {   
  3.   
  4.         response.setContentType("text/html");   
  5.         request.setCharacterEncoding("utf-8");   
  6.         response.setCharacterEncoding("utf-8");   
  7.         String userName = request.getParameter("username");   
  8.         String password = request.getParameter("password");   
  9.         String agree = request.getParameter("agree");   
  10.         User u = new User();   
  11.         u.setName(userName);   
  12.         u.setPassword(password);   
  13.         HttpSession session = null;   
  14.         if(u.canPass()) {   
  15.             if(agree.equals("yes")) {   
  16.                 Cookie myCookie = new Cookie("loginInfo", userName+"#"+password);   
  17.                 myCookie.setMaxAge(60*60*24*7);   
  18.                 response.addCookie(myCookie);   
  19.                 session = request.getSession();   
  20.                 session.setAttribute("user",(User)u);   
  21.             }   
  22.             ServletContext sc = getServletContext();   
  23.             RequestDispatcher rd = null;   
  24.             rd = sc.getRequestDispatcher("/result.jsp"); //定向的页面   
  25.             rd.forward(request, response);   
  26.         } else {   
  27.             response.sendRedirect(request.getContextPath()+"/login.jsp");   
  28.             return;   
  29.         }   
  30.     }  

结果页面

Code:
  1. <body>   
  2.     <%   
  3.         User u = (User)session.getAttribute("user");   
  4.         System.out.println(u);   
  5.         if(u==null) {   
  6.             u = new User();   
  7.             u.setName("");   
  8.             u.setPassword("");   
  9.         }   
  10.     %>   
  11.            当前登录用户的信息:账号(<%=u.getName() %>)密码(<%=u.getPassword() %>)   
  12.   </body>  

 

抱歉!评论已关闭.