大家好,想请教大虾们一个JSP乱码问题,我的操作系统和mysql都是设置BGK编码,但在index.jsp点击提交留言转到新页面出现乱码,打开数据库数据也是乱码,请大侠们指教:代码如下:1.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>message board</TITLE>
</HEAD>
<BODY>
<TABLE width="1178" align="center" border=1 cellspacing="0"
cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd" height="304">
<TR>
<TD align="center" bgcolor="#88a6dd">
访客留言板
</TD>
</TR>
<TR>
<TD>
<TABLE align="center" border=1 cellspacing="0" cellpadding="0"
bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<form ACTION="add_message.jsp">
<TR>
<TD>
姓名:
</TD>
<TD bgcolor="#88a6dd">
<input type="text" name="name" size=25>
</TD>
</TR>
<TR>
<TD bgcolor="#88a6dd">
E-mail:
</TD>
<TD bgcolor="#88a6dd">
<input type="text" name="mail" size=25>
</TD>
</TR>
<TR>
<TD>
主题:
</TD>
<TD bgcolor="#88a6dd">
<input type="text" name="title" size=25>
</TD>
</TR>
<TR>
<TD valign="top" bgcolor="#88a6dd">
留言:
</TD>
<TD>
<textarea name="content" rows=7 cols=25></textarea>
</TD>
</TR>
<TR>
<TD colspan=3>
<TABLE align="center" width="100%" cellspacing="0"
cellpadding="0" bordercolordark="#88a6dd"
bordercolorlight="#88a6dd">
<TR>
<TD align="center" bgcolor="#88a6dd">
<input type="submit" value="提交留言">
</TD>
<TD align="center">
<a href="./view_message.jsp"><font size=2>查看留言</font>
</a>
</TD>
<TD align="center" bgcolor="#88a6dd">
<input type="reset" value="重新填写">
</TD>
</TR>
</TABLE>
</TD>
</TR>
</from>
</TABLE>
</TD>
</TR>
<br />
</TABLE>
</BODY>
</HTML>
2.add_message.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=gbk" %> 
<% request.setCharacterEncoding("GBK") ;%>
<HTML>
<HEAD>
<TITLE>add message into table</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset= gbk">

</HEAD>
<BODY>
<%
String name = request.getParameter("name");
String mail = request.getParameter("mail");
String title = request.getParameter("title");
String content = request.getParameter("content");
if (name == null)
name = "";
if (title == null)
title = "";
if (content == null)
content = "";
if (mail == null)
mail = "";
if (name.length() == 0)
out.println("留言人姓名不能为空!");
else if (title.length() == 0)
out.println("留言主题不能为空!");
else if (content.length() == 0)
out.println("留言内容不能为空!");
else {
java.util.Date date = new java.util.Date();
String datetime = new Timestamp(date.getTime()).toString(); try { Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost:3306/lyb?user=root&password=123456");
//"jdbc:mysql://localhost:3306/test?user=michael&password=123456&useUnicode=true&characterEncoding=gbk" ;  PreparedStatement stm = con
.prepareStatement("insert into lyb_1 values(?,?,?,?,?)");
stm.setString(1, title);
stm.setString(2, name);
if (mail.length() == 0)
stm.setString(3, null);
else
stm.setString(3, mail);
stm.setString(4, datetime);
stm.setString(5, content);
try {
//stm.executeQuery();
stm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<jsp:forward page="view_message.jsp" />
<%
}
%>
</body>
</html>3.view_message.jsp<%@ page language="java" import="java.util.*" import="java.util.Date" pageEncoding="GBK" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=gbk" %> 
<% request.setCharacterEncoding("GBK") ;%><HTML>
<HEAD>
<TITLE>show the message in the table</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset= gbk">
</HEAD>
<BODY>
<p align="center">
所有访客留言
</p>
<hr>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection
                       ("jdbc:mysql://localhost:3306/lyb?user=root&password=123456&useUnicode=true&characterEncoding=gbk");
Statement stm = con.createStatement();
ResultSet result = stm
.executeQuery("select count(*) from lyb_1");
int lyb_1_count = 0;
if (result.next()) {
lyb_1_count = result.getInt(1);
result.close();
}
if (lyb_1_count > 0) {
result = stm
.executeQuery("select * from lyb_1 order by time desc");
while (result.next()) {
String title = result.getString("title");

String name = result.getString("name");

String mail = result.getString("mail");

Timestamp time = result.getTimestamp("time");

//Date date_time = new Date(time.getTime());
Date date_time=new Date(time.getTime());

Time time_time = new Time(time.getTime());

String content = result.getString("content");
%>
<TABLE width="100%" align="center" border=1 cellspacing="0"
cellpadding="0" bordercolordark="#000000" bordercolorlight="#88a6dd">
<tr>
<td bgcolor="#88a6dd">
<font size=2>主题:</font>
</td>
<td colspan=3><%=title%></td>
</tr>
<tr>
<td bgcolor="#88a6dd">
<font size=2>留言人:</font>
</td>
<td><%=name%></td>
<td bgcolor="#88a6dd">
<font size=2>E-mail:</font>
</td>
<td>
<%
out.println("<a href=mailto:" + mail + ">" + mail
+ "</a>");
%>
</td>
</tr>
<tr>
<td bgcolor="#88a6dd">
<font size=2>留言时间:</font>
</td>
<td colspan=3>
<%
out.println("<font size=2>" + date_time + time_time
+ "</font>");
%>
</td>
</tr>
<tr>
<td align="center">
<%
out.println("(" + lyb_1_count + ")");
%>
</td>
<td colspan=3><%=content%>
</td>
</tr>
</table>
<%
out.println("<hr>");
lyb_1_count--;
}
result.close();
con.close();
} else {
out.println("目前还没有任何留言!");
con.close();
}
} catch (Exception e) {
out.println(e);
}
%>
<p align="center">
<a href="./index.jsp">我要留言</a>
</p>
</body>
</html>

解决方案 »

  1.   

    兄弟
    http://so.csdn.net/bbsSearchResult.aspx?q=jsp+%e4%b9%b1%e7%a0%81&p=0包你解决.
      

  2.   


      你在脚本添加一句如下代码:        request.setCharacterEncoding("GBK");
      

  3.   

    写个过滤器。filter  继承Filter接口 实现放行 然后在WEB配置。
    第二种就是  request.setCharacterEncoding("UTF-8");在你的servlet里面dopost方法里面加上
      

  4.   

    在每个JSP文件中<body>前加上<%request.setCharacterEncoding("gb2312");%>
    或者像四楼所说的写个filter过滤器
      

  5.   

    先看看的mysql默认的是那个编码,可以在用show create database lyb 默认的是什么拉丁文的,如果是在mysql的安装目录下找到my.ini 文件 用编辑器打开,57行改为default-character-set=gbk 所有输入的都为gbk编码了  之后再81行改为default-character-set=utf8 页面请求时都用国际通一编码 一般都不会出现乱码了。在页面上有中文的请求时都不要写上request.setCharacterEncoding("utf-8"); 这是我用的方法,还没有出现过乱码的现象了。
      

  6.   

    网上解决乱码的文章太多了,呵呵,建议g。cn下
      

  7.   

    搞个滤器要是对get的提交方式无效那就要手动 编码了