今天在寫作業的時候,又遇到了這個問題……幾年前學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 %>