Jsp garbled summary Solution

Source: Internet
Author: User

Data garbled during form submission:
For get method submission:
Find the server. xml in the conf folder under the tomcat installation path, and click <Connector> Add URIEncoding = "UTF-8". See the red text section.
<Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" URIEncoding = "UTF-8"/>
This method is only applicable to the get method.
For the post method:
There are several solutions,
① Add the following code to the jsp page: request. setCharacterEncoding ("UTF-8"); sometimes you need to add respon. setCharacterEncoding ("UTF-8 ");
② Use a filter (recommended ):
Add a new PageEncodingFilter class under the src folder of the project. The Code is as follows:
Chinese filter code:
Package com. util;
 
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; // The class used above
 
// Three methods of Chinese filter, one attribute
Public class PageEncodingFilter implements Filter {
Private String encode;
 
// Initialization. The initial value of encoding comes from "web. xml ".
Public void init (FilterConfig arg0) throws ServletException {
This. encode = arg0.getInitParameter ("encoding ");
 
}
 
// Perform Filtering
Public void doFilter (ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
 
HttpServletRequest request = (HttpServletRequest) arg0; // strong conversion to HttpServletRequest
Request. setCharacterEncoding (encode); // The encode parameter is String. Do not add "Double quotation marks"
Arg2.doFilter (arg0, arg1 );
 
}
 
// Destroy
Public void destroy (){
This. encode = null;
 
}
}
Note:
// Creation steps:
1. Create a new class to implement the interface Filter (the package contains "javax. servlet. Filter", which varies slightly according to the "MyEclips" version );
2. modify the code as above;
3. Open the web. xml file (this file is in webRoot/WEB-INF/web. xml) and configure as follows.

Configuration code:
<! -- Chinese filter initialization parameter settings -->
<Filter>
<Filter-name> PageEncodingFilter </filter-name> <! -- Filter Name -->
<Filter-class> org. ty. struts. util. PageEncodingFilter </filter-class> <! -- Filter file location -->
<Init-param>
<Param-name> encoding </param-name> <! -- Initial Parameter Name, specifying the sequence set used by the jsp page -->
<Param-value> UTF-8 </param-value> <! -- Initial Parameter Value, specifying the Chinese sequence set -->
</Init-param>
</Filter>
<! -- Filter ing path configuration -->
<Filter-mapping>
<Filter-name> PageEncodingFilter </filter-name>
<Url-pattern>/* </url-pattern> <! -- Filter all directories in Chinese -->
</Filter-mapping>
Garbled jsp page:
Add the following code to the jsp code:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
Html files
Add the following code to the head header:
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>

From Rookie's column

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.