包括
与Input/Ouput(输入/输出)有关的隐含对象包括:request对象、response对象、out对象,这些对象主要作为客户端和服务器间通信的桥梁。
(1)request对象表示客户端对服务器端发送的请求
(2)response对象表示服务器对客户端的响应
(3)out对象负责把处理结果输出到客户端
request对象
介绍
1、resquest对象即请求对象,主要用于接受客户端通过HTTP协议传送给服务端的数据
2、request对象类型为javax.servlet.http.HttopServletRequest,与Servlet中的请求对象为同一对象
3、request对象的作用域为一次request请求
接口方法
1、setCharacterEncoding(String charset) 设置请求体参数的解码字符集
2、getParameter(String name) 根据参数名获取单一参数值
扫描二维码关注公众号,回复:
2431242 查看本文章
3、getParameterValues(String name) 根据参数名获取一组参数值
4、setAttribute(String name,Object value) 以名/值得方式存储请求域属性
5、getAttribute(String name) 根据属性名获取存储的对象数据
例子
通过一个用户登陆功能,演示request对象获取请求参数方法的使用和对提交数据进行验证
login.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="loginParameter.jsp" method="post">
<p>用户名:<input name="username" type="text"/></p>
<p>密 码:<input name="password" type="password"/></p>
<p><input name="submit" type="submit" value="登陆">
</form>
</body>
</html>
loginValidate.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>
<%
//设置POST请求编码
request.setCharacterEncoding("UTF-8");
//获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
out.print(username);
out.print(password);
StringBuffer errorMsg = new StringBuffer();
//参数信息验证
if("".equals(username)){
errorMsg.append("用户名不能为空!<br>");
}
if("".equals(password)){
errorMsg.append("密码不能为空<br>");
}else if(password.length() < 6 || password.length() > 12)
errorMsg.append("密码长度在6-12位之间,<br>");
//将错误信息爆保存在请求域属性errorMsg中
request.setAttribute("errorMsg", errorMsg.toString());
if(errorMsg.toString().equals("")){
out.println(username + ",您的登陆身份验证成功!");
}else{
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
%>
</body>
</html>
显示页面