The servlet has a very interesting and useful function, that is, filter. The so-called filter is a small program that executes before a user requests to the servlet, or after the servlet executes, to preprocess a user request, or to do some public processing of the results returned by the servlet.
Filter can be executed before the request reaches the Servlet, even if a static page or picture is requested, and the filter is executed preferentially. This allows us to preprocess all objects within the application, including Java programs and static content, through filter. (This function I'm afraid the PHP program can not do it.) )
Filter can usually be used in the following situations:
• Certification
• Record access logs
• Encryption and decryption
• Character encoding Conversion
• Data compression
As the most commonly used "authentication", you can write the authentication code in the filter, so that not only eliminates the authentication in the servlet trouble, but also control the site static content access.
Filter is an interface, located in Javax.servlet.Filter. Its relationship with other interfaces is as follows:
When we write our own filter, we simply write a class to implement the filter interface.
Implementing the filter interface requires the following methods:
Copy Code code as follows:
void init (Filterconfig filterconfig) throws Servletexception
The initialization method of the filter.
Copy Code code as follows:
The method to execute before the filter is finished.
Copy Code code as follows:
void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception
The execution content of the filter. The parameter request and response are input and output parameters respectively, while chain is the filter chain object.
So-called Filterchain, a filter within an application can have multiple, they are strung together like chains, in which a filter executes and can invoke Chain.dofilter (request, response) Transfer execution to the next filter until all of the filter has finished executing.
This example program we modified the previous section of the login program, for it added a download link (see Sessiontest.java). This download link points to img/sample.jpg. We want to be able to pass a filter that authenticates the user when the user tries to download the img/sample.jpg, and if the validation fails, the user is prevented from downloading and jumps to the login page.
When you create a filter, unfortunately, you can't just choose New from the right-click menu as a servlet, and you can only write the source code and add it manually to the Web.xml. As shown in the following figure, note that you want to implement the Javax.servlet.Filter interface when you create a new class.
After you have finished writing the code, open the Web.xml file and add the filter manually. Like the servlet, you need to add and two tags. Here we want to/img/all the files under the login verification, so the Url-pattern place we fill out the/img/*.
The last execution, access to/learnjsp/img/sample.jpg, will jump to the login page. Once you have logged in, visit the above link again to see the correct picture.
Sample Downloads: Servlet-filter_jb51net.zip