Solution to jsp file garbled

Source: Internet
Author: User

Recently, Chinese garbled characters are frequently encountered when debugging Jsp files. The troubleshooting methods are summarized for your reference:
1. garbled characters are displayed on the Jsp file Page. In this case, it is better to handle it. Add the following code to the Page command on the Page:
<% @ Page contentType = "text/html; charset = gb2312" %>

2. When a Jsp page is submitted using a form, the submitted data contains Chinese characters. When we obtain the form data, garbled characters will also appear when it is displayed on other pages, the solution is to add the following line of code when receiving data in the submitted Servlet:
Request. setCharacterEncoding ("gb2312 ");
This is one of the methods. When there are few pages, it is better. If there are many pages, this sentence will be added for every new page I add, so we can use a filter to solve the problem, the specific steps are as follows:
First, write a filter class. The Code is as follows:
Package demo;
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 SetCharacterEncodingFilter implements Filter {
Public void destroy (){
}
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Request. setCharacterEncoding ("gbk ");
// Transfer control to the next Filter
Chain. doFilter (request, response );
}
Public void init (FilterConfig filterConfig) throws ServletException {
}
}
Then add the following code to the web. xml file:
<Filter>
<! -- Name the filter -->
<Filter-name> Set Character Encoding </filter-name>
<! -- The package name of the filter class. Class Name -->
<Filter-class> demo. SetCharacterEncodingFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> Set Character Encoding </filter-name>
<! -- Match all requests -->
<Url-pattern>/* </url-pattern>
</Filter-mapping>
In this way, all requests will be processed by this filter, so that no matter how many pages are added, you can rest assured that you do not always have to consider adding such a code.

3. garbled characters occur during database access. This phenomenon is depressing and complicated to handle.
First, we need to convert the following encoding when storing data into the database: for example, to store strings containing Chinese characters into the database, first:
String s = request. getParameter ("author ");
String author = new String (s. getBytes ("ISO8859_1"), "gb2312 ");
String s = rs. getString ("author ");
String author = new String (s. getBytes ("GB2312"), "ISO8859_1 ");

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.