Solve the Problem of request garbled characters in JSP

Source: Internet
Author: User

 

I often see people in the discussion board asking me how to display Chinese garbled characters in JSP. How can I use the Chinese characters entered by the user I obtained through the request be garbled? How can I write Chinese characters to the database, and so on.
In fact, this problem is very simple, it is not Chinese characters, or Japanese, or what other double byte language, we will treat it as a UTF-8.
(1) double-byte text in the request
Well below we come to reality in the whole application using UTF-8 coding work, the reason for choosing UTF-8 is not only for the above reason, we know that Java is based on the UTF-8, so we choose UTF-8 should be correct ^_^
We first put our. Java,. jsp files are saved with UTF-8 encoding, if the previous did not use the UTF-8 to save it does not matter, but it is recommended to Write later to save with UTF-8.
And in. in JSP write: <% @ page contenttype = "text/html; charset = UTF-8" %> instead of <% @ page contenttype = "text/html; charset = UTF-8" %>
Add the following section in Web. xml:
<Web-app>
...
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> com. redv. Projects. eduadmin. util. Filters. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
...
</Web-app>

The code for com. redv. Projects. eduadmin. util. Filters. setcharacterencodingfilter is as follows:

Package com. redv. Projects. eduadmin. util. filters;

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. unavailableexception;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Public class setcharacterencodingfilter
Implements filter {

Protected string encoding = NULL;

Protected filterconfig = NULL;

Protected Boolean ignore = true;

Public void destroy (){

This. Encoding = NULL;
This. filterconfig = NULL;

}

Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {

// Conditionally select and set the character encoding to be used
If (ignore | (request. getcharacterencoding () = NULL )){
String encoding = selectencoding (request );
If (encoding! = NULL ){

Request. setcharacterencoding (encoding );
// This sentence is at work. Haha, it: overrides the name of the character encoding used in
The body of this request. This method must be called prior to reading
Request Parameters or reading input using getreader ().
}
}

// Pass control on to the next Filter
Chain. dofilter (request, response );

}

Public void Init (filterconfig) throws servletexception {

This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL ){
This. Ignore = true;
}
Else if (value. inclusignorecase ("true ")){
This. Ignore = true;
}
Else if (value. inclusignorecase ("yes ")){
This. Ignore = true;
}
Else {
This. Ignore = false;
}

}

Protected string selectencoding (servletrequest request ){

Return (this. Encoding );

}

}

This
Sample, our request is encoded in UTT-8, In the JSP program can use: request. getparameter ("mykey") to directly get
To the UTF-8 encoded string, and does not need to be like this: New
String (request. getparameter ("mykey"). getbytes ("ISO-8859-1 "),
"GBK. Http://www.devdao.com/

Two-byte text http://www.upas.org/java/DatabaseEncodingProblemSolution/ for database processing
Another
The other is writing data to the database. We know that we can use this URL to process Chinese character encoding requests when using MySQL.
Question: JDBC: mysql: // localhost: 3306/upas? Useunicode = true &
Characterencoding = gb2312,
So what should we do if we cannot solve it like MySQL? Do we write this every time:
Import java. SQL .*;

Class. forname ("org. gjt. Mm. MySQL. Driver ");
Connection con = NULL;
Preparedstatement pstmt = NULL;
Resultset rs = NULL;
Try {
Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/test", "root ","");
Pstmt = con. preparestatement ("select F3, F4 from tbl1 where F1 =? And F2 =? ");
Pstmt. setstring (1, new string (f1.getbytes ("GBK"), "ISO-8859-1 ");
Pstmt. setstring (2, new string (f2.getbytes ("GBK"), "ISO-8859-1 ");
Rs = pstmt.exe cutequery ();
String F3, F4;
While (Rs. Next ()){
F3 = new string (Rs. getstring (1). getbytes ("ISO-8859-1"), "GBK ");
F4 = new string (Rs. getstring (2). getbytes ("ISO-8859-1"), "GBK ");
}
}
Finally {
// Close Resouces
...
}

In fact, we can write like this:
Import java. SQL .*;
Import com. redv. SQL. encoding .*;

Class. forname ("org. gjt. Mm. MySQL. Driver ");
Connection con = NULL;
Preparedstatement pstmt = NULL;
Resultset rs = NULL;
Try {
Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/test", "root ","");
// Take over database connection instance
Boolean coding = true;
Encodingconnection codingconnection = new encodingconnection (con, coding, "ISO-8859-1", "GBK ");
// Obtain the database connection instance after taking over, and then use con directly to re-package the instance through encodingconnection
Con = codingconnection. getconnection ();
Pstmt = con. preparestatement ("select F3, F4 from tbl1 where F1 =? And F2 =? ");
Pstmt. setstring (1, F1 );
Pstmt. setstring (2, F2 );
Rs = pstmt.exe cutequery ();
String F3, F4;
While (Rs. Next ()){
F3 = Rs. getstring (1 );
F4 = Rs. getstring (2 );
}
}
Finally {
// Close Resouces
...
}

View
Let's see how it works. We just need to modify it a little while getting the database connection. We can even save it as a parameter in
In properties, change the Boolean value of coding to determine whether to use automatic encoding conversion. We can often use a database class to encapsulate the Section for obtaining database connections.
Getconnection, so that we can
Obtain the database connection from javax. SQL. datasource. At this time, we only need to modify our database class, instead of searching for all
Rs. setstring (),
Rs. getstring () is added to our encoding conversion code. Even when we use the con. createstatment () statement
When there are Chinese characters or other double-byte characters, there is no problem:
Select name, gender from student table where class like '% Computer %'

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.