[Java Web] Filter

Source: Internet
Author: User
Tags java web

First, brief

A filter is a middle-tier component of the request and response of the server and the client, and the primary user filters the browser's requests and forwards them to the next resource.

The client's request passes through Filterchain before being handed over to the servlet, and the response returned by the server is Filterchain. The filterchain contains multiple filter.

Second, use

Filterconfig is the configuration object of the filter, and the main function is to get the configuration information in the filter.

//Myfilter.java Packagefilter;Importjavax.servlet.*;Importjava.io.IOException;ImportJava.io.PrintWriter; Public classMyfilterImplementsFilter {Privatefilterconfig config; PrivateString addr; @Override Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {String remote=servletrequest.getremoteaddr (); Servletresponse.setcharacterencoding ("GB2312"); PrintWriter out=Servletresponse.getwriter (); if(remote.equals (addr)) {Out.println ("Request rejected:" +remote); } Else{out.println ("Request accepted:" +remote);        Filterchain.dofilter (ServletRequest, servletresponse); }} @Override Public voidInit (filterconfig filterconfig) {config=Filterconfig; Addr= Config.getinitparameter ("addr"); }}//Web. XML<?xml version= "1.0" encoding= "UTF-8"?><web-app> <filter> <filter-name>myfilter</filte R-name> <filter-class>filter. myfilter</filter-class> <init-param> <param-name>addr</param-name> <param-value>127.0.0. 1</param-value> </init-param> </filter> <filter-mapping> <filter-name>my Filter</filter-name> <url-pattern>/*</url-pattern> <!--filter all requests --</filter-mapping></web-app>
View Code

Converts character encodings using filters.

//Myfilter.java Packagefilter;Importjavax.servlet.*;Importjava.io.IOException; Public classMyfilterImplementsFilter {Private Booleanenable; PrivateString encoding; @Override Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {if(enable) {servletrequest.setcharacterencoding (encoding);        servletresponse.setcharacterencoding (encoding);    } filterchain.dofilter (ServletRequest, servletresponse); } @Override Public voidInit (Filterconfig filterconfig) {Enable= Boolean.valueof (Filterconfig.getinitparameter ("Enable")); Encoding= Filterconfig.getinitparameter ("Encoding"); }}//Web. XML<?xml version= "1.0" encoding= "UTF-8"?><web-app> <filter> <filter-name>myfilter</filte R-name> <filter-class>filter. myfilter</filter-class> <init-param> <param-name>enable</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>encoding</param- name> <param-value>GB2312</param-value> </init-param> </filter> <fil Ter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> <!--filter all requests --</filter-mapping></web-app>
View Code

[Java Web] Filter

Related Article

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.