Solution to the Japanese Garbled text in JSP

Source: Internet
Author: User

Note: There may be many reasons for Garbled text, and the results vary in different environments.
I have always been committed to finding a complete and omnipotent solution. I hope you can discuss it together.

Part 1:
About Japanese encoding, the current use of more is shift_js and windows-31J.

Shift_js and windows-31J differences, see: http://blog.csdn.net/luyang1016/archive/2007/06/14/1652469.aspx

For general form submission garbled characters, processing is nothing more than adding three sentences in JSP.

<% @ Page Language = "Java" contenttype = "text/html; charset = Windows-31J" %>
Indicates that JSP encoding is Windows-31J

<Meta http-equiv = "Content-Type" content = "text/html; charset = sjis">
JSP fixed project, is sjis, fixed project since it is fixed characters, generally do not need to use Windows-31J

<% Request. setcharacterencoding ("Windows-31J"); %>
Generally, the project is set in the encapsulated actionservlt extends org. Apache. Struts. Action. actionservlet.

Without the above settings, request. getparameter ("AAA") in the Tomcat environment is garbled if AAA is a Japanese character. (Tomcat's default encoding is IS0-8859-1 ).

The above processing is the simplest processing, and it is said that it can be done through the Web. XML filter configuration for management, how to configure web. XML. At the end of the article, the answer provided by a netizen is provided. The function is similar to the setting in the encapsulated actionservlt extends org. Apache. Struts. Action. actionservlet.

Part 2:
Solution to Garbled text caused by passing Japanese parameters in URLs

(If the format passed in the URL is not specified, it is consistent with the default encoding of apserver. That is, if Tomcat is used, I will pass the URL here, code is ISO-8859-1)

(I don't know what Websphere is by default. I haven't investigated it)

Generally, You Need To urlencoder the URL ※

Example: String param1 = urlencoder. encode ("invalid Parameters", "Windows-31J ");

Note: For encode character sets, urldecoder. Decode (request. getparameter ("param1") must be parsed before use.
However, in struts, the request. getparameter ("param1"); In the next screen is still returned. It may be that the servlet has been converted internally.

If the above solution cannot solve your problem, and you use Tomcat as the apserver,
You can try the following solution to solve the problem. (This method is not recommended, because the best program should not rely on the apserver configuration)

The method is as follows:
In Tomcat's server. XML, add uriencoding = "Windows-31J
<! -- Define a non-SSL coyote HTTP/1.1 Connector on the port specified during installation -->
<Connector Port = "8080" maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
DEBUG = "0" connectiontimeout = "20000"
Disableuploadtimeout = "true" uriencoding = "Windows-31J"/>

PS: In the tomcat5 series, it is feasible to pass Japanese parameters by submitting forms or URLs in the get mode. The post method can be solved by referring to the following filter processing method, or by rewriting the actionservlet method by myself.

※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ ※※※※※※※※※※※※※※※※Public class urlencoderextends object is a utility class for HTML encoding. This class contains a static method to convert string to application/X-WWW-form-urlencoded MIME format. For more information about html encoding, see HTML standards.

Use the following rules for string encoding:

 

The letter and digit characters "A" to "Z", "a" to "Z", and "0" to "9" remain unchanged.
The special characters ".", "-", "*", and "_" remain unchanged.
The space character "" is converted into a plus sign "+ ".
All other characters are insecure. Therefore, some encoding mechanisms are used to convert them into one or more bytes. Each byte is then represented by a three-character string "% XY", where xy is the two hexadecimal representation of the byte. The recommended encoding mechanism is UTF-8. However, for compatibility consideration, if no encoding is specified, the default encoding of the corresponding platform is used.
For example, using the UTF-8 encoding mechanism, the string "the string U @ foo-bar" will be converted to "The + String + % C3 % BC % 40foo-bar" because in the UTF-8, character U is encoded into two bytes, C3 (hexadecimal) and BC (hexadecimal). The character @ is encoded into a byte of 40 (hexadecimal ).
 

※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ ※※※※※※※※※※※※※※※※※Perfect solution.
Make a filter. First, create a file
Filter. characterencodingfilter. Java

Then write the code
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;

Public class characterencodingfilter implements filter {

Public void destroy (){
}
 
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {
Request. setcharacterencoding ("Windows-31J ");
Chain. dofilter (request, response );
}

Public void Init (filterconfig conf) throws servletexception {
}

}
Then modify the configuration file web. xml
<Filter>
<Filter-Name> characterencodingfilter </filter-Name>
<Filter-class> filter. characterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> Windows-31J </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> characterencodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

 

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.