Web 項目遇到的亂碼問題

來源:互聯網
上載者:User

標籤:web   問題:   內容   包含   ring   alt   中文編碼   except   技術分享   

問題代碼:  jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset="UTF-8">    <title>Insert title here</title>  </head><body>    <!-- 提交的時候必須是name=value索引值對 -->        <!-- 應用程式名稱+請求路徑 -->    <form action="/gp_web_day17/RequestDemo5" method="post">        使用者名稱:<input type="text" name="username"><br/>        密    碼:<input type="password" name="pwd"><br/>        性    別:<input type="radio" name="sex" value="男" />男               <input type="radio" name="sex" value="女" />女<br />                       愛    好:<input type="checkbox" name="hobby" value="睡覺">睡覺                          <input type="checkbox" name="hobby" value="遊戲">遊戲                   <input type="checkbox" name="hobby" value="旅遊">旅遊<br />                所在國家:<select name="sel">                    <option>-----請選擇-----</option>                    <option value="cn">中國</option>                    <option value="fa">法國</option>                    <option value="en">英國</option>                </select>                        <input type="submit" value="註冊">    </form></body></html>
   Servlet:
@WebServlet("/RequestDemo5")public class RequestDemo5 extends HttpServlet {    private static final long serialVersionUID = 1L;           public RequestDemo5() {        super();            }    //處理get方式的亂碼問題:    //     get方式傳遞的參數內容預設編碼方式問ISO-8859-1        //    1 自己用再編碼再解碼處理    //    2在tomcat的設定檔中加入URIEncoding="UTF-8"    public String dispose(String str){                try {            
       //編碼 byte[] arr = str.getBytes("ISO-8859-1");   //解碼 String ssString = new String(arr, "UTF-8"); return ssString; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("解碼前:"+request.getParameter("username")+" "+request.getParameter("sex")); String name = dispose(request.getParameter("username")); String sex = dispose(request.getParameter("sex")); System.out.println("解碼後:"+name+" sex:"+sex); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

 

運行結果:

 

問題分析:

問題解決:
//告訴瀏覽器使用ISO-8859-1編碼方式,就可以檢測get方法再編碼再解碼方式是否可以解決亂碼問題

<meta http-equiv="Content-Type" content="text/html; charset="ISO-8859-1">

 

問題總結:
<!-- contentType="text/html; charset=UTF-8:          jsp在編輯器中儲存檔案,使用的是UTF-8編碼格式        問題:通過瀏覽器調用servlet檔案,出現中文亂碼--><!-- pageEncoding="UTF-8:          jsp在編輯器中儲存檔案,使用的是UTF-8編碼格式         問題:通過瀏覽器調用jsp,jsp出現中文亂碼--><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<!-- 通過<meta>標籤類比response頭,控制瀏覽器用ISO-8859-1的編碼解析 --><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


//處理get方式的亂碼問題:
  // get方式傳遞的參數內容預設編碼方式ISO-8859-1

  //1 自己用再編碼再解碼處理
  //2在tomcat的設定檔中加入URIEncoding="UTF-8"

 

//處理post方法解決亂碼問題:

//將response對象中的資料以UTF-8解碼後的位元組發送給瀏覽器
  //request.setCharacterEncoding("utf-8");

 

UTF-8國際編碼  GBK中文編碼(包含GB2312,即通過GB2312編碼後可以通過GBK解碼,反之不成立)

tomcat預設編碼是ISO-8859-1

getBytes()通過平台預設字元集進行編碼

get方式傳遞的參數內容預設編碼方式ISO-8859-1

 

?? 表示沒有編碼

 

 參考部落格:http://www.cnblogs.com/oldinaction/p/5167481.html

  

Web 項目遇到的亂碼問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.