Type 1: When a browser opens a JSP page, Chinese characters on the page are garbled
There are a lot of IDE default JSP files are iso-8859-1 encoding, it is not recognized for Chinese, this problem can be solved with <% @ pageencoding = "UTF-8" %> or "gb2312" or "GBK". I will not talk about the specific differences between them here, you can search Baidu. Here we only talk about the method.
Type 2: Chinese garbled characters appear on the page when the browser calls a Servlet
For example, a servlet method is as follows:
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out. println ("<HTML> ");
Out. println ("Out. println ("<body> ");
Out. println ("I'm not handsome ");
Out. println ("</body> ");
Out. println ("<HTML> ");
Out. Close ();
}
When the browser calls this servlet, "I'm not handsome" is garbled, then the response. setcontenttype ("test/html") is changed to response. setcontenttype ("text/html; charset = gb2312.
Type 3: Submit the POST method to another JSP or servlet for processing and display the submitted content information, Garbled text
For example, the core code of my. jsp is as follows:
<Body>
<Form method = "Post" Action = "display. jsp">
<Input type = "text" name = "username">
<Input type = "Submit" value = "Submit">
</Form>
</Body>
The core code of display. jsp is as follows:
<Body>
The user 'name is: <% = request. getparameter ("username")>
</Body>
Open my. jsp in a browser, enter "brother" in the text box, click the submit button, and hand it over to display. jsp for processing. Then, "brother" is garbled. The solution is to add <% request. setcharacterencoding ("gb2312"); %> In the first line of the display. JSP code.
Filters are used to solve the Chinese Garbled text like request requests, that is, a servlet is used to filter the Garbled text during submission. For example, if the filter is encodingfilter. Java, the Code is as follows:
Package... self-developed
Import... self-developed
Public class encodingfilter implements Filter
{
Private filterconfig config;
Private string encoding = "ISO8859-1 ";
Public void Init (filterconfig config) throws servletexception
{
This. Config = config;
String S = config. getinitparameter ("encoding ");
If (s! = NULL)
{
Encoding = s;
}
}
Public void dofilter (servletrequest request, servletresponse response, fitlerchain chain) throws ioexception, servletexception
{
Request. setcharacterencoding (encoding );
Chain. dofilter (request, response );
}
Public void destroy ()
{
Config = NULL;
}
}
The Web. xml configuration for this filter is as follows:
<Filter>
<Fitler-Name> encodingfilter </filter-Name>
<Filter-class>... indicates the path of the class in the project name. For example, package1.package2. encodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name> // corresponds to encoding in config. getinitparameter ("encoding") of the code.
<Param-value> gb2312 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Fitler-Name> encodingfilter </fitler-Name>
<URL-pattern>/* </url-pattern> // indicates that any request submitted in Chinese will filter the submitted information.
</Filter-mapping>
Type 4: Garbled characters when the get submission form is used to rewrite the URI
As mentioned above, the post method can be processed. However, for the get method, the solution cannot be solved by modifying JSP or servlet. This means only the server configuration file can be modified. The following describes the Chinese garbled characters of this get method:
Similar to the previous example, for example, the core code of my. jsp is as follows:
<Body>
<Form method = "get" Action = "display. jsp">
<Input type = "text" name = "username">
<Input type = "Submit" value = "Submit">
</Form>
</Body>
The core code of display. jsp is as follows:
<Body>
The user 'name is: <% = request. getparameter ("username")>
</Body>
When we enter: http: // localhost: 8080/project name/display. jsp? Username = your brother. At this time, the "brother" shown on the display. jsp page is garbled.
When the form is submitted in the get method. input "elder brother" in JSP and submit it to display. after JSP, the "brother" on the page is garbled, and the "brother" on the domain name address is garbled.
If the get type is garbled, it cannot be the same as processing the post type, for example, in the display. add <% request. setcharacterencoding ("gb2312"); %>, this is useless. Modify % atat_home %/CONF/server. xml configuration information, add uriencoding = "gb2312" to <connector Port = "8080" protocol = "HTTP/1.1" maxthreads = "100" connectiontimeout = "10000" redirectport = "8443"/>", restart tomcat to solve the garbled get problem.