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.
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.
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.
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. Sure you can do much more than that
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
andHttpSession
directly. In standard JSP, both are only available by${pageContext}
. Here are some real world use examples:Refreshing page when session times out:
Passing session ID to an Applet (so that it can communicate with servlet in the same session):
Displaying some message only on first request of a session:
note that
new
has special treatment because it's a reserved keyword in EL, at least, since EL 2.2Displaying user IP:
Making links domain-relative without hardcoding current context path:
Dynamically defining the
<base>
tag (with a bit help of JSTL functions taglib):Etcetera. Peek around in the aforelinked
HttpServletRequest
andHttpSession
javadoc to learn about all those getter methods. Some of them may be useful in JSP/EL as well.