JSP在使用URL传递中文参数的时候遇到的乱码问题

来源:互联网 发布:三菱plc模拟软件 编辑:程序博客网 时间:2024/05/01 17:29

今天在写作业的时候,又遇到了这个问题……几年前学JSP的时候就曾经遇到过,那时候不知道上网查的什么办法,就给解决了。但再次碰到的时候,总是又忘记,然后又是一顿google…… 这次我索性把这个问题及其解决办法写出来吧,免得以后再忘记……

这个问题描述如下:

在我的web project中,有那么几个JSP。其中有index.jsp,里头定义了form(发送到middle.jsp),然后form有文本框的输入项。其次是response.jsp,它用于获取index.jsp发过来的request的参数,其中也包括获取中文参数。还有middle.jsp,用于把response.jsp包含进去……

当我点击发送的时候,哈哈,问题来了,response.jsp里头显示的request.getParameter("XXXX")的值(其实应该是个中文值)是一堆问号……我的index.jsp页面用的编码是UTF-8,response也是,项目的编码也是UTF-8。好了,问题描述完毕……

不说别的,先上代码

 

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
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>Hello</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 bgcolor="white">
<img src="duke.waving.gif">
<h2>Hello, my name is Duke. What's yours?</h2>
<form method="post">
<div>
<table width="50%">
<tr>
<td width="30%">用户名</td><td width="70%"><input type="text" name="username" size="25"></td>
</tr>
<tr>
<td>用户生日</td><td><input type="text" name="birthday" size="25"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</div>
</form>

</body>
<%
if( request.getParameter("username") != null && request.getParameter("birthday") != null){
String myparameter = "Passion!";
request.setCharacterEncoding("UTF-8");
request.setAttribute("another",myparameter);
if( request.getRequestDispatcher("middle.jsp") != null ){
request.getRequestDispatcher("middle.jsp").forward(request,response);
}
}
%>
</html>

 

 

middle.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

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 'middle.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 bgcolor="white">

 

 <%@ include file="response.jsp" %>     

 

  </body>

</html>

 

response.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 

<%

String userName = new String(request.getParameter("username").getBytes("iso8859-1"),"UTF-8");

String birthday = new String(request.getParameter("birthday").getBytes("iso8859-1"),"UTF-8");

String myparameter = (String)request.getAttribute("another");

%>

用户名:<%=userName %>

用户生日:<%=birthday %>

用户自定义参数:<%=myparameter %>

 

解决办法如代码中粗字体的部分,很清晰了吧…… 这样解决是一种比较不错的办法……反正我测试了,是没问题,但实际项目中肯定不能这么写啦……这样的话要维护的代码量又得加大了!最好的办法还是过滤器…… 
总结:URL传参数的时候,一般传中文,就会出现如我遇到的这样的那样的乱码……如果是学习的话,可以选择我所使用的办法来解决。但是,做项目绝对不推荐…… 本人能力比较弱,哈哈,说得不对的地方请高手指正哈!
Allen L.R
2010-11-08