JSP中page和pageContext的区别
来源:互联网 发布:淘宝几个好评一颗心 编辑:程序博客网 时间:2024/05/16 04:12
page java.lang.Object 对应this关键字。JSP网页本身,page对象是当前页面转换后的Servlet类的实例。从转换后的Servlet类的代码中,可以看到这种关系:Object page = this;在JSP页面中,很少使用page对象。
pageContext javax.servlet.jsp.PageContext 的实例,该对象代表该JSP 页面上下文,使用该对象可以访问页面中的共享数据。常用的方法有getServletContext和getServletConfig等。
- //使用pageContext 设置属性,该属性默认在page 范围内
- pageContext. setAttribute ("page" , "hello") ;
- //使用request 设置属性,该属性默认在request 范围内
- request. setAttribute ("request" , "hello");
- //使用pageContext将属性设置在request 范围中
- pageContext.setAttribute("request2″ , "hello" , pageContext.REQUEST_SCOPE);
- //使用session将属性设置在session 范围中
- session.setAttribute("session" , "hello");
- //使用pageContext将属性设置在session范围中
- pageContext.setAttribute("session2″ , "hello" , pageContext.SESSION_SCOPE);
- //使用application将属性设置在application范围中
- application. setAttribute ("app" , "hello") ;
- //使用pageContext 将属性设置在application 范围中
- pageContext.setAttribute("app2″ , "hello" , pageContext.APPLICATION_SCOPE) ;
1.pageContext对象是javax.servlet.jsp.PageContext类的实例,主要表示的是一个JSP页面的上下文。
2.pageContext对象有如下一些方法:
(1)页面跳转
public abstract void forward(String relativeUrlPath)throws ServletException, IOException
(2)页面包含
public void include(String relativeUrlPath) throws ServletException, IOException
(3)取得ServletConfig对象
public ServletConfig getServletConfig()
(4)取得ServletContext对象
public ServletContext getServletContext()
(5)取得ServletRequest对象
public ServletRequest getRequest()
(6)取得ServletResponse对象
public ServletResponse getResponse()
(7)取得HttpSession对象
public HttpSession getSession()
样例:
pageContext_forward01.jsp
- <%@ page contentType="text/html" pageEncoding="GBK"%>
- <html>
- <head><title>欢迎来到望星空</title></head>
- <body>
- <%
- pageContext.forward("pageContext_forward02.jsp?info=Joywy"); <!-- 括号内?左右不要有空格,否则会报出HTTP Status 404 -->
- %>
- </body>
- </html>
- <%@ page contentType="text/html" pageEncoding="GBK"%>
- <html>
- <head><title>欢迎来到望星空</title></head>
- <body>
- <%
- //直接从pageContext对象中取得了request
- String info = pageContext.getRequest().getParameter("info");
- %>
- <h3>info = <%=info%></h3>
- <h3>realpath = <%=pageContext.getServletContext().getRealPath("/")%></h3>
- </body>
- </html>
pageContext对象中的getRequest()和getResponse()两个方法返回的是ServletRequest和ServletResponse,而常用的request和response分别是HttpServletRequest和HttpServletResponse接口的实例。
- JSP中page和pageContext的区别
- JSP中page和pageContext的区别
- JSP中page和pageContext的区别
- JSP中page和pageContext的区别
- 20171013 JSP中page和pageContext的区别
- Jsp内置对象page和pageContext的区别
- page和pageContext的区别
- jsp EL表达式取值过程、page和pagecontext的区别
- jsp九大内置对象 page与pageContext的区别
- page与pageContext的区别
- jsp中的page和pageContext整理
- JSP中page(也即this)对象与pageContext对象区别
- jsp中的<%@page .......%> 和 <meta .....>的区别
- jsp中<jsp:forward page=""/>和response.sendRedirect("")两种跳转的区别
- jsp中<jsp:forward page=""/>和response.sendRedirect("")两种跳转的区别
- jsp中<jsp:forward page=""/>和response.sendRedirect("")两种跳转的区别
- JSP基本功 pageContext对象page,request,session,application四个域对象的使用及区别JSP运行原理和九大隐式对象及下载文件、访问次数的代码总结出el表达式简单
- Servlet中Request, ServletContext,Session域和Jsp中Page域的区别
- MySQL与SQL Server的一些区别浅析
- config对象
- 健康1
- Android Day02 知识点
- U3d内存优化(一)之UILabel使用String的问题
- JSP中page和pageContext的区别
- 63. Unique Paths II
- SVM解决多分类问题的方法
- high level tech
- Ambari部署hadoop完全分布式集群
- 给iOS开发者的Sketch入门教程
- Makefile中常用的函数
- 使用map出去if else
- 解决android内存泄露