首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Jsp中文乱码,该怎么处理

2012-01-08 
Jsp中文乱码jsp的一个页面接收另外一个页面传过来的中文显示问乱码,后来我又写了一个简单的测试程序准备测

Jsp中文乱码
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传过来的中文还是不能正确显示,希望大家帮我看下.

[解决办法]
不是在代码中引入一个什么"request.setEndoning...("gb2312")"什么的就可以了吗?嘿嘿,可以我记不得清楚了。不好意思!^_^
[解决办法]
你的sessiontest1.html,在向sessiontest2.html 提交的时候设置下编码试试,应该是你的html的编码和jsp页面的编码不一样造成的,最好用GBK吧.
 

[解决办法]
把sessiontest2.jsp 改成如下:sessiontest3.jsp仿照sessiontest2.jsp
<%@ 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> 


[解决办法]
产生乱码有4个地方,一个HTML页面中要设置编码方式,而且HTML保存是也要设置成相同的编码方式,还有不只是设置REQUEST的编码方式,还有RESPONSE的编码方式,RESPONSE.setCharerSet("UTF-8")好象是这样写的,自己找下吧


[解决办法]
写一个Filter设置编码就可以了
[解决办法]
jsp要经过两次编码,第一阶段会用pageEncoding(即jsp本身的编码),
第二阶段会用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都指定编码方案。
[解决办法]
你可以把这几个页面的编码统一一下,别一个页面是uft-8,而另一个是Gbk,gbk2312的,全部改写成uft-8
写个filter最方便,所有页面都能用它
[解决办法]
其实6楼的朋友讲得很清楚了,楼主在7楼的设置中最好再加一项pageEncoding的设置,我写jsp页面一般是这种框架(中文问题全部解决)希望对你有用:

Java code
<%@ page pageEncoding = "gb2312"%><%@ page contentType = "text/html;charset=gb2312" %><HTML>    <%        request.setCharacterEncoding("GB2312");    %></HTML>
[解决办法]
HTML code
<%@       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>
[解决办法]
用过滤器 ,DoFilter

热点排行