Java 为什么JSP中需要pageContext?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3735405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:05:10  来源:igfitidea点击:

Why there is a need of pageContext in JSP?

javajspel

提问by Dead Programmer

When we can access all the implicit variables in JSP, why do we have pageContext?

当我们可以访问 JSP 中的所有隐式变量时,为什么我们有pageContext

My assumption is the following: if we use EL expressions or JSTL, to access or set the attributes we need pageContext. Let me know whether I am right.

我的假设如下:如果我们使用 EL 表达式或 JSTL,要访问或设置我们需要pageContext的属性。让我知道我是否正确。

采纳答案by BalusC

You need it to access non-implicit variables. Does it now make sense?

您需要它来访问隐式变量。现在有意义吗?



Update: Sometimes would just like to access the getter methods of HttpServletRequestand HttpSessiondirectly. In standard JSP, both are only available by ${pageContext}. Here are some real world use examples:

更新:有时候只是想访问的getter方法HttpServletRequestHttpSession直接。在标准 JSP 中,两者都只能通过${pageContext}. 以下是一些真实世界的使用示例:



Refreshing page when session times out:

会话超时时刷新页面:

<meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval}">


Passing session ID to an Applet (so that it can communicate with servlet in the same session):

将会话 ID 传递给 Applet(以便它可以在同一会话中与 servlet 通信):

<param name="jsessionid" value="${pageContext.session.id}">


Displaying some message only on first request of a session:

仅在会话的第一次请求时显示一些消息:

<c:if test="${pageContext.session['new']}">Welcome!</c:if>

note that newhas special treatment because it's a reserved keyword in EL, at least, since EL 2.2

请注意,它new有特殊处理,因为它是 EL 中的保留关键字,至少从 EL 2.2 开始



Displaying user IP:

显示用户IP:

Your IP is: ${pageContext.request.remoteAddr}


Making links domain-relative without hardcoding current context path:

在不硬编码当前上下文路径的情况下使链接与域相关:

<a href="${pageContext.request.contextPath}/login">login</a>


Dynamically defining the <base>tag (with a bit help of JSTL functionstaglib):

动态定义<base>标签(在JSTL 函数taglib 的帮助下):

<base href="${fn:replace(pageContext.request.requestURL, pageContext.request.requestURI, pageContext.request.contextPath)}/">


Etcetera. Peek around in the aforelinked HttpServletRequestand HttpSessionjavadoc to learn about all those getter methods. Some of them may be useful in JSP/EL as well.

等等。浏览上述链接HttpServletRequestHttpSessionjavadoc 以了解所有这些 getter 方法。其中一些在 JSP/EL 中也可能有用。

回答by JFC

All 11 implicit EL variables are defined as Map, except the pageContext variable. pageContext variable provides convenient methods for accessing request/response/session attributes or forwarding the request.

除 pageContext 变量外,所有 11 个隐式 EL 变量都定义为 Map。pageContext 变量提供了访问请求/响应/会话属性或转发请求的便捷方法。

回答by isapir

To add to @BalusC's excellent answer, the PageContext that you are getting might not be limited to what you see in the specification.

要添加到@BalusC 的出色答案中,您获得的 PageContext 可能不限于您在规范中看到的内容。

For example, Luceeis a JSP Servlet that adds many features to the interface and abstract classes. By getting a reference to the PageContext you can gain access to a lot of information that is otherwise unavailable.

例如,Lucee是一个 JSP Servlet,它为接口和抽象类添加了许多功能。通过获取对 PageContext 的引用,您可以访问许多原本不可用的信息。