What Does This Expression Language ${Pagecontext.Request.Contextpath} Exactly Do in Jsp El

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

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.

Concepts for page path in JSP and servlets

${pageContext.request.contextPath} : Returns the portion of the request URI that indicates the context of the request. In fact, it is identical to request.getContextPath(), since ${pageContext.request} refers to the HttpServletRequest of the current request.

For example:

http://localhost:80/myapplication/path/servlet

  • ${pageContext.request.contextPath} returns /myapplication

  • request.getServletPath() Returns the part of this request's URL that calls the servlet, e.g. /path/servlet

  • ${pageContext.request.servletPath} returns /path/servlet

Get real path with Expression Language

  • The warning is unrelated (seems to happen randomly)
  • The "real path" is not the "context path"

What you are looking for is ${application.realPath}

Jsp include not working: file not found, status 500

Provide the path relative to the current page.
Try:

<jsp:include page="shared/header.jsp"/>  

${pageContext.request.contextPath} is the current contextPath of the app in your case is comediansapp so it will try to find a file on the path /comediansapp/shared/header.jsp

Please check: https://stackoverflow.com/a/5850406/4325878

Complete example that I tried:

Project

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:include page="shared/header.jsp" />
<a href="${pageContext.request.contextPath}/login.jsp">Login</a>
<a href="${pageContext.request.contextPath}/signup.jsp">Signup</a>

</body>
</html>

shared/header.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<nav style="height:50px; background:red;">
<strong> JSP!!! </strong>
</nav>

Working Example:
Working Example

What is the precedence of ambiguous (boolean/int) accessors in JSP Expression Language?

Looks like this was asked and answered already, defaulting to the boolean public boolean is<PropertyName>() accessor.

Expression Language: how to simplify this statement (in-like clause needed)

First create an EL function for that. A kickoff example for Facelets can be found in this answer and another one for JSP can be found somewhere near the bottom of our EL wiki page.

public static boolean contains(Object[] array, Object item) {
if (array == null || item == null) {
return false;
}

for (Object object : array) {
if (object != null && object.toString().equals(item.toString())) {
return true;
}
}

return false;
}

(or if you're using JSF utility library OmniFaces, use its of:contains(), although it works in Facelets only, not in the deprecated JSP)

then use it as follows:

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@taglib prefix="my" uri="http://my.example.com/functions" %>
...
${my:contains(fn:split('foo,bar,john,doe', ','), myvar)}

(the fn:split() is merely a trick to convert a delimited String to a String[])

You could even simplify/specialize it further by passing the delimited string directly and perform the split in the function so that you can end up like as:

${my:contains('foo,bar,john,doe', myvar)}

When to use forwardslash and when not to use forwardslash?

In

@WebServlet("/login")

The /login is a url pattern that is relative applications to the contextPath

e.g. if your application had a context path of webapp then a request to

http://localhost:8080/webapp/login would load the LoginServlet

In your jsp the form action

Is relative to the jsp page itself, and not the contextPath.

However because your jsp is located in the webroot folder (the top level folder where your jsp's and WEB-INF folder live)

http://localhost:8080/webapp/login.jsp

then the action="login" attribute in the form

will resolve to the location

http://localhost:8080/webapp/login

when the form is submitted and will call the LoginServlet

If you move the jsp into a subfolder (e.g. folder1) then action=login will not call the login servlet

as the jsp will now be located at

http://localhost:8080/webapp/subfolder/login.jsp
and so action=login

will now resolve to
http://localhost:8080/webapp/subfolder/login

and the servlet will not be found (remember the login servlet is relative to the context root, thats what the / means in @WebServlet("/login"))

changing the form action to

<form action="../login" method="post">

would work.

To avoid having to work this out in webpage forms

most people will change the form action to look like this

<form action="${pageContext.request.contextPath}/login" method="post">

So that where ever the jsp is located the el expression

${pageContext.request.contextPath}/login

will resolve to same location as the servlet defined with url pattern /login

see What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL? for more info an the el expression

Hope this helps



Related Topics



Leave a reply



Submit