Servlet3.0 provides @webfilter annotations to define a class that implements the Javax.servlet.Filter interface as a filter so that when we create a filter in a web app, 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 PackageMe.gacl.web.filter;2 Importjava.io.IOException;3 ImportJavax.servlet.Filter;4 ImportJavax.servlet.FilterChain;5 ImportJavax.servlet.FilterConfig;6 Importjavax.servlet.ServletException;7 Importjavax.servlet.ServletRequest;8 ImportJavax.servlet.ServletResponse;9 ImportJavax.servlet.annotation.WebFilter;Ten One /** A * Use annotations to define encoding filters - * @WebFilter defines a class that implements the Javax.servlet.Filte interface as a filter - * Attribute filtername the name of the filter, optional the * attribute urlpatterns specifies the URL pattern to filter, or it can be declared using the property value. (Specifies that the URL pattern to filter is a required attribute) - */ -@WebFilter (filtername= "Servlet3filter", urlpatterns= "/*") - Public classServlet3filterImplementsFilter { + - @Override + Public voiddestroy () { ASystem.out.println ("Filter Destroy"); at } - - @Override - Public voidDoFilter (servletrequest request, servletresponse response, -Filterchain chain)throwsIOException, servletexception { -System.out.println ("Perform filter operation"); in Chain.dofilter (request, response); - } to + @Override - Public voidInit (filterconfig config)throwsservletexception { theSYSTEM.OUT.PRINTLN ("Filter Initialization"); * } $}
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 the Urlpatterns property or the Value property specifies 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-appversion= "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/javaee6 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>Ten </welcome-file-list> One </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)