HttpServletRequest应用——获取请求参数与通过Request对象传递数据

1.获取请求参数

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

ServletRequest获取请求参数的方法:

方法声明功能描述
String getParameter(String name)该方法用于获取某个指定名称的参数值,如果请求消息中没有包含指定名称的参数,getParameter()方法返回null;如果指定名称的参数存在但没有设置值,则返回一个空串;如果请求消息中包含有多个该指定名称的参数,getParameter()方法返回第一个出现的参数值
String[] getParameterValues(String name)HTTP请求消息中可以有多个相同名称的参数(通常由一个包含有多个同名的字段元素的FORM表单生成),如果要获得HTTP请求消息中的同一个参数名所对应的所有参数值,那么就应该使用getParameterValues()方法,该方法用于返回一个String类型的数组
Enumeration getParameterNames()该方法用于返回一个包含请求消息中所有参数名的Enumeration对象,在此基础上,可以对请求消息中的所有参数进行遍历处理
Map getParameterMap()getParameter Map()方法用于将请求消息中的所有参数名和值装入进一个Map对象中返回

实践案例

(1)创建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="GET">
 	 	用户名:<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>

其中参数名为“hobby”的值有多个

(2)创建Servlet类RequestParamsServlet

public class RequestParamsServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	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);
	}
}

注:其中

request.setCharacterEncoding("utf-8");

只对post方式有效,对于get方式,还需要在上述方法下先使用错误码表ISO-8859-1将用户名重新编码,再使用码表UTF-8进行解码。

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

(3)运行结果

form运行界面
在这里插入图片描述

控制台输出信息
在这里插入图片描述

2.通过Request对象传递数据

Request对象不仅可以获取一系列数据,还可以通过属性传递数据。在ServletRequest接口中,定义了一系列操作属性的方法:

方法声明功能描述
void setAttribute(String name,Object o)该方法用于将一个对象与一个名称关联后存储进ServletRequest对象中。需要注意的是,如果ServletRequest对象中已经存在指定名称的属性,setAttribute()方法将会删除原来的属性,然后再添加新的属性(如果传递给setAttribute()方法的属性值对象为null,则删除指定名称的属性,等同与removeAttribute()方法)。
void removeAttribute(String name)该方法用于从ServletReques对象中删除指定名称的属性
Object getAttribute(String name)该方法用于从ServletRequest对象中返回指定名称的属性对象
Enumeration getAttributeNames(String name)该方法用于返回一个包含ServletRequest对象中的所有属性名的Enumeration对象,在此基础上可以对ServletRequest对象中的所有属性进行遍历处理。

需要注意的是:

只有属于同一个请求中的数据才可以通过ServletRequest对象传递数据。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心醉瑶瑾前

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值