Java Web realizes the function of automatically jumping to the landing page after session expires [Based on filter]

  • 2020-11-18 06:16:12
  • OfStack

This article illustrates Java Web's implementation of the function of automatically jumping to the landing page after the expiration of session. To share for your reference, the details are as follows:

By means of filter, session will automatically jump to the landing page after expiration

Filters are only available on servers compatible with version 2.3 of the servlet specification. If your Web application needs to support older servers, you can't use filters.

1. Set up a basic filter

Setting up a filter involves the following five steps:

1) Establish a class SessionFilter that implements Filter interface. This class requires three methods: doFilter, init, and destroy. The doFilter method contains the main filtering code, the init method establishes the setup operation, and the destroy method clears.
2) Put filtering behavior in doFilter method. The first argument to the doFilter method is the ServletRequest object. This object gives the filter full access to incoming information, including form data, cookie, and HTTP request headers. The second parameter, ServletResponse, is usually ignored in simple filters. The last parameter, FilterChain, described in step 1 below, is used to call servlet or JSP pages.
3) Call the doFilter method of the SessionFilter object. The doFilter method of the Filter interface takes an FilterChain object as one of its arguments. When the doFilter method for this object is called, the next relevant filter is activated. If no other filter is associated with an servlet or JSP page, the servlet or JSP page is activated.
4) Register filters for corresponding servlet and JSP pages. Use the filter and ES52en-ES53en elements in the deployment descriptor file (web.xml).
5) Disable the activator servlet. Prevents users from taking advantage of the default servlet URL to bypass the filter Settings.

Source code is as follows:

package com.base.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;
import com.base.constants.SessionKeyConstants;
import com.mvc.entity.User;
public class SessionFilter implements Filter {
  public void destroy() {
    //  Filter destruction, 1 The general is to release resources 
   *  Some of the url Need to log in to access ( session Validation filter) 
  public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
      throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) arg0;
    HttpServletResponse response = (HttpServletResponse) arg1;
    HttpSession session = request.getSession();
    // judge session Is late 
    if ((User) session.getAttribute(SessionKeyConstants.LOGIN) == null) {
      String errors = " You haven't logged in yet, or session Has expired. Please login first !";
      request.setAttribute("Message", errors);
      // Jump to the login page 
      request.getRequestDispatcher("/login.jsp").forward(request, response);
    } else {
      arg2.doFilter(request, response);
  public void init(FilterConfig arg0) throws ServletException {
    //  Initialize operation, read web.xml This method is not used to initialize the filter configuration parameters to meet your requirements 

2. Configure in web.xml configuration file

<!--  Set up the session Expiration time is 30 minutes  -->
<!-- session Filter configuration correlation  -->
    <!-- In this case, if the request is /contract/ ... or /user/ ... It begins and it goes through request dispatcher the forward Methods passed in or directly from the client must pass through this filter. -->

For more information about java, please refer to Java Data Structure and Algorithm Tutorial, Java File and Directory Operation Skills Summary, Java Operation Skills Summary of DOM Node and Java Cache operation Skills Summary.

I hope this article has been helpful for java programming.

Related articles: