本帖最后由 hejinzhong 于 2014-9-11 08:40 编辑
- //过滤器功能
- public class UserLoginFilter implements Filter {
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- System.out.println("过滤启动");
- }
- @Override
- public void doFilter(ServletRequest req, ServletResponse res,
- FilterChain chain) throws IOException, ServletException {
-
- //将参数转换为和协议有关
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
- //(1)如果请求登录界面,则放行
- if("/HE06/index.jsp".equals(request.getServletContext())){
- chain.doFilter(request, response);
- return;
- }
- //(2)如果已登录,则放行
- if(request.getSession().getAttribute("user") != null){
- chain.doFilter(request, response);
- return;
- }
-
- //下面是未登录,则根据携带cookie与否,和是否正确来判断
-
- Cookie tempCookie = null;
-
- //判断是否有cookie
- Cookie[] cookie = request.getCookies();
- if(cookie != null){
- for (Cookie ck : cookie) {
- if("autoinfo".equals(ck.getName())){
- tempCookie = ck;
- }
- }
- }
-
- /*cookie是否正确
- if(tempCookie != null){
- User user = new User(tempCookie.getValue().split("#")[0],
- tempCookie.getValue().split("#")[1]);
- user = new UserService().find(user);
-
- //不正确,则删除cookie信息
- if(user == null){
- tempCookie.setMaxAge(0);
- tempCookie.setPath("/");
- response.addCookie(tempCookie);
- chain.doFilter(request, response);
- return;
- }
-
- //如果信息正确,则记录登录,并放行
- request.getSession().setAttribute("user", user);
- }
- chain.doFilter(request, response);
- */
-
- if(tempCookie == null){
- chain.doFilter(request, response);
- return;
- }
-
- String[] userStr = tempCookie.getValue().split("#");
- User user = new User(userStr[0], userStr[1]);
- user = new UserService().find(user);
-
- if(user == null){
- tempCookie.setMaxAge(0);
- tempCookie.setPath("/");
- response.addCookie(tempCookie);
- chain.doFilter(request, response);
- return;
- }
- request.getSession().setAttribute("user", user);//记录登录
- chain.doFilter(request, response);
- }
- @Override
- public void destroy() {
- System.out.println("过滤销毁");
- }
- }
- ------------------------------------------------------------------------
- //登录处理功能
- public class UserLoginServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- /**
- * 从请求中获取相应参数,封装后,传递给服务器查找
- * 如果可以查找到,即登录成功
- * 然后判断用户是否勾选了自动登录
- */
-
- //封装并查询
- User user = new User(request.getParameter("name"),request.getParameter("password"));
- user = new UserService().find(user);
-
- //根据查询结果进行操作
- if(user != null){
- //设置cookie信息
- if(request.getParameter("auto") != null){
- Cookie cookie = new Cookie("autoinfo",user.getName()+"#"+user.getPassword());
- cookie.setMaxAge(60*60);
- cookie.setPath("/");
- response.addCookie(cookie);
- }
-
- //记录登录状态,并重定向到成功界面
- request.getSession().setAttribute("user", user);
- response.sendRedirect(request.getContextPath()+"/suc.jsp");
- }else{
- //不成功则给提示
- request.setAttribute("msg", "用户名和密码不一致");
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- }
- }
- }
复制代码
|