The filters in the servlet and JSP are all Java classes

Source: Internet
Author: User


JspFilter Filters


The filters in Servlets and JSPs are Java classes, and they exist for the following purposes:


    • Intercept the backend resource when it is requested to access it
    • Manage responses returned to clients from the server


A number of commonly used filter types are listed below:


    • Authentication Filter
    • Data compression Filters
    • Encryption Filter
    • Filters that trigger resource access events
    • Image Conversion Filter
    • Login and verify Filters
    • MIME type chain Filter
    • Token filter
    • XSL/T filter for converting XML content


The filter will be inserted into the Web. xml file and then mapped to the servlet, JSP file name, or URL pattern. Deployment profile Web. XML can be found under the <tomcat-installation-directory>\conf directory.



When a JSP container launches a network application, it creates an instance of each filter that must be declared in the Deployment profile Web. XML and executed in the order declared.


Servlet Filter Method


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


Serial Number method & Description
1 public void DoFilter (ServletRequest, Servletresponse, Filterchain)

This method is called by the container whenever Request/response is going through the filter chain, because the client requests the resource 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

JSP Filter Example


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 a simple filter usage, but you can use these concepts from rows to construct more complex programs.


// Introducing Java packages
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.util.*;
 
/ / Implement the Filter class
Public class LogFilter implements Filter {
   Public void init(FilterConfig config)
                         Throws ServletException{
      / / Get the initialization parameters
      String testParam = config.getInitParameter("test-param");
 
      / / Print initialization parameters
      System.out.println("Test Param: " + testParam);
   }
   Public void doFilter(ServletRequest request,
                 ServletResponse response,
                 FilterChain chain)
                 Throws java.io.IOException, ServletException {
 
      / / Get the client ip address
      String ipAddress = request.getRemoteAddr();
 
      / / Output ip address and current time
      System.out.println("IP "+ ipAddress + ", Time "
                                       + new Date().toString());
 
      // Pass the request path filter chain
      chain.doFilter(request,response);
   }
   Public void destroy( ){
      /* Called before the Filter instance is removed on the server. */
   }
}


Compile the Logfilter.java file, and then place the compiled class file under the <tomcat installation directory >/webapps/root/web-inf/classes directory.


JSP filter mappings in the Web. xml 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. In the Deployment profile Web. XML, use the <filter> tag for filter mapping:


<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 in all servlets and JSP programs, as we specify "/*" in the configuration. You can also specify a servlet or JSP path if you only want to apply the filter to a few servlets or JSP programs.






Now, accessing the servlet or JSP page as you normally would, you will find that the log in the server generates a record of the visit. You can also use the log4j recorder to record logs in other files.


Using multiple filters


Your Web application can define a number of different filters. Now that you have defined two filters, Authenfilter and Logfilter, the other steps are the same as before, unless you want to create a different mapping, like this:


<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>
Application Order of filters


The mapping order of <filter> elements in Web. XML determines the order in which the containers apply these filters. To reverse the order of the apps, you only need to reverse the order in which the <filter> elements are defined in Web. Xml.



For example, the above example will first apply Logfilter and then apply Authenfilter, but the following example will reverse the order of the application:


<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>


The filters in the servlet and JSP are all Java classes


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.