Java Learning Filter

Source: Internet
Author: User

in the process of learning JSP and Java, there are always garbled cases. So we have to add this kind of information often:
    <%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%>

The filter is added later. Let's look at a picture:


This is the application that joins the filter in the middle of the Web client, server, and JSP pages.

Filter life cycle:

       1 , load when starting server filter Span lang= "en-US" style= "Font-family:calibri" >init () method to initialize the instance ( filter servlet

2 , only methods are called on each request DoFilter () for processing

3 , stopping the server when calling Destroy () method, destroying the instance; (destroying an instance later than Servlet )

Let's take a look at the filter code for the background wrapper:

Package Com.bjpowernode.drp.util.filter;import java.io.ioexception;//These jar packages are some of the features of filter. Import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import javax.servlet.servletresponse;/** *  Use the uniform processing character set * @author XZP * */public class Charsetencodingfilter implements Filter {private String endcoding; @Overridepublic  void Destroy () {//TODO auto-generated method stub} @Overridepublic void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {System.out.println ("Charsetencodingfilter---> >>begin ");//Set CharSet request.setcharacterencoding (endcoding);//Continue to execute Chain.dofilter (request,response); System.out.println ("Charsetencodingfilter--->>>end");} @Overridepublic void init (Filterconfig filterconfig) throws Servletexception {this.endcoding= Filterconfig.getinitparameter ("Encoding");}}
Finally, XML configuration is required:

<?xml version= "1.0" encoding= "UTF-8"? ><filter><filter-name>charsetencodingfilter</ Filter-name><filter-class>com.bjpowernode.drp.util.filter.charsetencodingfilter</filter-class> <init-param><param-name>encoding</param-name><param-value>gbk</param-value></ init-param><filter-mapping>     <filter-name>CharsetEncodingFilter</filter-name>        < Url-pattern>*.jsp</url-pattern> </filter-mapping></filter>
Summarize:

1.Filter can be seen as a crosscutting technique.

2.Filter is embodied in the design pattern: "Responsibility chain Mode", "Template method Mode".

3.Filter provides a declarative service that intercepts request and response requests. And only works on the POST request.




Java Learning 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.