乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Servlet中直接response.getWriter().write() 輸出亂碼解決!

       收藏小管 2017-07-02

      基礎(chǔ)環(huán)境

      項目編碼:utf-8
      頁面編碼:utf-8
      請求類型:Post

      Demo1:

      Servlet核心代碼

      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
      String username = request.getParameter("username");
      System.out.println(username);//控制臺正確輸出中文
      response.getWriter().write(username);//頁面中文亂碼
      • 1
      • 2
      • 3
      • 4
      • 5
      • 1
      • 2
      • 3
      • 4
      • 5

      運行結(jié)果

      這里寫圖片描述

      Demo2:

      Servlet核心代碼

      request.setCharacterEncoding("UTF-8");
      String username = request.getParameter("username");
      System.out.println(username);
      request.setAttribute("username", username);
      request.getRequestDispatcher("result.jsp").forward(request, response);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

      運行結(jié)果

      這里寫圖片描述

      Demo3:

      Servlet核心代碼

      request.setCharacterEncoding("UTF-8");
      response.setContentType("text/html;charset=utf-8");
      String username = request.getParameter("username");
      System.out.println(username);
      response.getWriter().write(username);
      • 1
      • 2
      • 3
      • 4
      • 5
      • 1
      • 2
      • 3
      • 4
      • 5

      運行結(jié)果

      這里寫圖片描述

      思考?

      為什么Demo1會出現(xiàn)亂碼?而Demo2沒有出現(xiàn)亂碼?
      明明Demo1的response也設(shè)置了utf-8編碼。

      Demo1亂碼的原因

      從控制臺輸出可以看出,控制臺并沒有亂碼,只是response在輸出中文的
      時候出現(xiàn)了亂碼,因此,這行代碼:response.setCharacterEncoding(“UTF-8”);
      可能并沒有起到想要的效果。

      反觀Demo2,并沒有response.setCharacterEncoding(“UTF-8”); 這句話,但是輸出的
      頁面是通過request的轉(zhuǎn)發(fā)到JSP頁面實現(xiàn)的。其實JSP頁面最后也要轉(zhuǎn)換為Servlet
      頁面中的元素也是response.getWriter().write(“”);出來的。既然有response,那么
      這個response的編碼是如何確定的呢?我們看一看JSP與對應(yīng)的Servlet就可以明白了:

      這里寫圖片描述

      也就是說Demo2中的response.setCharacterEncoding隱藏在JSP頁面中了。
      然后根據(jù)轉(zhuǎn)換后的Servlet可以看出response.setContentType(“text/html;charset=utf-8”);
      才能達到應(yīng)有的效果,在使用http協(xié)議的情況中,該方法設(shè)置 Content-type實體報頭
      response.setContentType()的作用是使客戶端瀏覽器,區(qū)分不同種類的數(shù)據(jù),并根據(jù)不
      同的MIME調(diào)用瀏覽器內(nèi)不同的程序嵌入模塊來處理相應(yīng)的數(shù)據(jù)。
      例如:web瀏覽器就是通過MIME類型來判斷文件是GIF圖片,通過MIME類型來處理json字符串。
      Tomcat的安裝目錄\conf\web.xml 中就定義了大量MIME類型 ,可以參考。
      response.setContentType(“text/html; charset=utf-8”); html
      response.setContentType(“text/plain; charset=utf-8”); 文本
      response.setContentType(“text/JavaScript; charset=utf-8”); json數(shù)據(jù)
      response.setContentType(“application/xml; charset=utf-8”); xml數(shù)據(jù)

      因此Demo3正是解決Demo1亂碼的方法!

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多