jsp中文乱码

hztianye 2011-11-24 11:15:40
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%
String action = request.getParameter("action");
if (action != null&& action.equals("post")) {
request.setCharacterEncoding("gbk");
String title = request.getParameter("title");
System.out.println(title);
String cont = request.getParameter("cont");
cont = cont.replaceAll("\n","<br>");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false);
String sql = "insert into article values(null,0,?,?,?,now(),0)";

PreparedStatement psta = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
psta.setInt(1,-1);
psta.setString(2,title);
psta.setString(3,cont);
psta.executeUpdate();


ResultSet rsKey = psta.getGeneratedKeys();
rsKey.next();
int key = rsKey.getInt(1);
rsKey.close();
String sql1 = "update article set rootid =? where id =?";
PreparedStatement psta1 = conn.prepareStatement(sql1);
psta1.setInt(1,key);
psta1.setInt(2,key);
psta1.executeUpdate();

conn.commit();
conn.setAutoCommit(true);

psta.close();
psta1.close();
conn.close();
response.sendRedirect("ShowArticleTree.jsp");
}

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>

<form action="Post.jsp" method ="post">
<input type="hidden" name="action" value="post">
<table>
<tr>
<td><input type="text" size=80 name="title"/></td>
</tr>
<tr>
<td><textarea cols="80" rows="20" name="cont"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>

发现插入数据库的中文是乱码,怎么回事?
...全文
159 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunlight815 2011-11-25
  • 打赏
  • 举报
回复
把数据库的设置改一下试试
hengxingbao 2011-11-25
  • 打赏
  • 举报
回复
文件编码格式改为GBK
jinjong 2011-11-25
  • 打赏
  • 举报
回复
数据库的问题
upsuccess 2011-11-25
  • 打赏
  • 举报
回复
数据库的编码规则和页面的编码规则不一致,你修改下数据库的编码规则。
xiaxinhuo 2011-11-25
  • 打赏
  • 举报
回复
mysql要设置字符集的
良才2015 2011-11-25
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>

pageEncoding改为UTF-8
romanitc 2011-11-25
  • 打赏
  • 举报
回复
mysql的数据库需要设置字符集 UTF-8
Canace_lemon 2011-11-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huiwenjie168 的回复:]
jdbc:mysql://localhost/bbs?user=root&password=root&useUnicode=true&amp;characterEncoding=UTF-8

这样试试
[/Quote]

mysql的数据库需要设置字符集的 UTF-8
Cactus_hxk 2011-11-25
  • 打赏
  • 举报
回复
jdbc:mysql://localhost/bbs?user=root&password=root&useUnicode=true&characterEncoding=UTF-8

这样试试
war334444 2011-11-25
  • 打赏
  • 举报
回复
虽然不太明白你的执行流程 不过你用的应该也是TOMCAT吧 如果是 尝试一下改变TOMCAT conf目录下的service文件 把 <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="GBK"/>加上 这个 因为我一直是UTF-8编码 所以改的是UTF-8 改过之后就基本没有出现过中文乱码了。。。。。GBK的效果不太知道 你可以试试
chen7788 2011-11-25
  • 打赏
  • 举报
回复
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
楼主的数据库是用的 mysql 的。
mysql数据库插入中文是要设置字符集的。
五哥 2011-11-25
  • 打赏
  • 举报
回复
你到Action中 ,再插入之前 ,看看是不是乱码 ,如果不是 应该是数据库的编码问题 ,需要修改数据库编码 ;

莫非使用mysql ?
hztianye 2011-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sunlight815 的回复:]

把数据库的设置改一下试试
[/Quote]
补充一下,这个jsp文件就是Post.jsp,我已经改过数据库的设置,两个不同的jsp之间跳转,中文字符都没有问题。现在是表单action跳转到自己这个jsp,中文却出问题,不解。
hztianye 2011-11-25
  • 打赏
  • 举报
回复
找到问题了,没把request.setCharacterEncoding("utf-8")放到最前面。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧