如何解决 jsp网页之间 传递中文乱码 的问题

jonathan7777 2011-06-06 06:55:03
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<!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=UTF-8">
<title>编辑图书信息</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 36px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<% String strId = request.getParameter("id");//获取传过来的参数(网络上传输的只能是字符串)
Class.forName("com.mysql.jdbc.Driver");//java的反射
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bk","root","123");
PreparedStatement ps =con.prepareStatement("select * from t_book where id=?");
ps.setInt(1,Integer.parseInt(strId));

ResultSet rs=ps.executeQuery();
if(rs.next()){
%>
<form name="form1" method="post" action="bookSaveEdit.jsp">
<!-- 关键语句 -->
<input type="hidden" name="id" value="<%=rs.getInt("id") %>">
<div align="center" class="STYLE1">编辑图书信息</div>
<table width="300" height="120" border="1" align="center">

<tr>
<td width="74">书名:</td>
<td width="210"><input name="name" type="text" id="name" value="<%=rs.getString("name") %>"></td>
</tr>
<tr>
<td>出版社:</td>
<td><input name="publish" type="text" id="publish" value="<%=rs.getString("publish") %>"></td>
</tr>
<tr>
<td>价格:</td>
<td><input name="price" type="text" id="price" value="<%=rs.getFloat("price") %>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="确定修改">
<input type="reset" name="Submit2" value="取消"></td>
</tr>
</table>
</form>
<%
}
con.close();
%>
</body>
</html>






然后传到




<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<!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=UTF-8">
<title>保存编辑</title>
</head>
<body>
<%
String strId = request.getParameter("id");
String name=request.getParameter("name");
String pub=new String(request.getParameter("publish").getBytes("ISO-8859-1"), "gb2312");
String strPr=request.getParameter("price");
float price=Float.parseFloat(strPr);//数据转换:字符串(数字)转整型//float型
int id=Integer.parseInt(strId);
//利用jdbc完成数据库插入操作
Class.forName("com.mysql.jdbc.Driver");//java的反射
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bk","root","123");
PreparedStatement ps =con.prepareStatement("update t_book set name=?,publish=?,price=? where id=?");
ps.setString(1,name);
ps.setString(2,pub);
ps.setFloat(3,price);
ps.setInt(4,id);
ps.execute();
con.close();
response.sendRedirect("bookList.jsp");
%>
</body>
</html>


用了这句String pub=new String(request.getParameter("publish").getBytes("ISO-8859-1"), "gb2312");在网页间传值的时候才不是乱码,从而写到数据库也不是乱码,否则经过网页这样一传递,写入数据库后自然也就是乱码了,请问如果不用String pub=new String(request.getParameter("publish").getBytes("ISO-8859-1"), "gb2312");这句话的话,要想在网页间传递中文不至于变成乱码我该怎么办?



...全文
396 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
jonathan7777 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 liuyuhua0066 的回复:]
乱码问题只要用心总结一次就可以一劳永逸了,为什么不肯自己去总结!
[/Quote]总结得花时间,还需要经验不是?这不还想请教一下高手迅速解决问题吗?
liuyuhua0066 2011-06-07
  • 打赏
  • 举报
回复
乱码问题只要用心总结一次就可以一劳永逸了,为什么不肯自己去总结!
张国良 2011-06-07
  • 打赏
  • 举报
回复
在action中或service中使用一下强转
UserInfo user = new UserInfo();
try {
user.setUserId(userId);
user.setUserUseName(new String(userUseName.getBytes("ISO-8859-1"), "utf-8"));
user.setUserName(new String(userName.getBytes("ISO-8859-1"), "utf-8"));
user.setUserPassword(new String(userPassword.getBytes("ISO-8859-1"), "utf-8"));
user.setUserDept(userDept);
user.setUserTel(new String(userTel.getBytes("ISO-8859-1"), "utf-8"));
user.setUserCardId(new String(userCardId.getBytes("ISO-8859-1"), "utf-8"));
user.setUserRoleId(userRoleId);

userInfoService.userInfoInsert(user);
}
javamyself 2011-06-07
  • 打赏
  • 举报
回复
给tomcat里的servelt.xml文件的Connector 节点下设置你的编码格式 URIEncoding="GBK" 试试
随心点儿 2011-06-07
  • 打赏
  • 举报
回复
统一编码utf-8,之后就是用get提交表单数据,若传递到其他页面时乱码的话,用iso-8859-1进行转一下编码方式!
kevinPai2011 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tkd03072010 的回复:]
写个过滤器吧
或者
一般情况下 request默认编码为iso-8859-1
当遇到中文的时候肯定报错
所以在设置一下编码:

Java code

//设置编码
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response……
[/Quote]
+1
KingViker 2011-06-07
  • 打赏
  • 举报
回复
忘记了 还有数据库 当然那个是必须要考虑的 还有就是get和post的时候
KingViker 2011-06-07
  • 打赏
  • 举报
回复
总是有人问关于乱码的问题 其实你们只要明白原理 那里来的那么多乱麻问题,我觉得字符集不同意的地方不多吧?不外乎 前台html,后台 还有一个就是console控制台的默认语言(当然这个很容易被人忽略),有时候你在设置字符值的时候不是很到位可能是先转换成默认的语言在转换成你设置的 其实就这么几个问题
xiaona1047985204 2011-06-07
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%> 这的charset=UTF-8
xinghen88 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tkd03072010 的回复:]
写个过滤器吧
或者
一般情况下 request默认编码为iso-8859-1
当遇到中文的时候肯定报错
所以在设置一下编码:

Java code

//设置编码
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response……
[/Quote]
这种方法行不通的,过滤器也没用,额也遇到这种问题了,超链接传递中文参数乱码问题!坐等高手学习中.....
TKD03072010 2011-06-07
  • 打赏
  • 举报
回复
写个过滤器吧
或者
一般情况下 request默认编码为iso-8859-1
当遇到中文的时候肯定报错
所以在设置一下编码:

//设置编码
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html");

楼主可以试试这两种方法!!!
xianaofei 2011-06-07
  • 打赏
  • 举报
回复
String pub=new String(request.getParameter("publish").getBytes("ISO-8859-1"), "gb2312");

把前后两个参数换换试试 写成
String pub=new String(request.getParameter("publish").getBytes("gb2312"), "iso-8859-1");
tt11223 2011-06-07
  • 打赏
  • 举报
回复
把文件保存的编码设置成gb2312
iamlinkent 2011-06-07
  • 打赏
  • 举报
回复
把文件保存的编码设置成gb2312
180Yeah 2011-06-07
  • 打赏
  • 举报
回复
友情帮顶
lffsonic 2011-06-07
  • 打赏
  • 举报
回复
写一个fiter,本例解决ajax中文乱码,url等中文乱码

public class CodeFilter
implements Filter
{

private static final String DEFAULT_ENCODING = "GBK";
private static final String ajaxEncoding = "UTF-8";
protected String commonEncoding;
protected FilterConfig filterConfig;
protected boolean ignore;

public CodeFilter()
{
ignore = true;
}

public void init(FilterConfig filterConfig)
throws ServletException
{
this.filterConfig = filterConfig;
commonEncoding = filterConfig.getInitParameter("encoding");
if (commonEncoding == null)
commonEncoding = "GBK";
String value = filterConfig.getInitParameter("ignore");
if (value == null)
ignore = true;
else
if (value.equalsIgnoreCase("true"))
ignore = true;
else
if (value.equalsIgnoreCase("yes"))
ignore = true;
else
ignore = false;
}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
{
try
{
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
if (ignore || request.getCharacterEncoding() == null)
if (request.getHeader("RequestType") != null && request.getHeader("RequestType").equalsIgnoreCase("ajax"))
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
} else
if (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest"))
{
if (request.getHeader("content-type") != null && request.getHeader("content-type") != "")
request.setCharacterEncoding("UTF-8");
response.setContentType((new StringBuilder("text/html;charset=")).append(commonEncoding).toString());
} else
{
request.setCharacterEncoding(commonEncoding);
response.setCharacterEncoding(commonEncoding);
response.setContentType((new StringBuilder("text/html;charset=")).append(commonEncoding).toString());
}
filterChain.doFilter(req, res);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ServletException e)
{
e.printStackTrace();
}
}

public void destroy()
{
commonEncoding = null;
filterConfig = null;
}
}
clook819 2011-06-07
  • 打赏
  • 举报
回复
我只是友情帮顶
kongluhua 2011-06-07
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
这两个地方都统一编码~~
bike_j2ee 2011-06-06
  • 打赏
  • 举报
回复
把<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
去了!
jonathan7777 2011-06-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fclxyz 的回复:]
乱码问题是csdn上永恒不变的话题啊。

首先要统一编码,项目的,页面的,数据库 的,最好都弄成utf8.

然后就是不要用超链接传中文
[/Quote]页面的还有数据库的都是gb2312,项目的怎么弄啊?我统一了前两个可是还是乱码
加载更多回复(1)

67,513

社区成员

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

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