GET方式提交参数分析

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

code.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>
<form action="${pageContext.request.contextPath}/doget" method="get">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
</body>
</html>

DoGetServlet

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

     protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//URL的组成
//域名:端口/contextPath/servletPath/pathInfo?queryString //1.不加处理获得的请求参数,获得的结果为"ISO-8859-1"解码后的参数
String name1 = request.getParameter("name");
System.out.println(name1); //name1 = 啊啊å //2.先用"ISO-8859-1"进行编码,再用"UTF-8"进行解码
String name2 = URLEncoder.encode(request.getParameter("name"), "iso-8859-1");
name2 = URLDecoder.decode(name2, "utf-8");
System.out.println(name2); //name2 = 啊啊啊 //3.用String的构造函数对获得的请求参数进行处理,
//通过使用指定的 charset 解码指定的 byte 数组,
//构造一个新的 String。
String name3 =
new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
System.out.println(name3); //name3 = 啊啊啊 }

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

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//URL的组成
//域名:端口/contextPath/servletPath/pathInfo?queryString //1.不加处理获得的请求参数,获得的结果为"ISO-8859-1"解码后的参数
String name1 = request.getParameter("name");
System.out.println(name1); //name1 = 啊啊啊 //2.先用"ISO-8859-1"进行编码,再用"UTF-8"进行解码
String name2 = URLEncoder.encode(request.getParameter("name"), "iso-8859-1");
name2 = URLDecoder.decode(name2, "utf-8");
System.out.println(name2); //name2 = ??? //3.用String的构造函数对获得的请求参数进行处理,
//通过使用指定的 charset 解码指定的 byte 数组,
//构造一个新的 String。
String name3 =
new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
System.out.println(name3); //name3 = ??? }

POST方式提交参数分析

Java开发中的编码分析__GET&amp;POST-LMLPHP

code.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>
<form action="${pageContext.request.contextPath}/doget" method="post">
<input type="text" name="name">
<input type="submit" value="提交">
</form>
</body>
</html>
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //1.不加处理获得的请求参数,获得的结果为"ISO-8859-1"解码后的参数
String name1 = request.getParameter("name");
System.out.println(name1); //name1 = 啊啊å }
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //2.设置请求参数为"UTF-8",获得结果为"UTF-8"•Š解码后的参数
request.setCharacterEncoding("utf-8");
String name2 = request.getParameter("name");
System.out.println(name2); //name1 = 啊啊啊
}
04-02 08:11
查看更多