計算機生於美國
亂碼的出現根本原因在於編碼和解碼使用了不同的編碼方案
jsp頁面間的參數傳遞有以下幾種方式
在介紹表單傳遞參數的內容之前
表單的提交方式
通常使用的表單的提交方式主要是
Requesttest
- <%@ page language="java" contentType="text/html; charset=UTF
" - pageEncoding="UTF
"%> - <!DOCTYPE html PUBLIC "
//W C//DTD HTML Transitional//EN" " - <html>
- <head>
- <meta http
equiv="Content Type" content="text/html; charset=UTF "> - <title>Insert title here</title>
- </head>
- <body>
- <%
post方式提交表單 %> - <form action="
- UserName:<input type="text" name="username"/>
- Password:<input type="password" name="password"/>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=UTF
" pageEncoding="UTF "%> <!D - OCTYPE html PUBLIC "
//W C//DTD HTML Transitional//EN" " - itle>Insert title here</title> </head> <body> <%
post方式提交表單 %> <form action=":<input type="text" nam - e="username"/> Password:<input type="password" name="password"/> <input type="submit" va
- lue="Submit"> </form> </body> </html>
在上面的請求頁面的username輸入框裡輸入的是“世界杯”三個漢字
Post方式的請求報文代碼
- POST /EncodingTest/requestresult
jsp HTTP/ - Accept: image/gif
image/jpeg image/pjpeg image/pjpeg application/x shockwave flash applicati - on/vnd
ms excel application/vnd ms powerpoint application/msword */* - Referer:
- Accept
Language: zh cn - User
Agent: Mozilla/ (compatible; MSIE ; Windows NT ; Trident/ ; CIBA; aff kingsoft ci - ba;
NET CLR ) - Content
Type: application/x www form urlencoded - Accept
Encoding: gzip deflate - Host: localhost:
- Content
Length: - Connection: Keep
Alive - Cache
Control: no cache - username=%E
%B % %E % % C%E % D%AF&password= - POST /EncodingTest/requestresult
jsp HTTP/ Accept: image/gif image/jpeg image/pjpeg image/pjp - eg
application/x shockwave flash application/vnd ms excel application/vnd ms powerpoint applicati - on/msword
*/* Referer: : zh cn Us - er
Agent: Mozilla/ (compatible; MSIE ; Windows NT ; Trident/ ; CIBA; aff kingsoft ciba; N - ET CLR
) Content Type: application/x www form urlencoded Accept Encoding: gzip deflate H - ost: localhost:
Content Length: Connection: Keep Alive Cache Control: no cache username=%E %B % %E % % C%E % D%AF&password=
以上報文內容
下面的同一請求頁面的get提交方式的請求報文
Get方式的請求報文代碼
- GET /EncodingTest/requestresult
jsp?username=%E %B % %E % % C%E % D%AF&password= H - TTP/
- Accept: image/gif
image/jpeg image/pjpeg image/pjpeg application/x shockwave flash applica - tion/vnd
ms excel application/vnd ms powerpoint application/msword */* - Referer:
- Accept
Language: zh cn - User
Agent: Mozilla/ (compatible; MSIE ; Windows NT ; Trident/ ; CIBA; aff kingsoft cib - a;
NET CLR ) - Accept
Encoding: gzip deflate - Host: localhost:
- Connection: Keep
Alive - GET /EncodingTest/requestresult
jsp?username=%E %B % %E % % C%E % D%AF&passwo - rd=
HTTP/ Accept: image/gif image/jpeg image/pjpeg image/pjpeg application/x shockw - ave
flash application/vnd ms excel application/vnd ms powerpoint application/msword */* Refer - er: : zh
cn User Agent: Mozi - lla/
(compatible; MSIE ; Windows NT ; Trident/ ; CIBA; aff kingsoft ciba; NET CLR ) Accept Encoding: gzip deflate Host: localhost: Connection: Keep Alive
以上報文內容
請求報文中對漢字的處理
從上面兩種報文可以看出頁面上輸入的“世界杯”三個漢字被替換成了"%E
這個字符串是“世界杯”這三個漢字對應的"UTF
這時可能又會有另外一個問題
在了解了以上內容後
以上例為例
方案一代碼
- <%String str = new String(request
getParameter("username") getBytes("ISO ") "utf "); %> - Username:<%=str %>
- <%String str = new String(request
getParameter("username") getBytes("ISO ") "utf "); %> Usern - ame:<%=str %>
既然request
方案一是一種比較萬能的方法
方案二代碼
- <%request
setCharacterEncoding("UTF "); %> - <%request
setCharacterEncoding("UTF "); %>
方案二是在頁面的最開始或者是在該頁面中使用的第一個request
Overrides the name of the character encoding used in the body of this request
所以方案二只對post方式提交的請求有效
從上面的敘述可以知道
Encodingfilter代碼
- public class EncodingFilter implements Filter {
- private String charset;
- @Override
- public void destroy() {
- // TODO Auto
generated method stub - }
- @Override
- public void doFilter(ServletRequest request
ServletResponse response - FilterChain chain) throws IOException
ServletException { - //用init方法取得的charset覆蓋被攔截下來的request對象的charset
- request
setCharacterEncoding(this charset); - //將請求移交給下一下過濾器
如果還有的情況下 - chain
doFilter(request response); - }
- @Override
- public void init(FilterConfig config) throws ServletException {
- //從web
xml中的filter的配制信息中取得字符集 - this
charset = config getInitParameter("charset"); - }
- }
- public class EncodingFilter implements Filter { private String charset; @Override public void destr
- oy() { // TODO Auto
generated method stub } @Override public void doFilter(ServletRequest req - uest
ServletResponse response FilterChain chain) throws IOException ServletException { //用init方 - 法取得的charset覆蓋被攔截下來的request對象的charset request
setCharacterEncoding(this charset); //將 - 請求移交給下一下過濾器
如果還有的情況下 chain doFilter(request response); } @Override pub - lic void init(FilterConfig config) throws ServletException { //從web
xml中的filter的配制信息中取得字 - 符集 this
charset = config getInitParameter("charset"); } }
要想這個過濾器生效
Web
- <filter>
- <filter
name>EncodingFilter</filter name> - <filter
class>cn eric encodingtest filter EncodingFilter</filter class> - <init
param> - <param
name>charset</param name> - <param
value>UTF </param value> - </init
param> - </filter>
- <filter
mapping> - <filter
name>EncodingFilter</filter name> - <url
pattern>/*</url pattern> - </filter
mapping> - <filter> <filter
name>EncodingFilter</filter name> <filter class>cn eric encodingtest filter Encodi - ngFilter</filter
class> <init param> <param name>charset</param name> <param value>UT - F
</param value> </init param> </filter> <filter mapping> <filter name>EncodingFilter</filt - er
name> <url pattern>/*</url pattern> </filter mapping>
有些時候可能會遇到通過一個超級鏈接來把參數傳到下一個頁面
- <a href="
/jstlresult jsp?content=世界杯">Go South Africa
跟form提交有些不同的是
這裡浏覽器並沒有幫我們把這個轉化工作搞定
- <a href="
/jstlresult jsp?content=<%=java net URLEncoder encode("世界杯" "utf ") %>">Go South Africa
這樣的話在第二個頁面就能使用
- <%String str = new String(request
getParameter("content") getBytes("ISO ") "utf "); %>
的方法來正確的得到這個參數值了
總結一下
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19243.html