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.

//Introduce Java package import Java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;//Implement Filter Class public class Logfilter implements Filter {public void init (Filterconfig config) throws Serv        letexception{//Get initialization parameter String Testparam = Config.getinitparameter ("Test-param");    Print initialization Parameters System.out.println ("Test Param:" + Testparam); } public void DoFilter (ServletRequest request, servletresponse response, Filterchain C Hain) throws Java.io.IOException, servletexception {//Get client IP address String ipAddress = Request       . GETREMOTEADDR (); The output IP address and the current time System.out.println ("IP" + ipAddress + "," + "," + new Date "). ToS       Tring ());   Pass the request channel filter chain chain.dofilter (request,response); The public void Destroy () {/* is called before the filter instance is removed from 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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.