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

一、基本内容

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九大内置对象的总结,如果中间有任何错误请提出,谢谢!



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值