Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsp

el

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>
like image 559
a k Avatar asked May 01 '11 18:05

a k


People also ask

What is the use of PageContext request contextPath in JSP?

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.

What is request contextPath()?

The context path is the portion of the request URI that is used to select the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".

What is contextPath in JSP?

contextPath object. This contextPath can be useful for constructing a path to you web resources such as CSS, JavaScript and images. Libraries that you'll need to enable the JSP Expression Language (EL) in your JSP Pages, which usually already included in a Servlet container such as Apache Tomcat.

What is PageContext in Java?

PageContext extends JspContext to provide useful context information for when JSP technology is used in a Servlet environment. A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, as well as a layer above the implementation details.


4 Answers

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.

like image 173
no.good.at.coding Avatar answered Oct 04 '22 03:10

no.good.at.coding


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

like image 33
NaveenG Avatar answered Oct 04 '22 04:10

NaveenG


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

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

output: willPrintMyProjectcontextPath

like image 29
AKB Avatar answered Oct 04 '22 03:10

AKB


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

like image 43
Gene Avatar answered Oct 04 '22 03:10

Gene