JSP页面中文乱码

Seika 2012-12-19 01:20:33
JSP页面读取mysql里的数据,显示出来的是乱码。

在数据表直接查看则正常。以下是某个页面代码,请大大解救。

尝试了多种方法还是乱码。

使用的是tomcat7

<%@ page contentType="text/html; charset=gb2312" language="java"  pageEncoding="GB2312"%>
<%@ page import="com.dao.BorrowDAO"%>
<%@ page import="com.actionForm.BorrowForm"%>
<%@ page import="java.util.*"%>
<%

%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head>
<title>图书馆管理系统</title>
<link href="css/style.css" rel="stylesheet">
</head>
<body onLoad="clockon(bgclock)">
<%@include file="banner.jsp"%>
<%@include file="navigation.jsp"%>
<%
BorrowDAO borrowDAO = new BorrowDAO();
Collection coll_book = (Collection) borrowDAO.bookBorrowSort();
%>

<table width="778" height="510" border="0" align="center"
cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"
class="tableBorder_gray">
<tr>
<td align="center" valign="top" style="padding:5px;"><table
width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="20" align="right" valign="middle" class="word_orange">当前位置:首页
>>> </td>
</tr>
<tr>
<td valign="top"><table width="100%" border="0"
cellspacing="0" cellpadding="0">
<tr>
<td height="57" background="images/main_booksort.gif"> </td>
</tr>
<tr>
<td height="72" valign="top"><table width="100%"
height="63" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="2%" rowspan="2"> </td>
<td width="96%" align="center" valign="top"><table
width="100%" border="1" cellpadding="0" cellspacing="0"
bordercolor="#FFFFFF" bordercolordark="#B7B6B6"
bordercolorlight="#FFFFFF">
<tr align="center">
<td width="5%" height="25">排名</td>
<td width="10%">图书条形码</td>
<td width="24%">图书名称</td>
<td width="10%">图书类型</td>
<td width="10%">书架</td>
<td width="14%">出版社</td>
<td width="11%">作者</td>
<td>价钱</td>
<td>借阅次数</td>
</tr>
<%
if (coll_book != null && !coll_book.isEmpty()) {
Iterator it_book = coll_book.iterator();
int i = 1;
int degree = 0;
String bookname = "";
String typename = "";
String barcode_book = "";
String bookcase = "";
String pub = "";
String author = "";
String translator = "";
Float price = new Float(0);
while (it_book.hasNext() && i < 6) {
BorrowForm borrowForm = (BorrowForm) it_book.next();
bookname = chStr.toChinese(borrowForm.getBookName());
barcode_book = borrowForm.getBookBarcode();
typename = chStr.toChinese(borrowForm.getBookType());
degree = borrowForm.getDegree();
bookcase = chStr.toChinese(borrowForm.getBookcaseName());
pub = chStr.toChinese(borrowForm.getPubName());
author = chStr.toChinese(borrowForm.getAuthor());
price = borrowForm.getPrice();
%>
<tr>
<td height="25" align="center"><%=i%></td>
<td style="padding:5px;"> <%=barcode_book%></td>
<td style="padding:5px;"><%=bookname%></td>
<td style="padding:5px;"><%=typename%></td>
<td align="center"> <%=bookcase%></td>
<td align="center"> <%=pub%></td>
<td width="11%" align="center"><%=author%></td>
<td width="8%" align="center"><%=price%></td>
<td width="8%" align="center"><%=degree%></td>
</tr>
<%
i++;
}
}
%>
</table>
</td>
<td width="2%" rowspan="2"> </td>
</tr>
<tr>
<td height="30" align="right" valign="middle"><a
href=borrow.do?action=bookBorrowSort><img
src="images/more.GIF" width="50" height="20" border="0"> </a>
</td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%@ include file="copyright.jsp"%>
</body>
</html>
...全文
289 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
零度六度 2015-04-27
  • 打赏
  • 举报
回复
如果数据库和前端单独看正常的话,一般就是编码不一致导致的了,你看下你的数据库是什么编码格式, show VARIABLES like '%character%'; 看下是否和你的前端编码一致
qingtianrx 2015-04-27
  • 打赏
  • 举报
回复
我的之前也是一样情况,数据库和前台页面都改为UTF-8问题解决
SexyCode 2015-04-26
  • 打赏
  • 举报
回复
设置一个拦截器,对所有编码进行转换,转成你想要的
Journey_Through 2015-04-26
  • 打赏
  • 举报
回复
chStr.toChinese()去掉 页面设置UTF-8 我就是这样做的 数据库也是UTF-8
雍寇德 2012-12-19
  • 打赏
  • 举报
回复
引用 9 楼 hucp2007 的回复:
引用 8 楼 pn12685 的回复:数据库默认utf-8的,也把数据库连接更改为 jdbc:mysql://127.0.0.1:3306/elibrary?user=root&amp;password=123456&amp;useUnicode=true&amp;characterEncoding=gb2312 了。 乱码在于我的jsp……
LZ最好先试试 其实应该做个过滤器的
笑莫问 2012-12-19
  • 打赏
  • 举报
回复
引用 8 楼 pn12685 的回复:
数据库默认utf-8的,也把数据库连接更改为 jdbc:mysql://127.0.0.1:3306/elibrary?user=root&password=123456&useUnicode=true&characterEncoding=gb2312 了。 乱码在于我的jsp页面读取数据库的数据,中文字都是乱码。
1、你数据库用过的utf-8编码,代码、页面却都是gb2312,很不规范,说不好就是这个产生的乱码,建议同意编码, 2、看下乱码部分是取出来的java对象中文乱码,还是页面展现乱码
Seika 2012-12-19
  • 打赏
  • 举报
回复
数据库默认utf-8的,也把数据库连接更改为 jdbc:mysql://127.0.0.1:3306/elibrary?user=root&password=123456&useUnicode=true&characterEncoding=gb2312 了。 乱码在于我的jsp页面读取数据库的数据,中文字都是乱码。
fatbox123 2012-12-19
  • 打赏
  • 举报
回复
你得先确定是哪边出现了乱码,jsp页面 还是数据库 自己先调试下 然后在改!
csd_luoye 2012-12-19
  • 打赏
  • 举报
回复
引用 5 楼 ling_xuan 的回复:
先看看mysql的配置文件mysql.in中的字符集是不是gbk或者gb2312,再看看web.xml文件中的字符编码设置是不是gbk或gb2312
然后看看你的mysql配置的url xml中写成jdbc:mysql://localhost/ishow?useUnicode=true&characterEncoding=gb2312 properties中写成jdbc:mysql://localhost/ishow?useUnicode=true&characterEncoding=gb2312
翎轩宏 2012-12-19
  • 打赏
  • 举报
回复
先看看mysql的配置文件mysql.in中的字符集是不是gbk或者gb2312,再看看web.xml文件中的字符编码设置是不是gbk或gb2312
  • 打赏
  • 举报
回复
在body中加入<%request.setCharacterEncoding("GBK")%>,试试
beyondSelf 2012-12-19
  • 打赏
  • 举报
回复
如果记得没错的话,mysql有个字符集设置,将里面设成gb2312试试!
ydehh 2012-12-19
  • 打赏
  • 举报
回复
陪个过滤器。。把请求都变成gbk的试试
leandzgc 2012-12-19
  • 打赏
  • 举报
回复
在这个jsp文件上右键,然后属性还是首选项忘记了,source,然后有个编码方式,选择其它,然后在里面先找到UTF-8,保存后在你的jsp页面手动添加一些中文试试,不行再换GBK,而且你页面的Encoding等字符集跟source里面设置一致,试试。
爱上走路 2012-12-19
  • 打赏
  • 举报
回复
中文乱码的情况我也碰到过很多,这个问题需要系统的总结下。 一。数据库和Servlet编码要一致。 二。可以尝试修改数据库配置文件和服务器配置文件。 一般我是如下解决的,不过URL传中文确实很容易乱码 1.我用的Mysql数据库,修改配置文件为utf8编码即可。 2.我在JDBC底层改了数据库url,在数据库url后面加上?URLEncoding=UTF8

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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