Java Web realizes the function of automatically jumping to the landing page after the session expires [based on filter]
This article describes the Java Web Implementation of the function of automatically jumping to the landing page after the session expires. Share with you for your reference, as follows:
Automatically jump to the landing page after the session expires through a filter
Filters only work on servers that are compatible with servlet specification version 2.3. If your web application needs to support legacy servers, you can't use filters.
1、 Establish basic filter
Building a filter involves the following five steps:
1) Establish a class sessionfilter that implements the filter interface. This class requires three methods: dofilter, init, and destroy. The dofilter method contains the main filter code, the init method establishes the setting operation, and the destroy method clears it. 2) Put the filter behavior in the dofilter method. The first parameter of the dofilter method is the ServletRequest object. This object provides the filter with information about the entry (including form data, cookies and HTTP request headers). The second parameter is servletresponse, which is usually ignored in simple filters. The last parameter is filterchain, which is used to call servlets or JSP pages as described in the next step. 3) call the dofilter method of the sessionfilter object. The dofilter method of the filter interface takes a filterchain Object as one of its parameters. When the dofilter method of this object is called, the next related filter is activated. If no other filter is associated with a servlet or JSP page, the servlet or JSP page is activated. 4) Register filters for the corresponding servlets and JSP pages. Use the filter and filter mapping elements in the deployment descriptor file (web. XML). 5) disable the activator servlet. Prevent users from bypassing filter settings using the default servlet URL.
The source code is as follows:
2、 On the web XML configuration file
Readers interested in more Java related content can view the topics on this site: Java data structure and algorithm tutorial, summary of java file and directory operation skills, summary of Java DOM node operation skills, and summary of Java cache operation skills