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

          jsp亂碼問題研究

          Posted on 2006-09-28 09:47 xyang 閱讀(376) 評論(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 >


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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 碌曲县| 社会| 湖北省| 东明县| 河间市| 鸡泽县| 体育| 台湾省| 香港| 秭归县| 潞西市| 金阳县| 年辖:市辖区| 巍山| 昌都县| 霞浦县| 玛曲县| 凤台县| 灵武市| 讷河市| 宜良县| 调兵山市| 织金县| 云林县| 来安县| 保靖县| 延安市| 蛟河市| 福鼎市| 宜昌市| 洱源县| 定日县| 延安市| 靖州| 麦盖提县| 囊谦县| 寻甸| 通海县| 尉氏县| 沭阳县| 民和|