JSP在使用URL傳遞中文參數的時候遇到的亂碼問題

今天在寫作業的時候,又遇到了這個問題……幾年前學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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章