Summary of garbled characters in Tomcat JSP Web development

Source: Internet
Author: User

 

1. Static Page garbled

The file encoding is inconsistent with the encoding to be displayed in the browser.

1) Check the original file encoding. open the file in notepad and save it as a file;

2) Add a command to the current page to recommend that the browser use the specified encoding to display the file character content.

<Meta http-equiv = "content-type" content = "text/html; charset = GBK">

3) if the system is XP in English and the East Asian character set is not supported, garbled characters are displayed.

2. garbled JSP pages

1) The page command has a pageEncoding = "GBK", which specifies the encoding of the current page. If it is written

ISO8859-1 can not save Chinese characters;

2) page command contentType = "text/html; charset = ISO8859-1" will also make

The browser gives priority to an encoding.

If JSP code is garbled, it is usually displayed ?, And no matter what encoding you choose for the browser, it cannot be

Show

3. garbled form submission (Tomcat-specific)

1). POST garbled characters

A. First, the encoding of the submitted form by the browser is determined based on the page where the form is located, rather than based on the submitted

The code of the JSP page is determined by the encoding of all pages, such as GBK.

B. The processing method is to set the encoding before obtaining the parameter:

Request. setCharacterEncoding ("GBK ");

C. You can use a filter to solve the problem. Tomcat already has a ready-made one:

Apache-tomcat-5.5.23/webapps/jsp-examples/WEB-INF/classes/filters/SetCharacter

EncodingFilter. java

Web. xml

<Filter>

<Filter-name> Set Character Encoding </filter-name>

<Filter-class> filters. SetCharacterEncodingFilter </filter-class>

<Init-param>

<Param-name> encoding </param-name>

<Param-value> GBK </param-value>

</Init-param>

</Filter>

<Filter-mapping>

<Filter-name> Set Character Encoding </filter-name>

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

</Filter-mapping>

2) garbled GET

Using setCharacterEncoding () cannot solve a BUG in. TOMCAT. Form parameters transmitted in GET Mode

The number is always coded in ISO8859-1. We need to convert it into GBK.

String username = request. getParameter ("username ");

System. out. println (username );

// Transcoding, first obtain the original binary byte array

Byte [] data = username. getBytes ("ISO8859-1 ");

// Construct a new string based on the new character set

Username = new String (data, "GBK ");

Summary:

All pages (except for the final GET garbled problem) are encoded in a uniform (GBK or UTF-8) and will not appear

Garbled problem.

4. Use a filter to code the form parameters at a time to completely solve the garbled problem, namely:

Choose File> New> Class to create a Class named TomcatFormFilter. The package name is

Filters. Then, modify the Class Code as follows:

Package filters;

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

Public class TomcatFormFilter implements Filter {

/**

* Request. java

* Expand HttpServletRequestWrapper without affecting the original functions and providing

Some HttpServletRequest

* Functions in the interface. It can solve Chinese problems in Tomcat's default settings.

You need to replace

* Request object.

*/

Class Request extends HttpServletRequestWrapper

{

Public Request (HttpServletRequest request ){

Super (request );

}

/**

* Convert the internal code of the data read by the form.

* Convert the ISO character to GBK.

*/

Public String toChi (String input ){

Try {

Byte [] bytes = input. getBytes ("ISO8859-1 ");

Return new String (bytes, "GBK ");

}

Catch (Exception ex ){

}

Return null;

}

/**

* Return the HttpServletRequest holded by this object.

*/

Private HttpServletRequest getHttpServletRequest ()

{

Return (HttpServletRequest) super. getRequest ();

}

/**

* Read parameter -- corrected the Chinese problem.

*/

Public String getParameter (String name)

{

Return

ToChi (getHttpServletRequest (). getParameter (name ));

}

/**

* Read parameter list-fixed the Chinese problem.

*/

Public String [] getParameterValues (String name)

{

String values [] =

GetHttpServletRequest (). getParameterValues (name );

If (values! = Null ){

For (int I = 0; I <values. length; I ++ ){

Values [I] = toChi (values [I]);

}

}

Return values;

}

}

Public void destroy (){

}

Public void doFilter (ServletRequest request, ServletResponse

Response,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest httpreq = (HttpServletRequest) request;

If (httpreq. getMethod (). equals ("POST ")){

Request. setCharacterEncoding ("GBK ");

} Else {

Request = new Request (httpreq );

}

Chain. doFilter (request, response );

}

Public void init (FilterConfig filterConfig) throws

ServletException {

}

}

Then modify web. xml to add the Servlet definition:

<Filter>

<Filter-name> TomcatFormFilter </filter-name>

<Filter-class> filters. TomcatFormFilter </filter-class>

</Filter>

<Filter-mapping>

<Filter-name> TomcatFormFilter </filter-name>

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

</Filter-mapping>

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.