JavaWeb实现自动登录

自动登录是通过存储cookie值来实现的。

工程目录如下:

JavaWeb实现自动登录_第1张图片

login.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




登录


    
用户名:
密码: 自动登录

success.jsp(主要用于测试是否储存成功):

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


    ${sessionScope.username }
${sessionScope.password }

LoginServlet(用于登录逻辑处理):

package servletLogin;

import java.io.IOException;
import java.net.URLEncoder;

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;


public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        HttpSession session = request.getSession();
        //登录成功,(在这应该是和数据库的值对比)
        if(username.equals("alan呵呵") && password.equals("123")){
            
            String autoLogin = request.getParameter("autoLogin");

            if(autoLogin!=null){

                //编码是为了在cookie中存储汉字
                String usernameCode = URLEncoder.encode(username, "utf-8");
                
                Cookie usernameCookie = new Cookie("username",usernameCode);
                Cookie passwordCookie = new Cookie("password",password);
                //设置持久化时间
                usernameCookie.setMaxAge(60*60);
                passwordCookie.setMaxAge(60*60);
                //设置cookie携带路径
                usernameCookie.setPath(request.getContextPath());
                passwordCookie.setPath(request.getContextPath());
                //发送cookie
                response.addCookie(usernameCookie);
                response.addCookie(passwordCookie);
            }
            
            session.setAttribute("username", username);
            session.setAttribute("password", password);
            response.sendRedirect("success.jsp");
        }else{
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
}


LoginFilter(用于自动登录的拦截器):

package filter;

import java.io.IOException;
import java.net.URLDecoder;

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.HttpSession;


public class LoginFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();

        // 获得cookie中用户名和密码 进行登录的操作
        // 定义cookie_username
        String cookie_username = null;
        // 定义cookie_password
        String cookie_password = null;
        // 获得cookie
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                // 获得名字是cookie_username和cookie_password
                if ("username".equals(cookie.getName())) {
                    cookie_username = cookie.getValue();
                    // 对cookie中的值解码
                    cookie_username = URLDecoder.decode(cookie_username,
                            "UTF-8");
                }
                if ("password".equals(cookie.getName())) {
                    cookie_password = cookie.getValue();
                }
            }
        }
        
        if(cookie_username != null && cookie_password != null){
            if(cookie_username.equals("alan呵呵") && cookie_password.equals("123")){
                session.setAttribute("username", cookie_username);
                session.setAttribute("password", cookie_password);
            }
        }
        chain.doFilter(request, response);
    }
    public void destroy() {
    }
    public void init(FilterConfig fConfig) throws ServletException {
    }
}

web.xml配置文件:



  WEB24
 
 
 
  
 
      AutoLoginFilter
      filter.LoginFilter
  
  
      AutoLoginFilter
      /*
  
 
 
  
    login.jsp
  
 
  
    LoginServlet
    servletLogin.LoginServlet
  
  
    LoginServlet
    /login
  
 





你可能感兴趣的:(Servlet&Jsp)