1 web层LoginServlet修改
package www.test.web.servlet; import java.io.IOException; import java.net.URLEncoder; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import www.test.domain.User; import www.test.service.LoginService; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决乱码问题 request.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(); // 获得页面输入的验证 String checkCode_client = request.getParameter("checkCode"); // 获得生成图片的文字的验证码 String checkCode_session = (String) request.getSession().getAttribute("checkcode_session"); // 比对页面的和生成图片的文字的验证码是否一致 if (!checkCode_session.equals(checkCode_client)) { request.setAttribute("loginInfo", "您的验证码不正确"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } // 获取用户输入的数据 String username = request.getParameter("username"); //中文名张三 String password = request.getParameter("password"); LoginService service = new LoginService(); User user = null; try { user = service.login(username, password); } catch (SQLException e) { e.printStackTrace(); } if (user != null) { // 登录成功 // 判断用户是否勾选自动登录 String autoLogin = request.getParameter("autoLogin"); if (autoLogin != null) { //对中文张三进行编码 String username_code = URLEncoder.encode(username, "UTF-8");// %AE4%kfj Cookie cookie_username = new Cookie("cookie_username", username_code); Cookie cookie_password = new Cookie("cookie_password", password); // 设置 cookie 的持久化时间 cookie_username.setMaxAge(60 * 60); cookie_password.setMaxAge(60 * 60); // 设置 cookie 的携带路径 cookie_username.setPath(request.getContextPath()); cookie_password.setPath(request.getContextPath()); // 发送 cookie response.addCookie(cookie_username); response.addCookie(cookie_password); } //将登录的用户的 user 对象存到 session 中 session.setAttribute("user", user); //重定向到首页 response.sendRedirect(request.getContextPath()); } else { request.setAttribute("loginInfo", "密码或者用户名不正确"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2 web层AutoLoginFilter
package www.test.web.filter; import java.io.IOException; import java.net.URLDecoder; import java.sql.SQLException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import www.test.domain.User; import www.test.service.LoginService; public class AutoLoginFilter implements Filter { public AutoLoginFilter() { super(); } private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; HttpSession session = request.getSession(); // 获得 cookie 中用户名和密码 进行登录的操作 // 定义 cookie_username String cookie_username = null; // 定义 cookie_password String cookie_password = null; // 获得 cookie Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) {// 获得名字是 cookie_username 和 // cookie_password if ("cookie_username".equals(cookie.getName())) { cookie_username = cookie.getValue(); //恢复中文用户名 cookie_username = URLDecoder.decode(cookie_username, "UTF-8"); } if ("cookie_password".equals(cookie.getName())) { cookie_password = cookie.getValue(); } } } // 判断 username 和 password 是否是 null if (cookie_username != null && cookie_password != null) { // 登录的代码 LoginService service = new LoginService(); User user = null; try { user = service.login(cookie_username, cookie_password); } catch (SQLException e) { e.printStackTrace(); } // 将登录的用户的 user 对象存到 session 中 session.setAttribute("user", user); } filterChain.doFilter(req, res); // 这里放到达目的地(离开)的处理代码 //放行 filterChain.doFilter(request, response); } public void destroy() { } }