HttpServletRequest应用-获取请求参数及解决中文乱码

一、介绍

在实际开发中,经常需要获取用户提交的表单数据,例如,用户名、密码、电子邮件等,为了方便获取表单中的请求参数,在HttpServletRequest接口的父类ServletRequest中,定义了一系列获取请求参数的方法,如表所示。

 二、编写表单文件form.html

<!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="/chapter04/RequestParamsServlet" method="POST">
 	 	用户名:<input type="text" name="username"><br>
 	 	密  &nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
 	 	爱好:
 	 	<input type="checkbox" name="hobby" value="sing">唱歌
 	 	<input type="checkbox" name="hobby" value="dance">跳舞
 	 	<input type="checkbox" name="hobby" value="football">足球<br>
 	 	<input type="submit" value="提交">
 	</form>
</body>
</html>

三、创建RequestParamsServlet

public class RequestParamsServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,
        HttpServletResponse response)throws ServletException, IOException {
		//设置request对象的解码方式
		//request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		//name=new String(name.getBytes("iso8859-1"),"utf-8");
		String password = request.getParameter("password");
			System.out.println("用户名:" + name);
			System.out.println("密  码:" + password);
			// 获取参数名为“hobby”的值
			String[] hobbys = request.getParameterValues("hobby");
			System.out.print("爱好:");
			for (int i = 0; i < hobbys.length; i++) {
				System.out.print(hobbys[i] + ",");
			}
		}
		public void doPost(HttpServletRequest request,
	        HttpServletResponse response)throws ServletException, IOException {
			doGet(request, response);
		}
	}

四、运行

http://localhost:8080/chapter04/form.html

五、解决post提交的中文乱码

乱码原因:form.html的<meta http-equiv="Content-Type" content="text/html; charset=utf-8">,告诉浏览器使用utf-8对传递的请求参数编码,但在HttpServletRequest中默认使用iso-8859-1解码,所以设置HttpServletRequest的解码方式为utf-8

request.setCharacterEncoding("utf-8");

 六、解决get方式提交的中文乱码

将form.html中的method属性的值改为“get”,来做测试

只要在表单页面的head标签中写上<meta http-equiv="Content-Type" content="text/html; charset=utf-8">就好了,request对象默认采取utf-8解码查询字符串

分析如下:

在浏览器方面,如果表单页面存在<meta http-equiv="Content-Type" content="text/html; charset=utf-8">,那么对提交信息进行utf-8编码,如果charset 不是utf-8,使用默认字符集编码。

在Servlet中request.setCharacterEncoding("utf-8")对get方式提交的数据无效,对地址栏发过来的参数(也就是查询字符串,因为一般不在地址栏提交表单参数,但会传查询参数)单独处理,默认采用utf-8解码,通过调试和搜索可以发现,如下图

如果上面的这些类版本比较低时,估计会出现乱码,因为可能会用ISO-8859-1解码,我也不再分析了,直接给出代码解决。

name=new String(name.getBytes("iso8859-1"),"utf-8");

 也可以通过配置tomcat解决,在server.xml中配置useBodyEncodingForURI="true",如下

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" 
    redirectPort="8443" useBodyEncodingForURI="true"
/>

并在程序中调用request.setCharacterEncoding("utf-8"),只作为了解,所以不再去证实。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值