servlet3.0 provides @ webfilter to implement javax. servlet. the filter interface class is defined as a filter component.
package COM. cndatacom. filter; import Java. io. ioexception; import javax. servlet. filter; import javax. servlet. filterchain; import javax. servlet. filterconfig; import javax. servlet. servletexception; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import javax. servlet. annotation. webfilter;/*** use annotation to define the encoding filter * @ author luxh * // *** @ webfilter implements javax. servlet. the class of the filte interface is defined as the name of the filter declared by the * attribute filtername of the filter component. Optional. The * attribute urlpatterns specifies the URL mode to be filtered, or the attribute value can be declared. (The URL mode to be filtered is a required attribute.) */@ webfilter (filtername = "encodingfilter", urlpatterns = "/*") public class encodingfilter implements filter {@ overridepublic void Init (filterconfig arg0) throws writable {} @ overridepublic void dofilter (servletrequest request, response, filterchain) throws ioexception, response {request. setcharacterencoding ("UTF-8"); response. setcharacterencoding ("UTF-8"); filterchain. dofilter (request, response) ;}@ overridepublic void destroy () {}}
@ Webfilter (filtername = "encodingfilter", urlpatterns = "/*")
@ Webfilter defines a class that implements the javax. servlet. filte interface as a filter component.
The urlpatterns attribute specifies the URL mode to be filtered, or you can use the attribute value to specify.
Multiple Filter modes can be specified @ webfilter (filtername = "encodingfilter", urlpatterns = {"/usermanagerservlet", "/index. jsp "})
Web. xml still does not require any configuration.
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> </Web-app>
The servlet3.0 specification is implemented for developers:
Simplicity
Reduce development workload
Follow Web 2.0 principles