posts - 19, comments - 1, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          jsp亂碼問題研究

          Posted on 2006-09-28 09:47 xyang 閱讀(380) 評論(0)  編輯  收藏
          ??? ie默認根據服務器傳回來的contentType頭進行顯示(忽略mata標簽),對于html這種靜態文件,由于沒有contentType頭則根據<meta http-equiv="Content-Type" content="text/html; charset=GBK" />標簽中的編碼類型進行顯示.

          ??? pageEncoding指定的是jsp編譯時的編碼格式,必須對應于jsp文件內容的編碼,否則是亂碼
          默認pageEncoding為:ISO-8859-1,如果不指定contentType,輸出對應于pageEncoding的編碼方式
          也就是如果都不設置的話,默認輸出ISO-8859-1,肯定是亂碼(保存為unicode即可正常顯示).

          ??? response.setCharacterEncoding("GBK")
          <%@ page contentType="text/html; charset=UTF-8">
          這兩句作用相同,設置輸出的編碼類型,但response.setCharacterEncoding("GBK")優先級高

          ??? 通過 get/post 方式從 ie中發送漢字,發送編碼方式由Content-Type決定,request.getParameter("XX")得到的字符串是用ISO-8859-1表示的,所以必須在取值前用HttpServeletRequest.setCharacterEncoding 設置想得到的編碼類型,或是在<Connector>中添加URIEncoding="GBK"屬性,來獲取正確的編碼類型,但是,在執行setCharacterEncoding()之前,不能執行任何getParameter()。java doc上說明:This method must be called prior to reading request parameters or reading input using getReader()。而且,該指定只對POST方法有效,對GET方法無效。分析原因,應該是在執行第一個getParameter()的時候, java將會按照編碼分析所有的提交內容,而后續的getParameter()不再進行分析,所以setCharacterEncoding()無效。 而對于GET方法提交表單是,提交的內容在URL中,一開始就已經按照編碼分析所有的提交內容,setCharacterEncoding()自然就無效。



          <% @?page?contentType = " text/html;?charset=UTF-8 " ?pageEncoding = " UTF-8 " %>
          <%
          request.setCharacterEncoding(
          " UTF-8 " );
          response.setCharacterEncoding(
          " UTF-8 " );
          %>
          < html >
          ??
          < head >
          ????
          < title > test </ title >
          ????
          < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=UTF-8" ? />
          ??
          </ head >
          ??
          < body > = <% = new ? String (request.getParameter( " foo " ).getBytes( " iso8859-1 " ), " UTF-8 " ) %> =
          ??????
          < form? action ="" ?method ="get" >
          ??????foo?=?
          < input? type ="text" ?name ="foo" ?value ="${param[" foo"]}" >
          ??????????
          < input? type ="submit" >
          ??????
          </ form >
          ??????tomcat:
          < Connector? port ="8080" ?URIEncoding ="GBK" ? />
          ??
          </ body >
          </ html >


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 曲水县| 五台县| 大竹县| 永德县| 安远县| 深水埗区| 闽侯县| 拉萨市| 涪陵区| 吴桥县| 庆元县| 邹城市| 大渡口区| 苍溪县| 巴楚县| 曲松县| 天水市| 昌都县| 玉林市| 长沙市| 湖南省| 榆社县| 台江县| 桐城市| 凤山市| 韶山市| 策勒县| 长丰县| 天柱县| 河西区| 修武县| 通道| 扎鲁特旗| 铁力市| 柘城县| 开平市| 荥经县| 内丘县| 手游| 乌审旗| 黑山县|