04 jsp 内置对象+域对象

引入:
在jsp开发有些对象使用频率比较高,(例如:requesst,respone,session,servletcontext。。。)如果每次要使用这些对象都自己去获取对象才能使用,会显示太麻烦了。jsp把这些对象都创建或获取好了,我们开发者之间使用这些对象即可!!!这些对象就叫做内置对象

九大内置对象:

jsp对象名 类型
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
exception Throwable
page Object
out JspWriter
pageContext PageContext

out对象:
我们都知道response.getWriter().write()可以往浏览器中写入实体内容;那么out对象的writer()方法与如上方法有什么不同呢?

04 jsp 内置对象+域对象_第1张图片
Paste_Image.png
04 jsp 内置对象+域对象_第2张图片
Paste_Image.png
Paste_Image.png

pageContext对象:


    <%--可以获取其他内置对象 --%>
    <%
    //pageContext.getResponse().getWriter().write("来啊,爱情啊");
    %>
    
    <%--可以作为域对象 --%>
    <%
//  pageContext.setAttribute("name", "丁昌江");
//  out.write((String)pageContext.getAttribute("name"));
    %>
    
    <%--可以保存数据到其他域对象(request,session,application)
    注意:在哪个域对象中存储数据就应该在哪个域对象中读取数据
     --%>
    <%
    pageContext.setAttribute("name", "request-丁昌江",pageContext.REQUEST_SCOPE);
    //out.write((String)pageContext.getAttribute("name",pageContext.REQUEST_SCOPE));
    out.write((String)request.getAttribute("name"));
    %>

你可能感兴趣的:(04 jsp 内置对象+域对象)