Java開發中的編碼分析__GET&POST

來源:互聯網
上載者:User

標籤:coding   print   數組   protocol   logs   通過   redirect   tchar   color   

GET方式提交參數分析

code.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10     <form action="${pageContext.request.contextPath}/doget" method="get">11         <input type="text" name="name">12         <input type="submit" value="提交">13     </form>14 </body>15 </html>

 

DoGetServlet

 1 <Connector connectionTimeout="20000" port="88" protocol="HTTP/1.1" redirectPort="8443">

 1     protected void doGet(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3         //URL的組成 4         //網域名稱:連接埠/contextPath/servletPath/pathInfo?queryString 5          6         //1.不加處理獲得的請求參數,獲得的結果為"ISO-8859-1"解碼後的參數 7         String name1 = request.getParameter("name"); 8         System.out.println(name1);    //name1 = ???????     9         10         //2.先用"ISO-8859-1"進行編碼,再用"UTF-8"進行解碼11         String name2 = URLEncoder.encode(request.getParameter("name"), "iso-8859-1");12         name2 = URLDecoder.decode(name2, "utf-8");13         System.out.println(name2);    //name2 = 啊啊啊14         15         //3.用String的建構函式對獲得的請求參數進行處理,  16         //通過使用指定的 charset 解碼指定的 byte 數組,17         //構造一個新的 String。18         String name3 = 19                 new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");20         System.out.println(name3);    //name3 = 啊啊啊21         22     }

 1 <Connector connectionTimeout="20000" port="88" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/> 

 1 protected void doGet(HttpServletRequest request, HttpServletResponse response) 2             throws ServletException, IOException { 3         //URL的組成 4         //網域名稱:連接埠/contextPath/servletPath/pathInfo?queryString 5          6         //1.不加處理獲得的請求參數,獲得的結果為"ISO-8859-1"解碼後的參數 7         String name1 = request.getParameter("name"); 8         System.out.println(name1);    //name1 = 啊啊啊 9         10         //2.先用"ISO-8859-1"進行編碼,再用"UTF-8"進行解碼11         String name2 = URLEncoder.encode(request.getParameter("name"), "iso-8859-1");12         name2 = URLDecoder.decode(name2, "utf-8");13         System.out.println(name2);    //name2 = ???14         15         //3.用String的建構函式對獲得的請求參數進行處理,  16         //通過使用指定的 charset 解碼指定的 byte 數組,17         //構造一個新的 String。18         String name3 = 19                 new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");20         System.out.println(name3);    //name3 = ???21         22     }

 

POST方式提交參數分析

code.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10     <form action="${pageContext.request.contextPath}/doget" method="post">11         <input type="text" name="name">12         <input type="submit" value="提交">13     </form>14 </body>15 </html>
1 protected void doPost(HttpServletRequest request, HttpServletResponse response)2             throws ServletException, IOException {3         4         //1.不加處理獲得的請求參數,獲得的結果為"ISO-8859-1"解碼後的參數5         String name1 = request.getParameter("name");6         System.out.println(name1);    //name1 = ???????7         8     }
1 protected void doPost(HttpServletRequest request, HttpServletResponse response)2             throws ServletException, IOException {3         4         //2.佈建要求參數為"UTF-8",獲得結果為"UTF-8"??解碼後的參數5         request.setCharacterEncoding("utf-8");6         String name2 = request.getParameter("name");7         System.out.println(name2);    //name1 = 啊啊啊8     }

 

 


Java開發中的編碼分析__GET&POST

聯繫我們

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