setting of JSP character filter __js

Source: Internet
Author: User

This article describes the filter to set the problem of character encoding, by writing a servlet and configuration web.xml to achieve
This eliminates the need for the code of Pride characters in each JSP page, and the value needs to be encoded in the Web.xml configuration.

Web.xml configuration content is as follows:

<!--character filter;
    <filter>
        < Filter-name>encodefilter</filter-name>
        <filter-class
            Test.servlet.CharacterEncodingFilter
        </filter-class>
         <init-param>
            <param-name >encoding</param-name>
            < Param-value>utf-8</param-value>
        </init-param>
      </filter>

<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

The contents of the filter are as follows:

Package test.servlet;


Import javax.servlet.*;
Import java.io.IOException;

public class Characterencodingfilter implements Filter {


protected String encoding = "UTF-8";

protected Filterconfig filterconfig = null;

public void init (Filterconfig filterconfig) throws Servletexception {
This.filterconfig = Filterconfig;
this.encoding = Filterconfig.getinitparameter ("encoding");
}

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
Conditionally select and set the character encoding to be used
if (request.getcharacterencoding () = null) {
String encoding = selectencoding (request);
if (encoding!= null) {
request.setcharacterencoding (encoding);
}
}
Pass control in to the next filter
Chain.dofilter (request, response);
}

Protected String selectencoding (ServletRequest request) {
return (this.encoding);
}

public void Destroy () {
this.encoding = null;
This.filterconfig = null;
}

}

Go: Character Page Setup mode:

1. pageencoding:<%@ page pageencoding= "UTF-8"%>

JSP page encoding: The encoding of the JSP file itself

2. ContentType: <%@ page contenttype= "text/html; Charset=utf-8 "%>

Web page display encoding: The encoding that the output stream of the JSP displays in the browser

3. html page charset:<meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 ">

Web page input encoding: input box input font encoding

4. SetCharacterEncoding:request.setCharacterEncoding (), response.setcharacterencoding ()

Request flow for Web server input: Request data for Web server's corresponding browser

5. SetContentType:response.setContentType ()

Response flow for Web server output: Output data for Web-server-appropriate browsers

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/XinVSYuan/archive/2009/02/05/3864853.aspx

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.