各種編碼問題產生原因以及解決辦法---------響應編碼,請求編碼,URL編碼

來源:互聯網
上載者:User

標籤:script   conf   user   body   tty   gets   extends   out   expires   

 響應編碼

產生原因以及解決辦法:

樣本:

 1 package cn.yzu; 2 import java.io.IOException; 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 public class AServlet extends HttpServlet { 8     public void doGet(HttpServletRequest request, HttpServletResponse response) 9             throws ServletException, IOException {10         response.setContentType("text/html;charset=utf-8");11         response.getWriter().println("張三");12         response.getWriter().println("李四");13     }14 }
View Code

 運行:

請求編碼

 POST請求:

GET請求(conf/server.xml):

樣本:

index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 7 <html> 8   <head> 9     <base href="<%=basePath%>">10     <title>My JSP ‘index.jsp‘ starting page</title>11     <meta http-equiv="pragma" content="no-cache">12     <meta http-equiv="cache-control" content="no-cache">13     <meta http-equiv="expires" content="0">    14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">15     <meta http-equiv="description" content="This is my page">16   </head>17   <body>18       <form action="AServlet" method="post">19           <input type="text" value="張三" name="zs">20           <input type="submit" value="post請求">21       </form><hr>22       <form action="AServlet" method="get">23           <input type="text" value="李四" name="ls">24           <input type="submit" value="get請求">25       </form>26   </body>27 </html>
View Code

 AServlet

 1 package cn.yzu; 2 import java.io.IOException; 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 public class AServlet extends HttpServlet { 8     public void doGet(HttpServletRequest request, HttpServletResponse response) 9             throws ServletException, IOException {10         //處理get請求11         System.out.println(request.getParameter("ls"));    //不處理12         String name = request.getParameter("ls");        13         byte[] b = name.getBytes("iso-8859-1");            //回退14         name = new String(b, "utf-8");                    //重編15         System.out.println(name);16         17     }18     public void doPost(HttpServletRequest request, HttpServletResponse response)19             throws ServletException, IOException {20         //處理post請求21         request.setCharacterEncoding("utf-8");22         String username = request.getParameter("zs");23         System.out.println(username);24     }25 }
View Code

頁面:

輸出結果:

URL編碼:

URL編碼解碼樣本:

 1 import java.io.UnsupportedEncodingException; 2 import java.net.URLDecoder; 3 import java.net.URLEncoder; 4 import org.junit.Test; 5 public class Demo { 6     @Test 7     public void fun1() throws UnsupportedEncodingException { 8         String name = "張三"; 9         String s = URLEncoder.encode(name, "UTF-8");//編碼10         System.out.println(s);11         s = URLDecoder.decode(s, "utf-8");            //解碼12         System.out.println(s);13     }14 }
View Code

結果:

 

各種編碼問題產生原因以及解決辦法---------響應編碼,請求編碼,URL編碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.