萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> jsp編程 >> JSP+ MySQL中文亂碼問題post提交亂碼解決方案

JSP+ MySQL中文亂碼問題post提交亂碼解決方案

當提交的數據中含有中文(比如變量姓名的值為中文)時,mysql_insert.jsp頁面上顯示新增的那條記錄中的相應中文(姓名的值)亂碼  

寫了兩個jsp頁面index.jsp和mysql_insert.jsp。數據處理流程為:在浏覽器(chrome)上訪問index.jsp後在其表 單上輸入數據,提交至mysql_insert.jsp,mysql_insert.jsp首先將接收到的數據按變量存入MySQL的html_db數據 庫的person_tb中(該表原有部分數據),然後mysql_insert.jsp再拿出該表中所有數據顯示在mysql_insert.jsp頁面 上。

現在發現,當提交的數據中含有中文(比如變量姓名的值為中文)時,mysql_insert.jsp頁面上顯示新增的那條記錄中的相應中文(姓名的值)亂碼,其他數據都顯示正常,查看數據庫,發現也是相應的含有中文的變量值(姓名的值)亂碼。亂碼情況如下圖:
JSP+ MySQL中文亂碼問題post提交亂碼解決方案 三聯 
index.jsp 中第一行有語句:<%@ page contentType="text/html;charset=gb2312"%> ,在浏覽器(chrome)中訪問此頁面時無亂碼(主要指中文亂碼,英文亂碼現象還沒遇見過)。(試過將gb2312換成utf-8,訪問後中文亂碼)
mysql_insert.jsp中第一行有語句:<%@page language="java" pageEncoding="UTF-8"%>,在浏覽器中直接訪問此頁面時無亂碼。
mysql_insert.jsp頁面的代碼如下:

復制代碼 代碼如下:
<%@page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<HTML>
<HEAD>
<TITLE>add message into table </TITLE>
</HEAD>
<BODY>
<%
String id=request.getParameter("id"); //從表單獲得
String name=request.getParameter("name"); //從表單獲得
String sex=request.getParameter("sex"); //從表單獲得
String age=request.getParameter("age"); //從表單獲得
try
{
/** 連接數據庫參數 **/
String driverName = "com.mysql.jdbc.Driver"; //驅動名稱
String DBUser = "root"; //mysql用戶名
String DBPasswd = "123456"; //mysql密碼
String DBName = "html_db"; //數據庫名

String connUrl = "jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPasswd;
Class.forName(driverName).newInstance();
Connection conn = DriverManager.getConnection(connUrl);
Statement stmt = conn.createStatement();
stmt.executeQuery("SET NAMES UTF8");
String insert_sql = "insert into person_tb values('" + id + "','" + name + "','" + sex + "','" + age + "')";
String query_sql = "select * from person_tb";

try {
stmt.execute(insert_sql);
}catch(Exception e) {
e.printStackTrace();
}
try {
ResultSet rs = stmt.executeQuery(query_sql);
while(rs.next()) {
%>
ID:<%=rs.getString("id")%> </br>
姓名:<%=rs.getString("name")%> </br>
性別:<%=rs.getString("sex")%> </br>
年齡:<%=rs.getString("age")%> </br> </br>
<%
}
}catch(Exception e) {
e.printStackTrace();
}
//rs.close();
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>


我的數據庫設置的是全部使用UTF-8編碼,如下圖:
 
我的虛擬目錄下的web.xml內容如下:
 
tomcat/conf目錄下的server.xml文件的內容如下:

復制代碼 代碼如下:
<U><?xml version='1.0' encoding='utf-8'?></U>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>


tomcat/conf目錄下web.xml文件的主要內容如下:

復制代碼 代碼如下:
<U><?xml version="1.0" encoding="UTF-8"?></U>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<ur
copyright © 萬盛學電腦網 all rights reserved