日期:2014-05-18  浏览次数:20694 次

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+&qu