What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?
Asked Answered
C

4

41

I have a web app, where I have different navigation anchor tags such as Home, Profile and etc.

What I want:

When I press anchor tags like home or profile. I just want to ensure that current user gets its information in that Tags/JSP Page.

Sample Example that I am trying:

<a  href="${pageContext.request.contextPath}/JSPAddress.jsp">Profile</a>
Classieclassification answered 1/5, 2011 at 18:13 Comment(1)
Put your mouse above the [el] tag and click the "info" link on the popbox.Saxhorn
B
74

The pageContext is an implicit object available in JSPs. The EL documentation says

The context for the JSP page. Provides access to various objects including:
servletContext: ...
session: ...
request: ...
response: ...

Thus this expression will get the current HttpServletRequest object and get the context path for the current request and append /JSPAddress.jsp to it to create a link (that will work even if the context-path this resource is accessed at changes).

The primary purpose of this expression would be to keep your links 'relative' to the application context and insulate them from changes to the application path.


For example, if your JSP (named thisJSP.jsp) is accessed at http://myhost.com/myWebApp/thisJSP.jsp, thecontext path will be myWebApp. Thus, the link href generated will be /myWebApp/JSPAddress.jsp.

If someday, you decide to deploy the JSP on another server with the context-path of corpWebApp, the href generated for the link will automatically change to /corpWebApp/JSPAddress.jsp without any work on your part.

Biltong answered 1/5, 2011 at 18:25 Comment(3)
whats the difference between ${pageContext.request.contextPath} and ${request.contextPath} ?Proliferate
@Proliferate I don't believe request is an implicit object in the EL (there's a requestScope though) - the way to get a reference to the request instance is via the pageContext as in the OPs snippet.Biltong
You have to love the Java documentation, "The request's context path is the path that is the context of the request." That makes sense; and oranges taste orangey.Madelenemadelin
P
5

Include <%@ page isELIgnored="false"%> on top of your jsp page.

Polacca answered 12/6, 2015 at 6:20 Comment(1)
Dhanywaada, for me expressionlanguage was getting ignored.Macrobiotic
R
1

use request.getContextPath() instead of ${pageContext.request.contextPath} in JSP expression language.

<%
String contextPath = request.getContextPath();
%>
out.println(contextPath);

output: willPrintMyProjectcontextPath

Retiary answered 23/10, 2014 at 17:24 Comment(3)
Snippets are discouraged, and should not be used anymore. Expression Language is the correct way to do this, and has been for a long time now.Francinafrancine
@KjetilNordin it's still useful to know that from a scriptlet you have to do request.getContextPath() and not pageContext.getRequest().getContextPath() which would be the naive JSP to scriptlet translation.Armillda
The answer do not answer the title of the question. The accepted answer already states that pageContext is implicit in JSP. This answer is not useful, it is noise, and referes to discouraged ways of coding. It is also three years older than the accepted answer, and should in no way need to refer to very outdated practices. So I respectfully disagree with your comment @MarcusJuniusBrutusFrancinafrancine
S
1

For my project's setup, "${pageContext.request.contextPath}"= refers to "src/main/webapp". Another way to tell is by right clicking on your project in Eclipse and then going to Properties:

enter image description here

Safety answered 2/3, 2019 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.