Servlet3.0-use annotations to define filters)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.