-------------------JSP内置对象-------------------------------
JSP内置对象
- 1 什么是内置对象(面试常问)
内置对象是在JSP页面中无需创建就可以直接使用的变量。在JSP中一共有9个这样的对象!它们分别是:
- out(JspWriter);
- config(ServletConfig);
- page(当前JSP的真身类型);
- pageContext(PageContext);
- exception(Throwable);
- request(HttpServletRequest);
- response(HttpServletResponse);
- application(ServletContext);
- Session (HttpSession)。
- 2 隐藏对象概述
- out:最为常用的方法是print(),向页面输出!它与response.getWriter()基本相同!
- config:在页面中基本没有什么用,但如果JSP在web.xml中存在配置,而且存在初始化参数,那么可以使用config来获取;config对象的功能是:getServletContext()、getServletName()、getInitParameter(),这三个方法在JSP中都没什么用。所以config就没什么用了。JSP也可以在web.xml中配置,但我们没有配置它的必要!
- page:基本没用!表示当前JSP页面的当前实例!在页面中使用this和使用page是相同的;也没什么用。
- request:与Servlet中的request一样,没有区别;
- response:与Servlet中的response一样,没有区别;
- application:就是ServletContext对象;
- session:就是HttpSession对象;
- exception:虽然名字叫exception,但类型为Throwable。它只能在错误页中可以使用!后面讲
- pageContext:很有用的东西,下面会讲解。你以后可能不会经常直接使用它,但一定间接使用它
- 3 对照JSP真身查看内置对象
我们发现,在JSP中的内容会出现在“真身”的_jspService()方法中,而且在_jspService()方法上方还有一些其实代码:
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {
PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; 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页面的内容 }… |
|
JSP中的内容都出现在try块中,而且在try块的正文!上方是对隐藏对象的初始化!!!
上面代码只是给出普通页面的“真身”,而错误页面的“真身”你会看到exception对象的出现。
- 4 JSP四个域对象
-
- 域的范围和属性管理
-
- pageContext范围; 当前页面之内有效
|
-
- request范围;当前的请求内有效
|
-
- session范围;当前的会话内有效
|
-
- application范围;当前这次服务器生命周期内有效
|
域对象的共同特点是都管理域中的属性:
他们有着相同的方法
- void setAttribute(String name, Object value);
- Object getAttrbiute(String name, Object value);
- void removeAttribute(String name, Object value);
-
- pageContext详细分析
pageContext 对象是PageContext类型,它不只是域对象,而且还可以操作所有域对象,还可以获取其他隐藏对象。
- 本身是域对象:pageContext是JSP中的域对象,而在Servlet中不能使用它!它表示的当前页面中可以使用,是最小的范围!
- void setAttribute(String name, Object value);
- Object getAttrbiute(String name, Object value);
- void removeAttribute(String name, Object value);
- 操作所有域(四个域):可以使用pageContext对象操作所有域对象,在getAttribute()、setAttribute()、removeAttribute()三个方法中多添加一个参数,int scope来指定范围。在PageContext类中包含四个int类型的常量表示四个范围:
- PAGE_SCOPE:pageContext范围;
- REQUEST_SCOPE:request范围;
- SESSION_SCOPE:session范围;
- APPLICATION_SCOPE:application范围;
- void setAttribute(String name, Object value, int scope):设置数据到指定的范围中,例如:pageContext.setAttribute(“hello”, “hello world”, PageContext.REQUEST),这个方法调用等同与:request.setAttribute(“hello”, “hello world”);
- Object getAttribute(String name, int scope):获取指定范围的数据;
- void removeAttribute(String name, int scope):移除指定范围的数据;
- Object findAttribute(String name):在所有范围内查找数据,依次为page、request、session、application。如果在前一个范围内查找到数据,那么就返回,而不会在到下一个范围中去查找!
- 获取其他隐藏对象:可以使用pageContext获取其他隐藏对象。
- JspWriter getOut():获取out隐藏对象;
- ServletConfig getServletConfig():获取config隐藏对象;
- Object getPage():获取page隐藏对象;
- HttpServletRequest getRequest():获取request隐藏对象;
- HttpServletResponse getResponse:获取response隐藏对象;
- HttpSession getSession():获取session隐藏对象;
- ServletContext getServletContext():获取application隐藏对象;
- JspException getException():获取exception隐藏对象转换后的JspException对象。
<body> <% //设置pageContext属性的作用范围为request pageContext.setAttribute("name", "李昆鹏",PageContext.REQUEST_SCOPE); pageContext.setAttribute("birthday", new Date(),PageContext.REQUEST_SCOPE);
String name=(String)pageContext.getAttribute("name",PageContext.REQUEST_SCOPE); Date birthday=(Date)pageContext.getAttribute("birthday",PageContext.REQUEST_SCOPE);
//获得项目名,比较常用 String path1 = pageContext.getServletContext().getContextPath(); // /jsp_demo1 %> <h1>name: <%=name %></h1> <h1>birthday: <%=birthday %></h1> </body> |