The processing method of Chinese garbled in JSP tutorial
In the JSP code, the following frequently need to involve Chinese processing:
1. Attach Chinese parameters to the URL. Here Chinese parameters can usually be read directly, for example: <%= request.getparameter ("Showword")%>
2. Read the Chinese value of the HTML form submission in the JSWDK. This needs to be encoded, the more concise writing is:
String Name1=new string (Request.getparameter ("user_id"). GetBytes ("Iso8859_1").
In addition, with the support of JDK 1.3, you do not need to join the <%@ page contenttype= "text/html;charset=gb2312"%>, but under JDK 1.2.2, even if the above two methods are used at the same time is very unstable. But in the resin platform, the situation is better, as long as in the first line of the page to add: <%@ page contenttype= "text/html;charset=gb2312"%> can correctly handle Chinese, if you add code is wrong.
3. In JSWDK the session contains Chinese, if the value read out from the form is encoded to display correctly, but the Chinese value is not directly given, and the resin platform is very good.
4. Add code options when compiling the servlet and JSP. Use java-encoding iso8859-1 Myservlet.java when compiling the servlet; in the JSP zone configuration file, modify the compilation parameters to: Compiler=builtin-javac-encoding Iso8859-1. After using this method, you will not need to make any other changes to the normal display of Chinese.
In addition, the popular relational database tutorial system supports database encoding, which means that when you create a database, you can specify its own set of character sets, and the database data is stored in the specified encoding form. When an application accesses data, there are encoding conversions at both the entrance and exit points. For Chinese data, the database character encoding should be set to ensure the integrity of the data. gb2312, GBK, utf-8 are optional database encoding, you can also choose Iso8859-1 (8-bit), but increase the complexity of the programming, ISO8859-1 is not the recommended database encoding. In Jsp/servlet programming, you can use the management function provided by the database management system to check whether the Chinese data is correct.
Processing Method Instances
Here are two specific examples of Chinese garbled solution, the reader will be careful study may be harvested.
1. Common method of character conversion
Transfer the values in the form to the database and then remove them all into "? "。 Form uses the post to submit the data, and the statement is used in the code: string St=new (Request.getparameter ("name"). GetBytes ("Iso8859_1")), and charset=gb2312 is also declared.
To handle the Chinese parameters passed in form, you should add the following code to the JSP, define a GETSTR class that specifically addresses the problem, and then convert to the received parameters:
String Keyword1=request.getparameter ("Keyword1");
Keyword1=getstr (KEYWORD1);
This will solve the problem, the code is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%!
public string Getstr (String str) {
Try{string Temp_p=str;
Byte[] Temp_t=temp_p.getbytes ("iso8859-1");
String Temp=new string (temp_t);
return temp;
}
catch (Exception e) {}
return "NULL";
}
%>
<%--http://www.cndes.com Test--%>
<% string keyword= "Creative Network Technology Center welcomes your arrival";
String Keyword1=request.getparameter ("Keyword1");
Keyword1=getstr (KEYWORD1);
Out.print (keyword);
Out.print (KEYWORD1);
%>
Look at a page and database Chinese garbled processing method
post.jsp
<form method= "POST" action= "addmsg.jsp" >
Name:<input type= "text" name= "name" >
<input type= "Submit" value= "Submit" >
</form>
<title> use JDBC to access database directly </title>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<%
String Name=new string (Request.getparameter ("name"). GetBytes ("Iso8859_1"), "GBK");
String name = Request.getparameter ("name");
OUT.PRINTLN (name);
Class.forName ("Org.gjt.mm.mysql tutorial. Driver");
Connection
Con=drivermanager.getconnection ("Jdbc:mysql://localhost/bbs?useunicode=true&characterencoding=utf-8", "root") "," 123456 ");
Statement stmt=con.createstatement ();
String sql= "insert into book (name) VALUES (' +name+ ')";
int value;
Value=stmt.executeupdate (SQL);
if (value>0)
{
Out.println ("OK");
}
Else
{
Out.println ("no");
}
Stmt.close ();
Con.close ();
%>
</table>
</body>