Servlet3.0 provides @webfilter annotations to define a class that implements the Javax.servlet.Filter interface as a filter so that when we use a filter in a Web application, It is no longer necessary to configure the relevant description of the filter in the Web. xml file.
Let's create a filter and experience using the @webfilter annotation callout filter as follows:
The code for the filter is as follows:
1 package me.gacl.web.filter; 2 Import java.io.IOException; 3 Import Javax.servlet.Filter; 4 Import Javax.servlet.FilterChain; 5 Import Javax.servlet.FilterConfig; 6 Import javax.servlet.ServletException; 7 Import Javax.servlet.ServletRequest; 8 Import Javax.servlet.ServletResponse; 9 Import javax.servlet.annotation.webfilter;10 11/**12 * Use annotation Callout Filter 13 * @ Webfilter defines a class that implements the Javax.servlet.Filte interface as a filter 14 * attribute filtername the name of the declaration filter, optionally 15 * properties urlpatterns Specify the URL pattern to filter You can also use the attribute value to declare it. (Specifies that the URL pattern to filter is a required attribute) */17 @WebFilter (filtername= "Servlet3filter", urlpatterns= "/*") public class Servlet3filter Implements filter {@Override21 public void Destroy () {System.out.println ("filter Destruction"); 23}24 25 @Override26 public void DoFilter (ServletRequest request, Servletresponse response,27 Filterchain chain) Throws IOException, servletexception {System.out.println ("Performing filtering Operations"); Chain.dofilter (Request, response );}31 @Override33 PublIC void init (filterconfig config) throws servletexception {System.out.println ("filter Initialization"); 35}36}
The filter is configured with @WebFilter (filtername= "Servlet3filter", urlpatterns= "/*") , and filter's name is Servlet3filter, Represents urlpatterns= "/*" that the filter intercepts all requests.
The filter is initialized when the Web server starts, as shown in:
When a user accesses a resource in a Web app, the filter is intercepted, as shown in:
As you can see, with @webfilter annotations, we can define a class that implements the Javax.servlet.Filte interface as a filter, using the Urlpatterns property or the Value property to specify the URL pattern to filter.
Multiple filtering modes can be specified @webfilter (filtername= "Servlet3filter", urlpatterns={"/usermanagerservlet", "/index.jsp"})
With @webfilter annotations, our web. XML does not have to be configured.
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "3.0" 3 xmlns= "http://java.sun.com/xml/ Ns/javaee " 4 xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " 5 xsi:schemalocation=" http ://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> 7 < Display-name></display-name> 8 <welcome-file-list> 9 <welcome-file>index.jsp </welcome-file>10 </welcome-file-list>11 </web-app>
This way our web. xml file is very clean. It can be said that the advent of the SERVLET3.0 specification greatly reduces the effort to develop servlet and filter configurations.
SERVLET3.0 Learning Summary (ii)--Using annotation labeling filters (filter)