您的位置:首页 > Web前端 > JavaScript

jsp学习(4)---jsp9个内置对象

2017-04-18 16:52 169 查看

一、基本内容

1.九大内置对象主要内容

对象名类型说明
requestjavax.servlet.http.HttpServletRequest 
responsejavax.servlet.http.HttpServletResponse 
session
javax.servlet.http.HttpSession由session="true"开关
applicationjavax.servlet.ServletContext 
exceptionjava.lang.Throwable由isErrorPage="false"开关
pagejava.lang.Object当前对象this当前servlet实例
configjavax.servlet.ServletConfig 
outjavax.servlet.jsp.JspWriter字符输出流,相当于 printWriter对象
pageContextjavax.servlet.jsp.PageContext 
以上的重点内容是:request、response、applocation、pageContext等

2.pageContext的基本使用说明

(1)基本方法

void setAttribute(String name,Object o); //用来创建域对象
Object getAttribute(String name); //用来获取域对象
void removeAttribute(String name); //移除域对象
(2)操作其他域对象

pageContext作为一个域对象的同时可以操作其他3域对象(request、response、application)数据

void setAttribute(String name,Object o,int Scope);
Object getAttribute(String name,int Scope);
voidremoveAttribute(String name,int Scope);
scpoe的值:

PageContext.PAGE_SCOPE

PageContext.REQUEST_SCOPE

PageContext.SESSION_SCOPE

PageContext.APPLICATION_SCOPE



二、request、response、application、pageContext域对象的作用范围

1.pageContext作用范围仅在当前页面

<%@ 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>
<%
pageContext.setAttribute("p", "pageContext");
/* 等价于下面内容 */
//pageContext.setAttribute("p", "one",PageContext.PAGE_SCOPE);
%>

<%=pageContext.getAttribute("p",PageContext.PAGE_SCOPE)
%>
</body>
</html>


访问该界面为:



进行请求装发到6.jsp

在5.jsp页面中
<body>
<%
pageContext.setAttribute("p", "pageContext");
/* 等价于下面内容 */
//pageContext.setAttribute("p", "one",PageContext.PAGE_SCOPE);
request.getRequestDispatcher("/6.jsp").forward(request, response);
%>
</body>


在6.jsp页面中
<body>
<%
String p = (String)pageContext.getAttribute("p");
out.print(p);
%>
</body>


得到的结果为:



结论:pageContext的作用范围仅在当前界面

2.request、Session、application作用范围比较

request的作用范围:存放的数据在一次请求(转发)内有效。使用非常多
session的作用范围:存放的数据在一次会话中有效。使用的比较多。如:存放用户的登录信息,购物车功能。
application作用范围:存放的数据在整个应用范围内都有效,因为范围太大,应尽量少

在5.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>
<%
pageContext.setAttribute("p", "request", PageContext.REQUEST_SCOPE);
/* 等价于下面内容 */
//request.setAttribute("p", "request");

pageContext.setAttribute("p", "session", PageContext.SESSION_SCOPE);
/* 等价于下面内容 */
//session.setAttribute("p", "session");

pageContext.setAttribute("p", "application", PageContext.APPLICATION_SCOPE);
/* 等价于下面内容 */
//application.setAttribute("p", "request");

//request.getRequestDispatcher("/6.jsp").forward(request, response);
response.sendRedirect(request.getContextPath()+"/6.jsp");
%>
</body>
</html>


在6.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>
<%
out.print(request.getAttribute("p"));
%>

<%=session.getAttribute("p") %>

<%=application.getAttribute("p") %>

</body>
</html>


得到的结果是:



结论:request、Session、application的作用范围为:application>session>request

三、PageContext的其他用法

findAttribute(String name); 自动从page request sessionapplication依次查找,找到了就取值,结束查找。

修改6.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>

<%=pageContext.findAttribute("p") %>
</body>
</html>


得到的结果为:



四、总结
以上便是对jsp九大内置对象的总结,如果中间有任何错误请提出,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息