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