java verifies that the user is logged in to java for automatic login

  • 2020-05-09 18:34:44
  • OfStack

This article shares the detailed code of java to verify whether the user has logged in and to realize automatic login for your reference. The specific content is as follows

1. Verify whether the user has logged in

package cn.hongxin.filter;


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

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


public class LoginFilter implements Filter{

  public void init(FilterConfig filterConfig) throws ServletException {


  public void doFilter(ServletRequest request, ServletResponse response,

      FilterChain chain) throws IOException, ServletException {

    // will request Equivalent to htt...

    HttpServletRequest req = (HttpServletRequest) request;

    // To obtain session

    HttpSession ss = req.getSession();

    // from session To derive user


      System.err.println(" You haven't logged in yet ");

      req.getSession().setAttribute("msg", " Please log in first ");

      // Redirect to login 

      HttpServletResponse resp = (HttpServletResponse) response;



      // release 

      chain.doFilter(request, response);



  public void destroy() {



Configure to web.xml and consider jsps/* :










2. Realize automatic login to  

Automatic login is to help users use the page multiple times without having to enter their username and password again.

The user saves the user's login information, the person, to a local file called Cookie.

At the time of statement of   Name,value, new Cookie(key,value);

  Path               - the default value is the path where cookie is currently saved, serlvet.

If Cookie in this path: http: / / loclhost: 8080 / project/abc/AServlet, Cookie path as: http: / / loclhost project/abc


Place in http: / / loclhost/project/abc directory servlet can read the cookie values.


Save Cookie class: http: / / loclhost: 8080 / project/a/b/AServlet, Cookie default path as; http: / / loclhost project/a/b

Step 1: develop a login page


  <c:when test="${empty}">

    <form name="x" method="post" action="<c:url value='/LoginServlet'/>">

      Name:<input type="text" name="name"/><br/>


      <input type="radio" name="auto" value="-1"> No automatic login 


      <input type="radio" name="auto" value="1">1 day <br/>

      <input type="radio" name="auto" value="7">1 weeks <br/>

      <input type="submit"/>




     You're logged in :${name}<br/>

    <a href="<c:url value='/LoginServlet'/>"> exit </a>



Step 2: successfully save cookie

  public void doPost(HttpServletRequest request, HttpServletResponse response)

      throws ServletException, IOException {

    // Receive user name 

    String name = request.getParameter("name");

    String auto = request.getParameter("auto");

    // Put the user information in session


    // judge auto Whether it is -1


      int day = Integer.parseInt(auto);//1|7

      int seconds = 60*60*24*day;

      // The statement cookie

      Cookie c = new Cookie("autoLogin",name);



      // save cookie






Step 3: request to visit any page in the site should be automatically logged in

Write a filter to override all url=/*. Read all cookie in doFilter. Is there a name cookie with the name autoLogin?

Always let go.


public void doFilter(ServletRequest request, ServletResponse response,

      FilterChain chain) throws IOException, ServletException {

    // Read here cookie

    HttpServletRequest req = (HttpServletRequest) request;

    // Get what you have cookie

    Cookie[] cs = req.getCookies();


      for(Cookie c:cs){

       if(c.getName().equals("autoLogin")){// If there is an automatic login cookie

         String value = c.getValue();// The user name 

         // Login is successful 

         req.getSession().setAttribute("name", value);





    // Whether it is automatically logged in or not 

    chain.doFilter(request, response);


Step 4: configure to web.xml for all url=/*









  step 5: development exit

System.err.println(" User exit ");

    // Delete the entire session


    Cookie c = new Cookie("autoLogin", "ddd");




//   request.getSession().removeAttribute("name");


Step 6: optimize the code

      because the user will enter AutoFiilter doFilter method when manually logging in, and read all Cookie once. This traversal is redundant for the user.

      so LoginServet url should be overdone in doFiler.

      and there is no automatic login for logouts.

The above is the entire content of this article, I hope to help you with your study.

Related articles: