Application encoding filter of JSP Filter

Source: Internet
Author: User

A filter is a Web service component that adds functions to the request and Response Processing of a Web application.
Stages of the filter lifecycle:
Instantiate-> initialize ---> filter ---> destroy
• The Filter must implement the javax. servlet. Filter interface, which contains
-Init (...)
• Parameter type: FilterConfig (initialization parameter object)
-DoFilter (...)
• There are three types of parameters: ServletRequest, ServletResponse, and FilterChain.
-Destroy ()
Create a filter package under SRC and create a filterEncoding class to inherit the filter Interface
 
[Html] package 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;
 
/**
* @ Author Administrator
* Character encoding filter
*/
Public class filterEncoding implements Filter {
Private String requestEncoding = "";
Private String responseEncoding = "";

Public void destroy (){
// TODO Auto-generated method stub
System. out. println ("the character encoding filter is destroyed! ");
}
 
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// System. out. println ("encoding filter running... ");
// Obtain the filter chain
Request. setCharacterEncoding (requestEncoding );
Response. setContentType (responseEncoding );
// Execute the filter chain
Chain. doFilter (request, response );
 
}
 
Public void init (FilterConfig conif) throws ServletException {
// TODO Auto-generated method stub
System. out. println ("character encoding filter initialization! ");
// Obtain data in web. xml during initialization
RequestEncoding = conif. getInitParameter ("requestEncoding ");
ResponseEncoding = conif. getInitParameter ("responseEncoding ");

}
 
}

Package 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;

/**
* @ Author Administrator
* Character encoding filter
*/
Public class filterEncoding implements Filter {
Private String requestEncoding = "";
Private String responseEncoding = "";
 
Public void destroy (){
// TODO Auto-generated method stub
System. out. println ("the character encoding filter is destroyed! ");
}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// System. out. println ("encoding filter running... ");
// Obtain the filter chain
Request. setCharacterEncoding (requestEncoding );
Response. setContentType (responseEncoding );
// Execute the filter chain
Chain. doFilter (request, response );

}

Public void init (FilterConfig conif) throws ServletException {
// TODO Auto-generated method stub
System. out. println ("character encoding filter initialization! ");
// Obtain data in web. xml during initialization
RequestEncoding = conif. getInitParameter ("requestEncoding ");
ResponseEncoding = conif. getInitParameter ("responseEncoding ");

}

}
Configure in web. xml
[Plain] <! -- Encoding filter start -->
<Filter>
<Filter-name> filterEncoding </filter-name>
<Filter-class> filter. filterEncoding </filter-class>
<Init-param>
<Param-name> requestEncoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-name> responseEncoding </param-name>
<Param-value> text/html; charset = UTF-8 </param-value>
</Init-param>
</Filter>

<Filter-mapping>
<Filter-name> filterEncoding </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
<! -- Encoding filter end --

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.