求java大神给我解释下重定向后session为什么会失效?
下面是我在Servlet里写的验证提交后的表单代码?publicvoiddoPost(HttpServletRequestrequest,HttpServletRespo...
下面是我在Servlet里写的验证提交后的表单代码?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("username");
String password=request.getParameter("userpassword");
HttpSession session=request.getSession();
if(name.equals("admin")&&password.equals("admin")){
session.setAttribute("name", name);
request.getRequestDispatcher("Success.jsp").forward(request, response);
}else {
response.sendRedirect("index.jsp");
}
}
我的问题是当把request.getRequestDispatcher("Success.jsp").forward(request, response);改成response.sendRedirect("Success.jsp");重定向到成功页面后session的值为啥就取不到了? 展开
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("username");
String password=request.getParameter("userpassword");
HttpSession session=request.getSession();
if(name.equals("admin")&&password.equals("admin")){
session.setAttribute("name", name);
request.getRequestDispatcher("Success.jsp").forward(request, response);
}else {
response.sendRedirect("index.jsp");
}
}
我的问题是当把request.getRequestDispatcher("Success.jsp").forward(request, response);改成response.sendRedirect("Success.jsp");重定向到成功页面后session的值为啥就取不到了? 展开
2个回答
展开全部
HttpSession session=request.getSession();
改为
HttpSession session=request.getSession(true);
下一次取session的时候
HttpSession session=request.getSession(false);
你是怎么取值的?el表达式?还是在java代码里面
改为
HttpSession session=request.getSession(true);
下一次取session的时候
HttpSession session=request.getSession(false);
你是怎么取值的?el表达式?还是在java代码里面
追问
下一次取值是在jsp页面里面了是session.setAttribute("name");
追答
session.getAttribute("name");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询