26

When we can access all the implicit variables in JSP, why do we have 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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112

3 Answers3

33

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


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


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):

<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 new has special treatment because it's a reserved keyword in EL, at least, since EL 2.2


Displaying user 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 functions taglib):

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

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • as i have mentioned for el expression language u need pageContext. – Dead Programmer Sep 17 '10 at 13:35
  • 4
    You don't need it to access implicit objects or attributes. E.g. `${param.name}` (for `request.getParameter(name)`), `${cookie.name}` (for cookies whose `getName().equals(name)`), `${header.name}` (for `request.getHeader(name)`) or `${attributename}` (for `pageContext.findAttribute(attributename)`) is then sufficient. All implicit objects are listed [here](http://docs.sun.com/app/docs/doc/819-3669/bnaij?l=es&a=view). By the way, your English is pretty poor. After thinking once more, there's probably ambiguity in your original question. You really need to verify and clarify this. – BalusC Sep 17 '10 at 13:36
  • thanks for the answer and the time you spent. yes my english is poor , i will try to improve,by asking questions here. – Dead Programmer Sep 17 '10 at 13:44
0

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

For example, Lucee is 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.

isapir
  • 21,295
  • 13
  • 115
  • 116
-1

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.

JFC
  • 328
  • 4
  • 15