`

HTTP码流与Jsp乱码问题的分析(二)

阅读更多

上一节我是用的GB2312编码方式的页面来测试的,这一节我用UTF-8页面来测试一下。

问题一

问题出现时环境:

1、以POST方式提交表单
2、请求的URL后还附加参数且参数值含有中文
3、附加参数值经过了encodeURIComponent()函数编码
4、Tomcat未设置URIEncoding与useBodyEncodingForURI
5、请求页面与结果页面<%@ page %>指令的contentType与pageEncoding编码方式都为UTF-8
6、结果页面中request的编码方式为UTF-8
 

请求utf-8.jsp与响应页面utf-8rs.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<body>
		<form name=form1 action="" method="post">
			<input type="text" name="textParam1" size="50 px" value="中a ~!@#$%^&amp;*()_+{}|:\&quot; &lt;&gt;?`-=[]\\;',./">
			<br>
			<input type="file" name="fileParam" size="50 px" value="">
			<br>
			<input type="button" value="submit" onclick="submitForm()">
		</form>
		<script type="text/javascript"> 
			function submitForm(){ 
				var str ="中a ~!@#$%^&*()_+{}|:\" <>?`-=[]\\;',./"; 
				form1.action = "utf-8rs.jsp?qryParam1=" + encodeURIComponent(str) + "&qryParam2="+  encodeURIComponent(form1.textParam1.value) ; 
				//form1.action = "http://localhost:8088/utf-8rs.jsp?qryParam1=" + encodeURIComponent(str) + "&qryParam2="+  encodeURIComponent(form1.textParam1.value) ;
				form1.submit(); 
			} 
		</script>
	</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<head>
		<title>Insert title here</title>
	</head>
	<body>
		<%
			//这里一定要设置,且与浏览的提交时编码方式一样,否则乱码(默认Tomcat以iso8859-1解码) 
			//且只能post提交方式的参数起使用,对URL后附带参数不起作用 
			request.setCharacterEncoding("UTF-8");
			String textParam1 = request.getParameter("textParam1");
			String qryParam1 = request.getParameter("qryParam1");
			System.out.println("textParam1=" + textParam1);
			System.out.println("qryParam1=" + qryParam1);
			System.out.println("qryParam1=" + (qryParam1==null?null:new String(qryParam1.getBytes("iso8859-1"),"utf-8")));
		%>
		request.getParameter("textParam1")(<font color=red>UTF-8编码->UTF-8解码</font>):<br>
		<%=request.getParameter("textParam1")%>		
		<br><hr>
		request.getParameter("qryParam1")(<font color=red>UTF-8编码->ISO8859-1解码</font>):<br>
		<%=qryParam1%>
		<br><hr>
		new String(request.getParameter("qryParam1").getBytes("iso8859-1"),"UTF-8")(<font color=red>UTF-8编码->ISO8859-1解码->ISO8859-1编码->UTF-8解码</font>):<br>
		<%=new String(request.getParameter("qryParam1").getBytes("iso8859-1"),"UTF-8")%>		
		
	</body>
</html>

 

提交时地址栏显示:

http://localhost:8080/HttpStream/utf-8rs.jsp?qryParam1=%E4%B8%ADa%20~!%40%23%24%25%5E%26*()_%2B%7B%7D%7C%3A%22%20%3C%3E%3F%60-%3D%5B%5D%5C%3B'%2C.%2F&qryParam2=%E4%B8%ADa%20~!%40%23%24%25%5E%26*()_%2B%7B%7D%7C%3A%5C%22%20%3C%3E%3F%60-%3D%5B%5D%5C%5C%3B'%2C.%2F

码流:

POST /utf-8rs.jsp?qryParam1=%E4%B8%ADa%20~!%40%23%24%25%5E%26*()_%2B%7B%7D%7C%3A%22%20%3C%3E%3F%60-%3D%5B%5D%5C%3B'%2C.%2F&qryParam2=%E4%B8%ADa%20~!%40%23%24%25%5E%26*()_%2B%7B%7D%7C%3A%5C%22%20%3C%3E%3F%60-%3D%5B%5D%5C%5C%3B'%2C.%2F HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, */*
Referer:
http://localhost:8080/HttpStream/utf-8.jsp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; aff-kingsoft-ciba)
Host: localhost:8088
Content-Length: 126
Connection: Keep-Alive
Cache-Control: no-cache

 

textParam1=%E4%B8%ADa+%7E%21@%23%24%25%5E%26*%28%29_%2B%7B%7D%7C%3A%5C%22+%3C%3E%3F%60-%3D%5B%5D%5C%5C%3B%27%2C.%2F&fileParam=

 

运行结果:

问题二

问题出现时环境:

1、以POST方式提交表单
2、请求的URL后还附加参数且参数值含有中文
3、附加参数值没有经过encodeURIComponent()函数编码
4、Tomcat未设置URIEncoding与useBodyEncodingForURI
5、请求页面与结果页面<%@ page %>指令的contentType与pageEncoding编码方式都为UTF-8
6、结果页面中request的编码方式为UTF-8

 

请求utf-8.jsp与响应页面utf-8rs.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<body>
		<form name=form1 action="" method="post">
			<input type="text" name="textParam1" size="50 px" value="中a ~!@#$%^&amp;*()_+{}|:\&quot; &lt;&gt;?`-=[]\\;',./">
			<br>
			<input type="file" name="fileParam" size="50 px" value="">
			<br>
			<input type="button" value="submit" onclick="submitForm()">
		</form>
		<script type="text/javascript"> 
			function submitForm(){ 
				var str ="中a ~!@#$%^&*()_+{}|:\" <>?`-=[]\\;',./"; 
				form1.action = "utf-8rs.jsp?qryParam1=" + (str) + "&qryParam2="+  (form1.textParam1.value) ; 
				//form1.action = "http://localhost:8088/utf-8rs.jsp?qryParam1=" + (str) + "&qryParam2="+  (form1.textParam1.value) ;
				form1.submit(); 
			} 
		</script>
	</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<head>
		<title>Insert title here</title>
	</head>
	<body>
		<%
			//这里一定要设置,且与浏览的提交时编码方式一样,否则乱码(默认Tomcat以iso8859-1解码) 
			//且只能post提交方式的参数起使用,对URL后附带参数不起作用 
			request.setCharacterEncoding("UTF-8");
			String textParam1 = request.getParameter("textParam1");
			String qryParam1 = request.getParameter("qryParam1");
			System.out.println("textParam1=" + textParam1);
			System.out.println("qryParam1=" + qryParam1);
			System.out.println("qryParam1=" + (qryParam1==null?null:new String(qryParam1.getBytes("iso8859-1"),"utf-8")));
		%>
		request.getParameter("textParam1")(<font color=red>UTF-8编码->UTF-8解码</font>):<br>
		<%=request.getParameter("textParam1")%>		
		<br><hr>
		request.getParameter("qryParam1")(<font color=red>UTF-8编码->ISO8859-1解码</font>):<br>
		<%=qryParam1%>
		<br><hr>
		new String(request.getParameter("qryParam1").getBytes("iso8859-1"),"UTF-8")(<font color=red>UTF-8编码->ISO8859-1解码->ISO8859-1编码->UTF-8解码</font>):<br>
		<%=new String(request.getParameter("qryParam1").getBytes("iso8859-1"),"UTF-8")%>		
		
	</body>
</html>

提交时地址栏显示:

http://localhost:8080/HttpStream/utf-8rs.jsp?qryParam1=中a%20~!@#$%^&*()_+{}|:"%20<>?`-=[]\;',./&qryParam2=中a%20~!@#$%^&*()_+{}|:\"%20<>?`-=[]\\;',./

码流:

 

POST /utf-8rs.jsp?qryParam1=中a%20~!@ HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, */*
Referer: http://localhost:8080/HttpStream/utf-8.jsp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; aff-kingsoft-ciba)
Host: localhost:8088
Content-Length: 126
Connection: Keep-Alive
Cache-Control: no-cache

 

textParam1=%E4%B8%ADa+%7E%21@%23%24%25%5E%26*%28%29_%2B%7B%7D%7C%3A%5C%22+%3C%3E%3F%60-%3D%5B%5D%5C%5C%3B%27%2C.%2F&fileParam=

 

运行结果:

问题三

问题出现时环境:

1、以get方式提交表单   
2、表单输入框中含有中文   
3、Tomcat未设置URIEncoding与useBodyEncodingForURI
4、请求页面与结果页面<%@ page %>指令的contentType与pageEncoding编码方式都为UTF-8
5、结果页面中request的编码方式为UTF-8

 

请求utf-8.jsp与响应页面utf-8rs.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<body>
		<form name=form1 action="" method="get">
			<input type="text" name="textParam1" size="50 px" value="中a ~!@#$%^&amp;*()_+{}|:\&quot; &lt;&gt;?`-=[]\\;',./">
			<br>
			<input type="file" name="fileParam" size="50 px" value="">
			<br>
			<input type="button" value="submit" onclick="submitForm()">
		</form>
		<script type="text/javascript"> 
			function submitForm(){ 
				var str ="中a ~!@#$%^&*()_+{}|:\" <>?`-=[]\\;',./"; 
				form1.action = "utf-8rs.jsp?qryParam1=" + encodeURIComponent(str) + "&qryParam2="+  encodeURIComponent(form1.textParam1.value) ; 
				//form1.action = "http://localhost:8088/utf-8rs.jsp?qryParam1=" + encodeURIComponent(str) + "&qryParam2="+  encodeURIComponent(form1.textParam1.value) ;
				form1.submit(); 
			} 
		</script>
	</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<head>
		<title>Insert title here</title>
	</head>
	<body>
		<%
			//这里一定要设置,且与浏览的提交时编码方式一样,否则乱码(默认Tomcat以iso8859-1解码) 
			//且只能post提交方式的参数起使用,对URL后附带参数不起作用 
			request.setCharacterEncoding("UTF-8");
			String textParam1 = request.getParameter("textParam1");
			String qryParam1 = request.getParameter("qryParam1");
			System.out.println("textParam1=" + textParam1);
			System.out.println("qryParam1=" + qryParam1);
			System.out.println("qryParam1=" + (qryParam1==null?null:new String(qryParam1.getBytes("iso8859-1"),"utf-8")));
		%>
		request.getParameter("textParam1")(<font color=red>UTF-8编码->UTF-8解码</font>):<br>
		<%=request.getParameter("textParam1")%>		
		<br><hr>
		request.getParameter("qryParam1")(<font color=red>UTF-8编码->ISO8859-1解码</font>):<br>
		<%=qryParam1%>
		<br><hr>
		new String(request.getParameter("qryParam1").getBytes("iso8859-1"),"UTF-8")(<font color=red>UTF-8编码->ISO8859-1解码->ISO8859-1编码->UTF-8解码</font>):<br>
		<%=new String(request.getParameter("qryParam1").getBytes("iso8859-1"),"UTF-8")%>		
		
	</body>
</html>

 

提交时地址栏显示:

http://localhost:8080/HttpStream/utf-8rs.jsp?textParam1=%E4%B8%ADa+%7E%21@%23%24%25%5E%26*%28%29_%2B%7B%7D%7C%3A%5C%22+%3C%3E%3F%60-%3D%5B%5D%5C%5C%3B%27%2C.%2F&fileParam=

码流:

GET /utf-8rs.jsp?textParam1=%E4%B8%ADa+%7E%21@%23%24%25%5E%26*%28%29_%2B%7B%7D%7C%3A%5C%22+%3C%3E%3F%60-%3D%5B%5D%5C%5C%3B%27%2C.%2F&fileParam= HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, */*
Referer: http://localhost:8080/HttpStream/utf-8.jsp
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; aff-kingsoft-ciba)
Host: localhost:8088
Connection: Keep-Alive

 

运行结果:

 

  • 大小: 9.5 KB
  • 大小: 8.1 KB
  • 大小: 8.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics