Java中JSP的pageContext对象和page对象怎么使用
导读:本文共2790字符,通常情况下阅读需要9分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: JSP pageContext对象pageContext 是 javax.servlet.jsp.PageContext 的实例对象。pageContext 对象表示整个 JSP 页面,可以获取或删除以下对象的任意属性:pagerequestsessionapplicationpageContext 常用的方法如下:Object findAttribute (S... ...
目录
(为您整理了一些要点),点击可以直达。JSP pageContext对象
pageContext 是 javax.servlet.jsp.PageContext 的实例对象。
pageContext 对象表示整个 JSP 页面,可以获取或删除以下对象的任意属性:
page
request
session
application
pageContext 常用的方法如下:
Object findAttribute (String AttributeName):按 page、request、session、application 的顺序查找指定的属性,并返回对应的属性值。如果没有相应的属性,则返回 NULL
Object getAttribute (String AttributeName, int Scope):在指定范围内获取属性值。与 findAttribute 不同的是,getAttribute 需要指定查找范围
void removeAttribute(String AttributeName, int Scope):在指定范围内删除某属性
void setAttribute(String AttributeName, Object AttributeValue, int Scope):在指定范围内设置属性和属性值
Exception getException():返回当前页的 Exception 对象
ServletRequest getRequest():返回当前页的 request 对象
ServletResponse getResponse():返回当前页的 response 对象
ServletConfig getServletConfig():返回当前页的 ServletConfig 对象
HttpSession getSession():返回当前页的 session 对象
Object getPage():返回当前页的 page 对象
ServletContext getServletContext():返回当前页的 application 对象
示例
使用 PageContext 对象取得不同范围的属性值。index.jsp 代码如下:
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head></head><body><%request.setAttribute("info","request范围的值");session.setAttribute("info","session范围的值");application.setAttribute("info","application范围的值");%>利用pageContext取出以下范围内各值(方法一):<br>request设定的值:<%=pageContext.getRequest().getAttribute("info")%><br>session设定的值:<%=pageContext.getSession().getAttribute("info")%><br>application设的值:<%=pageContext.getServletContext().getAttribute("info")%><hr>利用pageContext取出以下范围内各值(方法二):<br>范围1(page)内的值:<%=pageContext.getAttribute("info",1)%><br>范围2(request)内的值:<%=pageContext.getAttribute("info",2)%><br>范围3(session)内的值:<%=pageContext.getAttribute("info",3)%><br>范围4(application)内的值:<%=pageContext.getAttribute("info",4)%><hr>利用pageContext修改或删除某个范围内的值:<%pageContext.setAttribute("info","修改request范围的值",2);%><br>修改request设定的值:<br><%=pageContext.getRequest().getAttribute("info")%><br><%pageContext.removeAttribute("info");%>删除session设定的值:<%=session.getAttribute("info")%></body></html>
运行结果如下:
index.jsp运行结果
JSP page对象
JSP page 的实质是 java.lang.Object 对象,相当于 Java 中的 this 关键字。
page 对象是指当前的 JSP 页面本身,在实际开发中并不常用。
page 对象的常用方法如下:
class getClass():返回当前页面所在类
int hashCode():返回当前页面的 hash 代码
String toString():将当前页面所在类转换成字符串
boolean equals(Object obj):比较对象和指定的对象是否相等
void copy (Object obj):把对象复制到指定的对象中
Object clone():复制对象
示例
下面通过一个简单的例子来演示 page 中的方法。
index.jsp 代码如下:
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head></head><body><%Objectobj;obj=null;%>返回当前页面所在类:<%=page.getClass()%><br>返回当前页面的hash代码:<%=page.hashCode()%><br>转换成String类的对象:<%=page.toString()%><br>page和obj比较:<%=page.equals(obj)%><br>page和this比较:<%=page.equals(this)%></body></html>
运行结果如下:
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Java中JSP的pageContext对象和page对象怎么使用的详细内容,希望对您有所帮助,信息来源于网络。