您的位置:首页 > Web前端 > JavaScript

JSP基础---pageContext对象

2014-02-21 20:44 399 查看
pageContext对象

pageContext对象是javax.servlet.jsp.PageContext类的实例,主要表示的是一个JSP页面的上下文,在此类中除了之前讲解过的属性操作之外,还定义了以下的一些方法:

1):public abstract void forward(String relativeUrlPath)

2):public void include(String relativeUrlPath)

3):public ServletConfig getServletConfig()

4):public ServletContext getServletContext()

5):public ServletRequest getRequest()

6):public ServletResponse getResponse()

7):public HttpSession getSession()

pageContext主要的功能是在JSP文件中的支持,而且一定要记住的是,pageContext功能强大,几乎可以操作各种内置对象。

pageContext是一个功能强大的内置对象,所以以后在讲解JSP高端编程的时候,标签库编程基本上就要使用pageContext对象完成了。

下面使用pageContext完成一次跳转功能。

【pagecontext_01.jsp内容如下:】

<%@ page contentType="text/html" pageEncoding="GBK"%>

<html>

<head><title>pageContext</title></head>

<body>

<%

pageContext.forward("pagecontext_02.jsp?info=skewrain");

%>

既然现在是跳转,则肯定是可以传递参数的,那么采用地址重写的方式完成的参数传递。

【pagecontext_02.jsp内容如下:】

<%@ page contentType="text/html" pageEncoding="GBK"%>

<html>

<head><title>pagecontext</title></head>

<body>

<%

//直接从pageContext对象中取得了request

String info = pageContext.getRequest().getParameter("info");

%>

<h3>info = <%=info%></h3>

<h3>realpath = <%=pageContext.getServletContext().getRealPath("/")%></h3>

</body>

</html>

执行pagecontext_01.jsp的结果如下:



Info参数已经被传递过来了,而且此时的跳转类型是属于服务器端跳转的,realpath为虚拟目录对应的真实路径。

小结:

1):pageContext中可以取得request、session等主要内置对象;

2):通过pageContext对象可以完成页面的跳转功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: