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

          jsp亂碼問題研究

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


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


          網站導航:
           
          主站蜘蛛池模板: 锡林郭勒盟| 青铜峡市| 四子王旗| 贵阳市| 汕头市| 乐平市| 兰溪市| 吉木萨尔县| 且末县| 宁海县| 松滋市| 来宾市| 彰武县| 常山县| 敦化市| 台北市| 绥滨县| 滨州市| 华坪县| 尼玛县| 平昌县| 娄底市| 漯河市| 望城县| 二连浩特市| 铜鼓县| 谷城县| 健康| 延边| 罗田县| 宣化县| 元阳县| 祁东县| 德钦县| 朔州市| 绥宁县| 永靖县| 五华县| 富蕴县| 门源| 阜宁县|