如何解决 jsp网页之间 传递中文乱码 的问题
<%@ 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");这句话的话,要想在网页间传递中文不至于变成乱码我该怎么办?
[解决办法]
<%@ 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">
这两个地方都统一编码~~