jsp的一个页面接收另外一个页面传过来的中文显示问乱码,后来我又写了一个简单的测试程序准备测试一下,结果还是如此!代码如下:sessiontest1.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>sessiontest1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body>
<form action="sessiontest2.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
sessiontest2.jsp<%@ page language="java" contentType="text/html;charset=GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%!String trans(String chi)
{
String result = null ;
byte[] byte1;
try
{
byte1 = chi.getBytes("GB2312");
result = new String(byte1);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
%>
<html>
<head>
<title>My JSP 'sessiontest2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
String username = (String)request.getParameter("username");
session.setAttribute("username",username);
%>
您好
<%=trans(username) %>
!你最想去的地方是
<p>
<form action="sessiontest3.jsp" method="post">
<input type="text" name="place">
<p>
<input type="submit" value="submit">
</form>
</body>
</html>sessiontest3.jsp<%@ page language="java" contentType="text/html;charset=GB2312"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'sessiontest3.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
String username = (String)session.getAttribute("username");
String place = (String)request.getParameter("place");
%>
<%=username %>
<br>
最想去的地方是
<br>
<%=place %>
</body>
</html>
我在sessiontest2.jsp里写了一段显示中问字符的乱码,<%=trans(username) %>
可是从sessiontest1.html传过来的中文还是不能正确显示,希望大家帮我看下.
<html>
<head>
<title>sessiontest1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body>
<form action="sessiontest2.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
sessiontest2.jsp<%@ page language="java" contentType="text/html;charset=GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%!String trans(String chi)
{
String result = null ;
byte[] byte1;
try
{
byte1 = chi.getBytes("GB2312");
result = new String(byte1);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
%>
<html>
<head>
<title>My JSP 'sessiontest2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
String username = (String)request.getParameter("username");
session.setAttribute("username",username);
%>
您好
<%=trans(username) %>
!你最想去的地方是
<p>
<form action="sessiontest3.jsp" method="post">
<input type="text" name="place">
<p>
<input type="submit" value="submit">
</form>
</body>
</html>sessiontest3.jsp<%@ page language="java" contentType="text/html;charset=GB2312"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'sessiontest3.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
String username = (String)session.getAttribute("username");
String place = (String)request.getParameter("place");
%>
<%=username %>
<br>
最想去的地方是
<br>
<%=place %>
</body>
</html>
我在sessiontest2.jsp里写了一段显示中问字符的乱码,<%=trans(username) %>
可是从sessiontest1.html传过来的中文还是不能正确显示,希望大家帮我看下.
解决方案 »
- 网页导到myEclipse后,后缀变成jsp, 用浏览器访问本来好好的布局变乱了?
- 请教关于 myeclipse 问题
- 报错“org.apache.jasper.JasperException: Hibernate operation: Cannot open connection”问题
- 有个JS中添加变量的问题
- 新手~求助一题JSP关于插入数据到表的题!谢谢各位咯~
- mysql数据库连接
- logic:equal 可否同时判断两个属性值
- <jsp中应用css文件不生效
- 新手求问:现在做Java Web都涉及到哪些技术?
- spring mvc 如何从前台表单传递集合参数并绑定集合对象。
- 关于项目配置问题???
- 怎么动态修改form的action的值?
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> My JSP 'sessiontest2.jsp' starting page </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
request.setCharacterEncoding("gb2312");
String username = (String)request.getParameter("username");
session.setAttribute("username",username);
%>
您好:<%= username %>
!你最想去的地方是
<p>
<form action="sessiontest3.jsp" method="post">
<input type="text" name="place">
<p>
<input type="submit" value="submit">
</form>
</body>
</html>
第二阶段会用utf-8至utf-8,第三阶段是由tomcat出来的网页,用的是
contentType(由服务器发送给客房端的内容编码方式)。第一阶段是jsp编译成.java文件,它会依据pageEncoding的设定读取jsp,
结果是由指定的编码方案翻译成统一的utf-8 java源代码,若设错了会出现中文乱码,
第二阶段是由javac把.java文件编译,不论jsp用什么编译方式,经过这个阶段的结果全部是
utf-8编码,这是JVM对常数字串在二进制内表达的规范。
第三阶段是tomcat载入和执行阶段二的.class文件,这时隐藏在阶段一、二的参数contentType发挥功效。有的浏览器的默认编码方式是gbk,有的是gb2312,这两种都是中文编码方案,所以有时并不需要把
pageEncoding和contentType都指定,但有的是ISO-8859-1,utf-8等编码方式 ,这是有中文的话就会出现乱码,
所以最安全的方式就是显式的把pageEncoding和contentType都指定编码方案。
response.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=GB2312");将sessiontest1.html里面的编码也改成了
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
charset也改成了gb2312,还是不管用啊!
希望哪位能提供一写正确的源代码>
写个filter最方便,所有页面都能用它
<%@ page contentType = "text/html;charset=gb2312" %>
<HTML>
<%
request.setCharacterEncoding("GB2312");
%>
</HTML> 相比之下其实你在7楼加一个pageEncoding的设置可能就可以了。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> My JSP 'sessiontest2.jsp' starting page </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
request.setCharacterEncoding("gb2312");
String username = (String)request.getParameter("username");
session.setAttribute("username",username);
%>
您好: <%= username %>
!你最想去的地方是
<p>
<form action="sessiontest3.jsp" method="post">
<input type="text" name="place">
<p>
<input type="submit" value="submit">
</form>
</body>
</html>