刚刚接触 javaweb 要实现 自动登录 遇到以下问题
- cookie session 都设置好 配置文件 也没错 还是不可以 在登录后直接跳转 index0.jsp页面
块引用
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");//用户输入
String password = request.getParameter("password");
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
//out.print(username);
//out.print(password);
String s = dojdbc();
System.out.println(s);
String [] a = s.split("-") ;
String s_username = a[0];
String s_password = a[1];
if(s_username.equals(username)&&s_password.equals(password))
{
User user_detial = new User();
user_detial.setUsername(username);
user_detial.setPassword(password);
session.setAttribute("user", user_detial);
String autoLogin = request.getParameter("autologin");//记住密码打勾
System.out.println(autoLogin);
//cookie设置
if(autoLogin!=null)
{
System.out.println("记住密码");
Cookie cookie = new Cookie("autologin", username + "-" + password);
cookie.setMaxAge(60*60);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
System.out.println("cookie 设置完成");
}
//request.getSession().setAttribute("user", user);//用户对象放入Session域
//response.sendRedirect("success.jsp");//测试session加入成功
response.sendRedirect("index01.jsp");
}else {
//out.print("casfafasfa");
request.setAttribute("errerMsg", "用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
块引用
2.在filter 中 加上了页面跳转语句后 实现了自动跳转 不知道这种 方法是否合理