JSP九大内置对象

Object findAttribute(String name):依次在page、request、session、application范围查找名称为name的数据,如果找到就停止查找。这说明在这个范围内有相同名称的数据,那么page范围的优先级最高!

这样定义变量 session_aa

1 什么是JSP九大内置对象

在JSP中无需创建就可以使用的9个对象,它们是:

l  out(JspWriter):等同与response.getWriter(),用来向客户端发送文本数据;

l  config(ServletConfig):对应“真身”中的ServletConfig;

l  page(当前JSP的真身类型):当前JSP页面的“this”,即当前对象;

l  pageContext(PageContext):页面上下文对象,它是最后一个没讲的域对象;

l  exception(Throwable):只有在错误页面中可以使用这个对象;

l  request(HttpServletRequest):即HttpServletRequest类的对象;

l  response(HttpServletResponse):即HttpServletResponse类的对象;

l  application(ServletContext):即ServletContext类的对象;

l  session(HttpSession):即HttpSession类的对象,不是每个JSP页面中都可以使用,如果在某个JSP页面中设置<%@page session=”false”%>,说明这个页面不能使用session。

在这9个对象中有很多是极少会被使用的,例如:config、page、exception基本不会使用。

在这9个对象中有两个对象不是每个JSP页面都可以使用的:exception、session。

在这9个对象中有很多前面已经学过的对象:out、request、response、application、session、config。

2 通过“真身”来对照JSP

  我们知道JSP页面的内容出现在“真身”的_jspService()方法中,而在_jspService()方法开头部分已经创建了9大内置对象。

public void _jspService(HttpServletRequest request, HttpServletResponse response)[崔1]

throws java.io.IOException, ServletException {

PageContext pageContext = null;[崔2]

HttpSession session = null;[崔3]

ServletContext application = null;[崔4]

ServletConfig config = null;[崔5]

JspWriter out = null;[崔6]

Object page = this;[崔7]

JspWriter _jspx_out = null;

PageContext _jspx_page_context = null;

try {

response.setContentType("text/html;charset=UTF-8");

pageContext = _jspxFactory.getPageContext(this, request, response,

null, true, 8192, true);

_jspx_page_context = pageContext;

application = pageContext.getServletContext();

config = pageContext.getServletConfig();

session = pageContext.getSession();

out = pageContext.getOut();

_jspx_out = out;

    从这里开始,才是JSP页面的内容[崔8]

}…

3 pageContext对象

  在JavaWeb中一共四个域对象,其中Servlet中可以使用的是request、session、application三个对象,而在JSP中可以使用pageContext、request、session、application四个域对象。

pageContext 对象是PageContext类型,它的主要功能有:

l  域对象功能;

l  代理其它域对象功能;

l  获取其他内置对象;

3.1 域对象功能

  pageContext也是域对象,它的范围是当前页面。它的范围也是四个域对象中最小的!

l  void setAttribute(String name, Object value);

l  Object getAttrbiute(String name, Object value);

l  void removeAttribute(String name, Object value);

3.2 代理其它域对象功能

还可以使用pageContext来代理其它3个域对象的功能,也就是说可以使用pageContext向request、session、application对象中存取数据,例如:

pageContext.setAttribute("x", "X");[崔9]

pageContext.setAttribute("x", "XX", PageContext.REQUEST_SCOPE);[崔10]

pageContext.setAttribute("x", "XXX", PageContext.SESSION_SCOPE);[崔11]

pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE);[崔12]

l  void setAttribute(String name, Object value, int scope):在指定范围中添加数据;

l  Object getAttribute(String name, int scope):获取指定范围的数据;

l  void removeAttribute(String name, int scope):移除指定范围的数据;

l  Object findAttribute(String name):依次在page、request、session、application范围查找名称为name的数据,如果找到就停止查找。这说明在这个范围内有相同名称的数据,那么page范围的优先级最高!

3.3 获取其他内置对象

一个pageContext对象等于所有内置对象,即1个当9个。这是因为可以使用pageContext对象获取其它8个内置对象:

l  JspWriter getOut():获取out内置对象;

l  ServletConfig getServletConfig():获取config内置对象;

l  Object getPage():获取page内置对象;

l  ServletRequest getRequest():获取request内置对象;

l  ServletResponse getResponse():获取response内置对象;

l  HttpSession getSession():获取session内置对象;

l  ServletContext getServletContext():获取application内置对象;

l  Exception getException():获取exception内置对象;


[崔1]request和response两个对象是方法参数。

[崔2]pageContext对象

[崔3]session对象

[崔4]application对象

[崔5]config对象

[崔6]out对象

[崔7]page对象

[崔8]JSP页面的内容从这里才开始,所以可以使用上面定义的变量!这就是9大内置对象为什么可以在JSP页面中无需创建就可以使用的奥秘了!

[崔9]向pageContext中存储数据

[崔10]向request中存储数据

[崔11]向session中存储数据

[崔12]向application中存储数据

<%@page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
errorPage="error.jsp"%>
<!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>
<%
//int a = 10/0;
/* if(true){
throw new RuntimeException("出错了");
} */
pageContext.setAttribute("aaa", "AAA");
out.print(pageContext.getAttribute("aaa"));
pageContext.removeAttribute("aaa"); pageContext.setAttribute("bbb", "BB",PageContext.REQUEST_SCOPE);
out.print(request.getAttribute("bbb"));
out.print(pageContext.getRequest().getAttribute("bbb")); pageContext.setAttribute("cc", "CC",PageContext.SESSION_SCOPE);
out.print((String)request.getSession().getAttribute("cc"));
out.print(pageContext.getSession().getAttribute("cc")); pageContext.setAttribute("dd", "dd",pageContext.APPLICATION_SCOPE);
out.print(request.getServletContext().getAttribute("dd"));
out.print(pageContext.getServletContext().getAttribute("dd")); out.print(pageContext.findAttribute("dd")+" d");
%>
</body>
</html>

  

JSP基础--九大内置对象的更多相关文章

  1. 今天我们来认识一下JSP的九大内置对象

    虽然现在基本上我们都是使用SpringMVC+AJAX进行开发了Java Web了,但是还是很有必要了解一下JSP的九大内置对象的.像request.response.session这些对象,即便使用 ...

  2. servlet的三大作用域对象和jsp的九大内置对象及其四大作用域对象

    servlet的三大作用域对象: request(HttpServletRequest) session(HttpSession): application(ServletContext):tomca ...

  3. jsp学习——九大内置对象

    JSP一共有九个内置对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 博客:JSP的九 ...

  4. jsp的九大内置对象

    九大内置对象 jsp servlet   对象名 类型 使用范围 request HttpServletRequest 请求 浏览器--->服务器 response HttpServletRes ...

  5. jsp页面九大内置对象

    资源转载自网上,不可用于商用,学习可以.内置对象又叫隐式对象/隐含对象是由WEB容器加载的一组类的实例,不需要预先声明就可以在脚本代码和表达式中随意使用的对象. 这九大隐式对象可以按照期作用分类为: ...

  6. jsp的九大内置对象及EL表达式的隐含对象

    九大内置对象: request         request对象具有请求域,即完成客户端的请求之前,该对象一直有效. response       response对象具有页面作用域,即访问一个页面 ...

  7. jsp(九大内置对象,三大指令)

    九大内置对象JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.except ...

  8. jsp的九大内置对象和四大作用域(转)

    定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量 JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应): 1.request对象(作用域)  客户端的 ...

  9. jsp的九大内置对象和四大作用域

    定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量? JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应):? 1.request对象(作用域)? 客户 ...

随机推荐

  1. Windows下的vue-devtools工具的安装

    详细教程在这个链接里: https://www.cnblogs.com/xqmyhome/p/10972772.html

  2. 攻防世界--srm-50

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/6df7b29f8f18437887ff4be163b567d5.exe 1.准备 获取 ...

  3. IIS环境下PHP版本过低无法Sql查询的解决

    需求:帝国后台添加个后台框,输入地址,原页面重写成所指链接页面 重点:当输入框输入地址,提交到后台后,打开原链接,该页面会读取php文件GetUrlPage.php <?php header(& ...

  4. HTML替换元素,非替换元素和控制元素

    替换元素:元素内容由标签的属性来设置,标签其实就是一个占位符.替换元素因为元素内容来自外部资源,所以这些标签大多具有src,指明要引入的资源路径,所以大多仅需要一个标签就可以.例如:<link ...

  5. SQLyog安装

    安装 使用 首先看到下面的界面

  6. vue实现搜索功能

    vue实现搜索功能 template 部分 <!-- 搜索页面 --> <template> <div> <div class="goback&qu ...

  7. maven整合S2SH

    1.pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.ap ...

  8. 利用JFreeChart生成简单柱状图(Java)

    package barchartdemo1; import <a href="http://lib.csdn.net/base/javaee" class='replace_ ...

  9. 记录cobbler报错

    出现下面这个错误解决方法 httpd does not appear to be running and proxying cobbler, or SELinux is in the way. Ori ...

  10. Java中最基本的集合接口:初识Collection

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements). 一些 Collection允许相同的 ...