阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

JSP九大内置对象

95次阅读
没有评论

共计 2839 个字符,预计需要花费 8 分钟才能阅读完成。

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) 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 页面的内容 }…

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"); pageContext.setAttribute("x", "XX", PageContext.REQUEST_SCOPE); pageContext.setAttribute("x", "XXX", PageContext.SESSION_SCOPE); pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE);

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 内置对象;

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计2839字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中