实战第六弹:深入理解JSP内置对象(第一部分)
JSP 内置对象
- 了解和掌握 JSP 的常用内置对象。包括 request 对象、response 对象、session 对象、out 对象、pageContext 对象、exception 对象和 application 对象。
1.request 对象
- JSP 提供了一些由 JSP 容器实现和管理的内置对象,在 JSP 应用程序中不需要预先声明和创建这些对象就能直接使用。JSP 程序人员不需要对这些内部对象进行实例化,只需调用其方法就能实现特定的功能,使 Java Web 编程更加快捷、方便。
(1)request 对象介绍
- request 对象可获取通过 HTTP 协议传送到客户端的信息。当客户端通过 HTTP 协议请求一个 JSP 页面时,服务器端就会创建 request 对象并将客户端请求的信息封装到 request 对象中;当请求处理完该 request 对象将自动销毁。可以通过 request 对象提供的方法对保存在该对象中的数据进行操作。
- request 对象提供很多方法,常用的方法如下:
方法 | 作用 |
---|---|
getParameter(String name) | 用于获取表单提交的信息,以字符串的形式返回客户端传来的某一个请求参数的值,该参数名由 name 指定。当传递给此方法的参数名没有实际参数与之对应时,返回 null |
getParameterNames() | 用于获取客户端传送给服务端的所有参数值,其结果是一个 Enumeration(枚举) 实例 |
getCharacterEncoding() | 用于返回客户端请求中的字符编码方式 |
getContentLength() | 用于以字节为单位返回客户端请求大小。如果无法得到该请求的大小,则返回 -1 |
getHeader(String name) | 用于获取 HTTP 协议定义的文件头信息中指定的名字的值 |
getHeaderNames() | 用于返回 HTTP 协议所有文件头信息,其结果是一个 Enumeration(枚举) 实例 |
getMethod() | 用于获取客户端向服务端传送数据的方法,如 get、post 等方法 |
getProtocol() | 用于获取客户端向服务端传送数据所使用的协议名称 |
getRequestURL() | 用于获取客户端的 URL 地址 |
getRemoteAddr() | 用于获取客户端的 IP 地址 |
getRemoteHost() | 用于获取客户端的主机名字 |
getServerName() | 用于获取服务端的主机名字 |
getServerPort() | 用于获取服务端的端口号 |
removeAttribute(String name) | 用于删除请求中的一个属性 |
setAttribute(String name,Object obj) | 用于为 request 对象设置属性值 |
getAttribute(String name) | 用于返回 name 指定 request 对象的属性值,若不存在指定的属性,就返回 null |
getAttributeNames() | 用于返回 request 对象保持的所有属性值,其结果是一个 Enumeration(枚举) 实例 |
(2)request 对象实训
- 通过三个实训来了解和掌握 request 对象常用方法的使用
【实训一】
本实训使用 request 对象的 getParameter() 方法获取通过 HTTP 协议穿送过来的客户端的信息。实训包含两个文件:request1.jsp(主页面)和 requestCheck1.jsp(处理主页面数据的页面)。在 request1.jsp 页面中输入数据并单击提交按钮后,请求提交到 requestCheck.jsp,该页面对提交的数据进行处理,并输出结果。
- request1.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>request 对象实训1——页面</title>
</head>
<body bgcolor = "CCCFFF">
<div align = "center">
<form method = "post" action = "requestCheck1.jsp">
<table border = "2" bgcolor = "pink">
<tr>
<td>请输入你的姓名:</td>
<td><input type = "text" name = "userName"/></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type = "submit" name = "submit" value = "提交"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
- 主页面如下:
- requestCheck1.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>request 对象实训1——数据处理页面</title>
</head>
<body bgcolor = "CCCFFF">
<%
String name = request.getParameter("userName");
// 把 name 的值转换为标准字节
byte n[] = name.getBytes("ISO-8859-1");
// 把字节转换为 UTF-8 编码的字符串,解决中文乱码问题
name = new String(n,"UTF-8");
String buttonName = request.getParameter("submit");
byte bn[] = buttonName.getBytes("ISO-8859-1");
buttonName = new String(bn,"UTF-8");
%>
获取到客户端输入的用户名和按钮信息如下:
<hr noshade>
欢迎你:<%=name %>
<hr noshade>
点击的是:<%=buttonName %>按钮
<hr noshade>
</body>
</html>
- 数据处理页面:
【实训二】
本实训模拟在线考试系统,包含两个文件:request2.jsp(主页面)和 requestCheck2.jsp(数据处理页面)。主页面是单选题页面,题目答完后点击“考试完成”按钮,页面请求提交到数据处理页面,该页面对提交的数据进行处理,并将处理的数据即本次测试成绩输出。
- request2.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>request 对象实训2——考试系统主页面</title>
</head>
<body bgcolor = "CCCFFF">
<h2 align = "center">第六章测试题</h2>
<hr>
<form action = "requestCheck2.jsp" method = "post">
1. request 对象的 setHeader(String name,String value)方法的作用是()。
<br>
<input type = "radio" name = "1" value = "A">
添加 HTTP 文件头
<br>
<input type = "radio" name = "1" value = "B">
设定指定名字的 HTTP 文件头的值
<br>
<input type = "radio" name = "1" value = "C">
判断指定名字的 HTTP 文件头是否存在
<br>
<input type = "radio" name = "1" value = "D">
向客户端发送错误信息
<br>
2. 设置 session 的有效时间(也叫超时时间)的方式是()。
<br>
<input type = "radio" name = "2" value = "A">
setMaxInactiveInterval(int interval)
<br>
<input type = "radio" name = "2" value = "B">
getAttributrName()
<br>
<input type = "radio" name = "2" value = "C">
setAttributeName(String name,Java.lang.Object value)
<br>
<input type = "radio" name = "2" value = "D">
getLastAccessedTime()
<br>
3. 能清除缓冲区中的数据,并且把数据输出到客户端的 out 对象中的方法是()。
<br>
<input type = "radio" name = "3" value = "A">
out.newLine()
<br>
<input type = "radio" name = "3" value = "B">
out.clear()
<br>
<input type = "radio" name = "3" value = "C">
out.flush()
<br>
<input type = "radio" name = "3" value = "D">
out.clearBuffer()
<br>
4. pageContext 对象的 findAttribute()方法的作用是()。
<br>
<input type = "radio" name = "4" value = "A">
用来设置默认页面的范围或指定范围之中已命名的对象
<br>
<input type = "radio" name = "4" value = "B">
用来删除默认页面的范围或指定范围之中已命名的对象
<br>
<input type = "radio" name = "4" value = "C">
按照页面请求、会话以及应用程序范围的顺序实现对某个已命名属性的搜索
<br>
<input type = "radio" name = "4" value = "D">
以字符串的形式返回一个对异常的描述
<br><br>
<input type = "submit" value = "考试完成">
</form>
</body>
</html>
- 考试系统主页面:
- requestCheck2.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>request 对象实训2——考试系统数据处理页面</title>
</head>
<body bgcolor = "CCCFFF">
<%
int examResults = 0;
String str1 = request.getParameter("1");
String str2 = request.getParameter("2");
String str3 = request.getParameter("3");
String str4 = request.getParameter("4");
if(str1 == null){
str1 = " ";
}
if(str2 == null){
str2 = " ";
}
if(str3 == null){
str3 = " ";
}
if(str4 == null){
str4 = " ";
}
if(str1.equals("B")){
examResults ++;
}
if(str2.equals("A")){
examResults ++;
}
if(str3.equals("D")){
examResults ++;
}
if(str4.equals("C")){
examResults ++;
}
%>
<h2 align = "center">本次测试成绩是:</h2>
<p align = "center"><%=examResults*25 %>分</P>
</body>
</html>
- 考试系统数据处理页面:
【实训三】
本实训综合使用了 request 对象中的13个方法
- request3.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import = "java.util.Enumeration" %>
<!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>request 对象实训三</title>
</head>
<body bgcolor = "CCCFFF">
<%
request.setAttribute("name", "有一种爱叫放手");
request.setAttribute("password", "666666");
request.setAttribute("mail","wangxing@163.com");
request.removeAttribute("password");
Enumeration e = request.getAttributeNames();
String attrName;
while(e.hasMoreElements()){
attrName = e.nextElement().toString();
out.print(attrName + " = " + request.getAttribute(attrName) + "<br>");
}
%>
<hr>
编码方式:<%= request.getCharacterEncoding() %>
<br>
请求大小:<%=request.getContentLength() %>
<br>
传送数据方法:<%=request.getMethod() %>
<br>
协议:<%=request.getProtocol() %>
<br>
客户端地址:<%=request.getRemoteAddr() %>
<br>
客户端名称:<%=request.getRemoteHost() %>
<br>
客户端端口:<%=request.getRemotePort() %>
<br>
服务端名称:<%=request.getServerName() %>
<br>
服务端端口:<%=request.getServerPort() %>
</body>
</html>
- 页面:
2.response 对象
- 当用户访问一个服务器页面时,会通过 HTTP 协议提交请求,服务器获取到请求后返回一个 HTTP 响应。response 对象将服务器端发送到客户端的数据保存起来,并发送到客户端以响应客户的请求。
(1)response 对象介绍
- response 对象用于向客户段发送数据,用户可以使用该对象将服务器的数据已 HTML 格式发送给客户端。它与 request 组成了一对接收、发送数据的对象,这也是实现动态页面的基础。当服务器向客户端发送数据时,将把数据信息封装到 response 对象;当请求完成后 response 对象就会自动销毁。可以通过 response 对象提供的方法对保存在该对象中的数据进行操作。
- response 对象提供很多方法,常用方法如下:
方法 | 作用 |
---|---|
addCookie(Cookie cook) | 用于给用户添加一个 cookie 对象,保存客户端的相关信息 |
addHeader(String name,String value) | 用于添加带有指定名称和字符串的 HTTP 文件头信息,该 Header 信息将传送到客户端,如果不存在就添加,存在就覆盖 |
flushBuffer() | 用于强制把当前缓冲区的内容发送到客户端 |
getBufferSize() | 用于获取缓冲区的实际大小,如果未使用缓存区则返回 0 |
getCharacterEncoding() | 用于获取响应的字符编码方式 |
sendError() | 用于向客户端发送错误信息,如404指网页找不到错误 |
sendRedirect | 用于重新定向客户端的请求 |
setCharacterEncoding() | 用于设置响应的字符编码方式 |
setContentLength() | 用于设置响应内容的长度(字节数) |
setHeader() | 用于设置指定名称和字符串的 HTTP 文件头信息,该 Header 信息将传送到客户端,如果不存在就添加,存在就设置 |
(2)response 对象实训
- 通过两个实训来了解和掌握 response 对象的常用方法的使用
【实训一】
本实训的功能是实现页面定时刷新,每隔1秒刷新一次,服务器就重新执行一次该程序,产生新的当前时间,然后输出到客户端。当希望获取实时信息时就可以使用该功能,如网上的聊天室、论坛、股票信息等。
- response.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import = "java.util.Date" %>
<!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>response 对象实训1——页面刷新功能</title>
</head>
<body>
<h3>页面 1s 将刷新 1 次,你看到的某一时刻时间如下</h3>
<hr>
<%=new Date() %>
<%
response.setHeader("refresh", "1"); // 每隔 1s 重新对 refresh 赋值
%>
<hr>
</body>
</html>
- 页面:
【实训二】
本实训包含两个文件:response2.jsp 和 responseCheck2.jsp。response2.jsp 页面综合使用了 response 对象的多个方法。在该页面单击“确定”按钮后页面请求提交到 responseCheck2.jsp,该页面根据选择的数据调用 sendRedirect() 方法进行页面重定位。
- response2.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>response 对象实训2——主页面</title>
</head>
<body>
<hr>
<%
response.setBufferSize(10240);
%>
缓存大小:<%=response.getBufferSize() %>
<hr>
<%
response.setCharacterEncoding("UTF-8");
%>
编码方式:<%=response.getCharacterEncoding() %>
<hr>
网站友情链接:
<hr>
<form action = "responseCheck2.jsp" method = "post">
<select name = "link">
<option value = "moe" selected>*教育部</option>
<option value = "tup">清华大学出版社</option>
</select>
<input type = "submit" name = "submit" value = "确定">
</form>
</body>
</html>
- 主页面:
- responseCheck2.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>response 对象实训2——数据处理页面</title>
</head>
<body>
<%
String address = request.getParameter("link");
if(address != null){
if(address.equals("tup")){
response.sendRedirect("http://www.tup.tsinghua.edu.cn/index.html");
}
else{
response.sendRedirect("http://www.moe.gov.cn/");
}
}
%>
</body>
</html>