码迷,mamicode.com
首页 > 编程语言 > 详细

Java开发中的编码分析__GET&POST

时间:2017-10-17 15:01:48      阅读:235      评论:0      收藏:0      [点我收藏+]

标签: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

标签:coding   print   数组   protocol   logs   通过   redirect   tchar   color   

原文地址:http://www.cnblogs.com/rocker-pg/p/7680794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!