Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP filter


May 12, 2021 JSP


Table of contents


JSP filter

The filters in both Servlet and JSP are Java classes that exist for the following purposes:

  • Intercept a back-end resource when it is requested
  • Manages the response returned to the client from the server

A number of commonly used filter types are listed below:

  • Certified filters
  • Data compression filter
  • Encrypt the filter
  • The filter that triggered the resource access event
  • Image conversion filter
  • Sign in and verify the filter
  • MIME type chain filter
  • Token filter
  • XSL/T filters that convert XML content

The filter is inserted into the web .xml file and then mapped to the name of the servlet, JSP file, or URL mode. The deployment profile web .xml can be found in the directory of the .lt;Tomcat-design-directory-conf.

When the JSP container launches a network application, it creates instances of each filter, which must be declared in the deployment description file web .xml and executed in the order in which they are declared.


Servlet filter method

One filter is a Java class that implements the javax.servlet.Filter interface. The javax.servlet.Filter interface defines three methods:

Serial number Method . . .
1 public void doFilter (ServletRequest, ServletResponse, FilterChain)

The container calls this method whenever the request/response passes through the filter chain because the client requests resources at the end of the chain

2 public void init(FilterConfig filterConfig)

The container calls this method to indicate that a filter is placed in the service

3 public void destroy()

The container calls this method to indicate that a filter is being removed from the service


Example of a JSP filter

This example will print the IP address and the date time of each access to the JSP file. Of course, this is just a simple example of some simple filter usage, but you can use these concepts to construct more complex programs from rows.

//  引入Java包
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
 
// 实现 Filter 类
public class LogFilter implements Filter  {
   public void  init(FilterConfig config) 
                         throws ServletException{
      // 获取初始化参数
      String testParam = config.getInitParameter("test-param"); 
 
      //打印初始化参数
      System.out.println("Test Param: " + testParam); 
   }
   public void  doFilter(ServletRequest request, 
                 ServletResponse response,
                 FilterChain chain) 
                 throws java.io.IOException, ServletException {
 
      // 获取客户端ip地址  
      String ipAddress = request.getRemoteAddr();
 
      // 输出ip地址及当前时间
      System.out.println("IP "+ ipAddress + ", Time "
                                       + new Date().toString());
 
      // 传递请求道过滤器链
      chain.doFilter(request,response);
   }
   public void destroy( ){
      /* 在Filter实例在服务器上被移除前调用。*/
   }
}

Compile the LogFilter .java file, and then place the compiled class file in the .lt; Tomcat installation directory./webapps/ROOT/WEB-INF/classes directory.


The .xml the JSP filter map in the file

The filter is defined and then mapped to a URL or JSP file name, similar to how the servlet is defined and then mapped. I n the deployment description .xml, the filter map is performed using the hashtag:

<filter>
   <filter-name>LogFilter</filter-name>
   <filter-class>LogFilter</filter-class>
   <init-param>
	  <param-name>test-param</param-name>
	  <param-value>Initialization Paramter</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

The above filters will be applied to all servlet and JSP programs because we have specified "/"" in the configuration. You can also specify a servlet or JSP path if you only want to apply filters to a few servlet or JSP programs.

Now, by visiting the servlet or JSP page as usual, you'll find that the server log has a record of the visit. You can also use the Log4J logger to log in other files.


Use multiple filters

Your web application can define many different filters. Now that you have defined two filters, AuthenFilter and LogFilter, the other steps are the same as described earlier, unless you want to create a different map, as follows:

<filter>
   <filter-name>LogFilter</filter-name>
   <filter-class>LogFilter</filter-class>
   <init-param>
	  <param-name>test-param</param-name>
	  <param-value>Initialization Paramter</param-value>
   </init-param>
</filter>
 
<filter>
   <filter-name>AuthenFilter</filter-name>
   <filter-class>AuthenFilter</filter-class>
   <init-param>
	  <param-name>test-param</param-name>
	  <param-value>Initialization Paramter</param-value>
   </init-param>
</filter>
 
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter-mapping>
   <filter-name>AuthenFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

The order in which the filters are applied

In the .xml the order in which the elements are mapped determines the order in which the container applies these filters. To reverse the order of the application, you only need to .xml the order in which the elements in the web are defined.

For example, the example above applies LogFilter first and then AuthenFilter, but the following example reverses the order of the applications:

<filter-mapping>
   <filter-name>AuthenFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>