Servlet has a very interesting and useful function, that is, Filter. The so-called Filter is a small program that is executed after the user request arrives at the Servlet or after the Servlet is executed. This program can pre-process the user request, you can also perform some public processing on the Servlet returned results.
The Filter can be executed before the request arrives at the Servlet. Even if a static page or image is requested, the Filter will be executed first. In this way, all objects in the application (including Java programs and static content) can be preprocessed Through Filter. (I am afraid the PHP program will not be able to implement this function .)
Filters can be used in the following scenarios:
• Authentication
• Access Logging
• Encryption and decryption
• Character encoding and conversion
• Data Compression
For example, the most common "authentication" above can be used to write authentication code in the Filter, which not only saves the trouble of authentication in Servlet, but also controls the access to static content on the website.
Filter is an interface located in javax. servlet. Filter. Its relationship with other interfaces is as follows:
When writing our own Filter, we only need to write a class to implement the Filter interface.
To implement the Filter interface, you must implement the following methods:
Copy codeThe Code is as follows: void init (FilterConfig filterConfig) throws ServletException
Filter initialization method.Copy codeThe Code is as follows: void destroy ()
The method executed before the Filter ends.Copy codeCode: void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
The execution content of the Filter. Request and response parameters are input and output parameters respectively, while chain is the Filter chain object.
The so-called FilterChain, one application can have multiple filters, they can be chained together, one of which can be called after a Filter is executed. doFilter (request, response) transfers the execution permission to the next Filter until all the filters are executed.
In this example, we modified the login program in the previous section and added a download link for it (for details, see SessionTest. java ). This download link points to img/sample.jpg. We hope to use a Filter to verify the user's identity when the user tries to download img/sample.jpg. If the verification fails, the user is prohibited from downloading and the logon page is displayed.
When creating a Filter, it is a pity that you cannot choose to create a Filter directly from the right-click menu like Servlet. Instead, you can only write the source code and add it to web. xml manually. For example, you must implement the javax. servlet. Filter interface when creating a class.
After writing the code, open the web. xml file and manually add a Filter. Like Servlet, you need to add two tags. Here we need to perform login verification for all files under/img/, So we enter/img/* in url-pattern /*.
Finally, access/LearnJSP/img/sample.jpg To Go To The logon page. After logging on, visit the above link again to see the correct picture.
Example download: servlet-filter_jb51net.zip