Filter Basics 2 (48)

Source: Internet
Author: User
Tags configuration settings

Filter Chain Filterchain

When there are multiple connectors in a Web project, these filters filter the same URL. This is the formation of the filter chain/.

In the configuration of the filter, there are two configurations: filter,filter-mapping.

Filter-mapping before, then executes first, then executes after.

<filter>

<filter-name>first</filter-name>

<filter-class>cn.itcast.filter.FirstFilter</filter-class>

</filter>

<filter>

<filter-name>second</filter-name>

<filter-class>cn.itcast.filter.SecondFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>second</filter-name>

<url-pattern>/four</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>first</filter-name>

<url-pattern>/four</url-pattern>

</filter-mapping>

Filter Application 1-Set the request encoding

Write a filter that filters all URLs,/*. In the Dofilter method, set the code for request to Utf-8.

In general, this filter is always the first filter to be executed.

It is best to encode by configuration settings. <filter><init-param>, .....

The first step: Implement the filter interface, receive initialization parameters in Dofilter, set the encoding

Package cn.hongxin.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;

Public class Charfilter implements Filter {

Declaring the encoded member variable

Private String encoding;

Public void init (filterconfig config) throws servletexception {

encoding = Config.getinitparameter ("BM");

}

Public void DoFilter (servletrequest request, servletresponse response,

Filterchain chain) throws IOException, servletexception {

request.setcharacterencoding (encoding);

Release, must be released.

Chain.dofilter (request, response);

}

Public void Destroy () {

}

}

Step Two: Configure the above class to Web. xml

<filter>

<filter-name>char</filter-name>

<filter-class>cn.itcast.filter.CharFilter</filter-class>

<init-param>

<!--set the encoding in the configuration file for ease of configuration--

<param-name>BM</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>char</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

For get can handle Chinese

The reuqest is packaged in the Charfilter.

Purpose: Modify the enhanced GetParameter method, if it is a get transcoding.

First step: Declare the wrapper class:

Declaring wrapper classes

class Myrequest extends httpservletrequestwrapper{

Public Myrequest (HttpServletRequest request) {

Super (request);

}

Enhanced Getparamter

@Override

Public String GetParameter (string name) {

String val = Super. GetParameter (name);

if (Super. GetMethod (). Equals ("GET")) {

Try {

val = new String (val.getbytes ("Iso-8859-1"),Super. getcharacterencoding ());

} catch (Unsupportedencodingexception e) {

E.printstacktrace ();

}

}

return Val;

}

}

Step two: In the Dofilter method, declare an instance of the wrapper class

Package cn.hongxin.filter;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

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.HttpServletRequestWrapper;

Public class Charfilter implements Filter {

Declaring the encoded member variable

Private String encoding;

Public void init (filterconfig config) throws servletexception {

encoding = Config.getinitparameter ("BM");

}

Public void DoFilter (servletrequest request, servletresponse response,

Filterchain chain) throws IOException, servletexception {

request.setcharacterencoding (encoding);

Response.setcontenttype ("text/html;charset=" + encoding);

Determine if packaging is required

HttpServletRequest req = (httpservletrequest) request;

if (Req.getmethod (). Equals ("GET")) {

Request = New myrequest (req);

}

Declaring an instance of a wrapper class

Release

Chain.dofilter (request, response);

}

Public void Destroy () {

}

}

Declaring wrapper classes

class Myrequest extends httpservletrequestwrapper {

Public Myrequest (HttpServletRequest request) {

Super (request);

}

Enhanced Getparamter

@Override

Public String GetParameter (string name) {

String val = Super. GetParameter (name);

Try {

val = new String (val.getbytes ("iso-8859-1"),

Super. getcharacterencoding ());

} catch (Unsupportedencodingexception e) {

E.printstacktrace ();

}

return Val;

}

}

Filter Basics 2 (48)

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.