Java Web Filter Learning (2)

Source: Internet
Author: User

This paper mainly explains the basic use of filter, which involves

What is filter?

A filter handles a JSP

Multiple filter Processing a JSP

What is filter?

Filter is a filter under Java that enables the filtering of Java Web Program client and server-side messages, which can be pre-processed before the request is accepted by the server segment, or before the client accepts response. The response is processed.

The use of filter is very flexible, in the "chain" between the client and the server, when needed can be configured between the client and the server, can be removed when not needed. Filter can also set its filter on which pages are sent to or from which pages, which is a cross-cutting programming, pluggable. The general process of filter execution is as follows

To implement the filter-related functions, you need to implement the filter interface under the Javax.servlet.jar package, which has three methods, namely the Init Dofilter,destory method, put the code that needs to perform some functions into the DoFilter method

A filter handles a JSP:

Implement filter to implement the filter interface under the Javax.servlet package , this example is to set all the JSP also character encoding

Package Com.tgb.drp.util.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;/** * Uniform processing character Set using filter * */public class Charsetencodingfilter implements Filter {private String encodestring;        Filter Logoff method @overridepublic void Destroy () {}        //filter the function to be implemented @overridepublic void DoFilter (ServletRequest request , Servletresponse Response,filterchain chain) throws IOException, servletexception {System.out.println ("Begin");// Set the character set request.setcharacterencoding (encodestring);//Continue execution down, if there are other filter continue to call other filter, If not, send the message to the server or client Chain.dofilter (request, response); System.out.println ("End");}        Initialization method @overridepublic void init (Filterconfig filterconfig) throws Servletexception {//encodestring= Filterconfig.getinitparameter ("Encoding");}}

After writing the filter class, you need to set up in the configuration file which request and response to filter processing, this example set to all the JSP page processing, Under the Web-app node of Web. XML, after configuration, you can write a JSP page to test.

    <filter>    <filter-name>CharsetEncodingFilter</filter-name>    <filter-class> com.tgb.drp.util.filter.charsetencodingfilter</filter-class>    <init-param>    <param-name >encoding</param-name>    <param-value>GBK</param-value>    </init-param>    </filter>    <filter-mapping>    <filter-name>CharsetEncodingFilter</filter-name>    <url-pattern>*.jsp</url-pattern>    </filter-mapping>

Multiple filter processing a JSP page

If you want to implement multiple filter to process the same JSP page, for example, you need to set the character encoding, but also need to verify the identity, only after writing a good response to the filter to continue to configure in Web. Xml. We continue with the above example, and then write a verification of the identity of the filter, named Authenticationfilter, the code is as follows

Package Com.tgb.drp.util.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.http.HttpServletRequest; Import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;public class Authenticationfilter implements Filter {@Overridepublic void Destroy () {} @Overridepublic void DoFilter (ServletRequest Request, Servletresponse Response,filterchain chain) throws IOException, servletexception {// Control user access Rights HttpServletRequest req= (httpservletrequest) request; HttpServletResponse res= (httpservletresponse) response; HttpSession session=req.getsession (); if (Session.getattribute ("User_info")!=null) {Chain.dofilter (Request, Response);} Else{res.sendredirect (Req.getcontextpath () + "/error.html");}} @Overridepublic void init (Filterconfig filterconfig) throws servletexception {}}


then proceed with the configuration in the configuration file with the following code

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4" xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <welcome-file-list> <welcome-file>index.html</ welcome-file> </welcome-file-list> <filter> <filter-name>charsetencodingfilter</filter-n Ame> <filter-class>com.tgb.drp.util.filter.CharsetEncodingFilter</filter-class> <init-param     > <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharsetEncodingFilter</filter-name> <url-p attern>*.jsp</url-pattern> </filter-mapping> <filter> <filter-name>authenticationf Ilter</filter-name> <filter-class>com.tgb.drp.util.filter.authenticationfilter</filter-class> </filter> <filter-mapping> <filter-name&gt ; Authenticationfilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <session-config><session-timeout>60</session-timeout></session-config></web-app>

Okay, so you can set up two filter to process a JSP page, and when you start Tomcat and access the response's JSP page, the filter is set to work.

Java Web Filter Learning (2)

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.