This article describes the Java Web implementation function that automatically jumps to the login page after the session expires. Share it for your reference, as follows:
Through filters, the session will automatically jump to the login page after it expires.
Filters are only useful on servers compatible with servlet specification version 2.3. If your web application needs to support legacy servers, you cannot use filters.
1. Establish basic filters
Establishing a filter involves the following five steps:
1) Establish a class SessionFilter that implements the Filter interface . This class requires three methods, namely: doFilter, init and destroy. The doFilter method contains the main filtering code, the init method establishes the setting operation, and the destroy method cleans up.
2) Put filtering behavior in the doFilter method . The first parameter of the doFilter method is the ServletRequest object. This object provides the filter with full access to incoming information, 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, as described in the next step, which is used to call a servlet or JSP page.
3) Call the doFilter method of the SessionFilter object . The doFilter method of the Filter interface takes a FilterChain object as its parameter. When calling the doFilter method of this object, the next related filter is activated. If no other filter is associated with the servlet or JSP page, the servlet or JSP page is activated.
4) Register filters for the corresponding servlet and JSP pages . Use 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:
package com.base.filter;import java.io.IOException;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 destroy, generally releasing resources} /** * Some urls need to log in to access (session verification 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 whether the session expires if ((User) session.getAttribute(SessionKeyConstants.LOGIN) == null) { String errors = "You are not logged in yet, or the session has expired. Please log in first!"; request.setAttribute("Message", errors); //Skip to the login page request.getRequestDispatcher("/login.jsp").forward(request, response); } else { arg2.doFilter(request, response); } } public void init(FilterConfig arg0) throws ServletException { // Initialize the operation, read the initialization parameters of the filter configuration in web.xml, and meet the requirements you make no use of this method}}2. Configure in the web.xml configuration file
<!-- Set session expiration time to 30 minutes --><session-config> <session-timeout>30</session-timeout></session-config><!-- session filter configuration related--><filter> <filter-name>SessionFilter</filter-name> <filter-class>com.base.filter.SessionFilter</filter-class></filter><filter-mapping> <filter-name>SessionFilter</filter-name> <url-pattern>/contract/*</url-pattern> <url-pattern>/user/*</url-pattern> <dispatcher>FORWARD</dispatcher> <!--In this case, if the request starts with /contract/… or /user/… and is passed through the forward method of the request dispatcher or is directly passed from the client, it must go through this filter. --> <dispatcher>REQUEST</dispatcher></filter-mapping>
For more information about Java related content, please check out the topics of this site: "Java Data Structure and Algorithm Tutorial", "Summary of Java File and Directory Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.