tags:

views:

59

answers:

1

when we can access all the implicit variables in jsp , why do we have pageContext.

solution :

if we use el expressions or jstl , to access or set the attributes we need pageContext. let me know whether i am right.

+2  A: 

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>

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
@balus an example would clear me.
Suresh S
@balus as i have mentioned for el expression language u need pageContext.
Suresh S
BalusC
@BalusC thanks for the answer and the time you spent. yes my english is poor , i will try to improve,by asking questions here.
Suresh S
You're welcome.
BalusC