Servlet Filter--filter

Source: Internet
Author: User

First, the definition:

    • is a special servlet, can be used to filter the request, multiple filters can form a filter chain;
    • The request can be intercepted and processed with a filter before the request is sent to the servlet, and the response can be processed with a filter after the servlet has finished working and before the response is sent back to the customer.
    • Implementation of the Javax.servlet.Filter interface inside the Init,dofilter, destroy and other abstract methods, is the life cycle of the filter.

    • How the Filter works:

ii. configuration declarations in Web. XML
    • According to the servlet2.3 specification, filter execution is carried out in accordance with filter-mapping order;
    • The first code: declaring the filter name; the second code: mapping filters to filter mappings for the Web resource declaration corresponding URL pattern you want to filter
<filter>               <filter-name>  file name </filter-name>               <filter-class> package name. File name </ Filter-class>               <init-param>{  optional}                     <param-name> variable name  </param-name >                      <param-value>  variables </param-value>                 </init-param>      </filter>
<filter-mapping>     <filter-name> file name </filter-name>     <url-pattern> /* </url-pattern></filter-mapping>
    • If you are connected to a servlet, insert the following code:
<servlet>      <servlet-name>Demo1</servlet-name>      <servlet-class>servlet. Demo1</servlet-class> </servlet>
Iii. Examples of contact with servlets-solving Chinese problems1. Create a new Javaweb project TESTFL, create a characterencoderfilter.java--under the filter package
 Public classCharacterencoderfilterImplementsfilter{PrivateFilterconfig filterconfig=NULL;  Public voiddestroy () {} Public voidDoFilter (servletrequest req, Servletresponse resp, filterchain chain)throwsIOException, servletexception {String charset=filterconfig.getinitparameter ("CharSet");           Req.setcharacterencoding (CharSet);           Resp.setcharacterencoding (CharSet); Resp.setcontenttype ("Text/html;charset=" +charset);     Chain.dofilter (req, resp); }   Public voidInit (Filterconfig filterconfig)throwsservletexception { This. filterconfig=Filterconfig; } } 

2, demo2.java--under the servlet package

 Public classDemo2extendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request, response); }  Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String username=request.getparameter ("username");          SYSTEM.OUT.PRINTLN (username); Response.getwriter (). Write (Chinese); }}

3. In Web. XML, the filter file name, variable name, and servlet file are declared again:

(Note that the/* is the URL does not need to write the file name, just write http://localhost:8080/TestFL/,

if/abc/*, write as http://localhost:8080/TestFL/abc/to run)

 <filter> <filter-name>CharacterEncoderFilter</filter-name> <filter-class>filter.          Characterencoderfilter</filter-class> <init-param> <param-name>charset</param-name>        <param-value>utf-8</param-value> </init-param> </filter><filter-mapping> <filter-name>CharacterEncoderFilter</filter-name> <url-pattern>  /*  </url-pattern></filter-mapping><servlet> < Servlet-name>demo1</servlet-name> <servlet-class>servlet. Demo1</servlet-class></servlet> <servlet-mapping> <servlet-name>demo1</servlet-name > <url-pattern>/Demo1</url-pattern></servlet-mapping>  

3, design a simple form in the JSP file:(remember to change the pageencoding to Utf-8)

<formAction= "/testfl/demo2"Method= "POST">User name:<inputname= "username"/>         <inputtype= "Submit"value= "Submit" /> </form>

4, open the Tomcat service, redeploy deployment files, run http://localhost:8080/TestFL/on the browser, after submission: (All page requests are filtered through the filter)

Servlet Filter--filter

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.