1. Create a class that implements the filter interface
All filters must implement Javax.servlet.Filter. This interface contains three methods, namely Dofilter, Init, and destroy.
Copy Code code as follows:
public void Dofilter (Servletrequset request,
Servletresponse response,
Filterchain chain)
Thows Servletexception, IOException
Whenever a filter is invoked (that is, each time a servlet or JSP page associated with this filter is requested), the Dofilter method is executed. It is this method that contains most of the filtering logic. The first parameter is the servletrequest associated with the incoming request. For simple filters, most filtering logic is based on this object. If you are processing an HTTP request and need to access a method that is not available in servletrequest such as GetHeader or getcookies, you construct the object as a httpservletrequest.
The second parameter is servletresponse. This parameter is usually ignored except in the case of two scenarios where it is to be used. First, if you want to completely block access to related servlet or JSP pages. You can invoke Response.getwriter and send a response directly to the client. Second, if you want to modify the output of the relevant servlet or JSP page, you can include the response in an object that collects all the output sent to it. Then, after calling the Serlvet or JSP page, the filter can check the output, modify it if appropriate, and then send it to the client.
The last parameter of the Dofilter is the Filterchain object. Invoke Dofilter on this object to activate the next filter associated with the servlet or JSP page. If there is no other relevant filter, the Dofilter call activates the servlet or the JSP itself.
The public void init (filterconfig config) thows servletexception init method executes only the first time this filter is initialized, not every time a filter is invoked.
For simple filters, you can provide an empty body of this method, but there are two reasons to use Init. 1 The Filterconfig object provides access to the servlet environment and the filter name assigned in the Web.xml file.
Therefore, the common approach is to use Init to store Filterconfig objects in a field so that the Dofilter method can access the servlet environment or filter name. 2 Filterconfig object has a Getinitparameter method. It can access the filter initialization parameters allocated in the deployment descriptor file (Web.xml).
Copy Code code as follows:
Most filters simply provide an empty body for this method, but you can use it to complete cleanup tasks such as closing files or database connection pools used by the filter.
1.2 Put the filter behavior into the Dofilter method
The Dofilter method is a key part of most filters. The dofilter is executed whenever a filter is invoked. For most filters, the steps performed by Dofilter are based on incoming information.
Therefore, you may want to take advantage of the ServletRequest provided as the first parameter of Dofilter. This object is often constructed as a httpservletrequest type to provide access to a more specific method of the class.
1.3 Calling the Dofilter method of the Filterchain object
The Dofilter method of the filter interface takes a Filterchain object as its third parameter.
Activates the next associated filter when the object's Dofilter method is invoked.
This process generally lasts until the last filter in the chain. Activates the servlet or page itself when the last filter calls its Filterchain object's Dofilter method.
However, any filter in the chain can break this process by dofilter methods that do not call its filterchain.
In such cases, the Serlvet of the JSP page is no longer invoked,
And the filter that interrupts this call process is responsible for providing the output to the client that is, the filter-----Select the Dofilter method that invokes Fileterchain ———— called sequentially, and finally activates the servlet/jsp
| ———— do not call, select Interrupt —————— The current filter is responsible for the output
1.4 Registering filters for appropriate servlet and JSP pages
Copy Code code as follows:
The 1.filter element filter element is located in the front of the deployment descriptor file (Web.xml), before all filter-mapping, servlet, or servlet-mapping elements.
The filter element has the following six possible child elements:
1. Icon This is an optional element that declares an image file that the IDE can use.
2, Filter-name This is a required element, which assigns a selected name to the filter.
3, Display-name This is an optional element that gives the short name used by the IDE.
4, Description This is also an optional element, it gives the IDE's information, provides a text document.
5, Filter-class This is a required element that specifies the fully qualified name of the filter implementation class.
6, Init-param This is an optional element that defines the initialization parameters that can be read using the Filterconfig Getinitparameter method. A single filter element can contain multiple init-param elements.
Please note that filtration was first introduced in the Serlvet specification version 2.3. Therefore, the Web.xml file must use the 2.3 version of the DTD. Here's a simple example:
2.filter-mapping element
The filter-mapping element precedes the Serlvet element after the filter element in the Web.xml file. It contains the following three possible child elements:
1. Filter-name This required element must match the name given to the filter when declared with the filter element.
2. Url-pattern This element declares a pattern that starts with a slash (/) that specifies the URL to which the filter applies. Url-pattern or servlet-name must be provided in all filter-mapping elements. However, you cannot supply multiple Url-pattern element items to a single filter-mapping element. If you want the filter to be suitable for more than one pattern, repeat the entire filter-mapping element. That is:/The back of the thing, is to be filtered
3. Servlet-name This element gives a name that must match the name of the servlet or JSP page that was given using the servlet element. You cannot supply multiple servlet-name element items to a single filter-mapping element. Repeat this filter-mapping element if you want the filter to be appropriate for multiple servlet names.
1.5 Disable the Activator servlet
??? I don't understand.