JSP pageContext对象

1年前 (2024-04-26)

pageContext 是 javax.servlet.jsp.PageContext 的实例对象。pageContext 对象表示整个 JSP 页面,可以获取或删除以下对象的任意属性:

  • page

  • request

  • session

  • application


pageContext 常用的方法如下:

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 代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<title>编程帮(www.biancheng网站站点" rel="nofollow" /> index.jsp运行结果

index.jsp运行结果