Solve the problem of inserting garbled characters into the database on JSP page

Source: Internet
Author: User

When a JSP page uses a form to register a user name, there are two ways to see the table in the database that is garbled in Chinese:

1, JSP page passed in the parameters of Chinese is garbled, is the front desk problem, this time write a filter is good, can write the following filter

  

public class Encodingfilter implements Filter {
String encoding;
private static final String default_character_encoding = "UTF-8";
Public Encodingfilter () {
}

public void Destroy () {
}

public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {
TODO auto-generated Method Stub
Place your code here

Pass the request along the filter chain
try {
if ((encoding!=null) && (Encoding.length () >0)) {
request.setcharacterencoding (encoding);
Verify that you are entering this if statement
System.out.println ("In Filter and Encoding:" +encoding);
}else {
Request.setcharacterencoding (default_character_encoding);
}
} catch (Unsupportedencodingexception e) {
Request.setcharacterencoding (default_character_encoding);
E.printstacktrace ();
}
Chain.dofilter (request, response);
}

public void init (Filterconfig fconfig) throws Servletexception {
Get encoding in the configuration file
encoding = Fconfig.getinitparameter ("encoding");
This filter is initialized when the Web container is initialized.
SYSTEM.OUT.PRINTLN (encoding);
}

}

Note that there is a key place where you use the filter, the form must be submitted in a post, or if it is a get mode, it is still garbled

Or there's a stupid way.

1. String Name=request.getparameter ("name") in the b.jsp, modified to
String Name=new string (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "GB2312");
2. Then the page is displayed, then it is Chinese.

It's also possible.

2, the second method is the database inside the character set, the problem is not a more professional database of friends, generally do not understand the principle, I can only refer to other people's methods

Specifically, the character set of the server and the client's character set is inconsistent, the solution can also Baidu a bit, online there.

Solve the problem of inserting garbled characters into the database on JSP page

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.