servlet中表单提交中文数据显示乱码的问题

来源:百度知道 编辑:UC知道 时间:2024/06/09 04:31:20
网页显示正确,但提交到servlet中显示乱码,代码中已经加入了 request.setCharacterEncoding("GB2312");
response.setCharacterEncoding("GB2312");
还有
response.setContentType("text/html; charset=GB2312");
我用的是tomcat5.0,在server.xml也加入了URIEncoding='GB2312'.
但还是解决不了,以后是部分代码
String sql = "insert into guestbook (id, name, phone, email, title, content, time) values (?, ?, ?, ?, ?, ?, ?)";
String name = request.getParameter("name");
String title = request.getParameter("title");
int result = 0;

request.setCharacterEncoding("GB2312");
response.setCharacterEncoding("GB2312");

response.setContentType("text/html; charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>add message</title></head>");
out.prin

Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost/guestbook?user=root&password=123");

改为
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost/guestbook?&user=root&password=123&useUnicode=true&characterEncoding=GB2312");

如果仅仅是少数怪汉字显示不出,把GB2312改成GB18030或GBK试试呢?

网页中:<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK" %>