29

I have worked on number of J2EE projects where the view layer is JSP. In most projects, I have seen that we reference external resources i.e. images, javascript, jsp's, css etc. using the contextPath in the scriptlet.

The code is as follows,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GC Demo Using HandlebarsJS</title>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jqueryUI-AutoComplete/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/handlebarsJS/handlebars.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.servletContext.contextPath}/js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.css">

From the above jsp, here I am importing the external resources which are in my same project bundle i.e. in my war.

Now the same above JSP can be written as below code,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GC Demo Using HandlebarsJS</title>
    <script type="text/javascript" src="../js/jqueryUI-AutoComplete/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.js"></script>
    <script type="text/javascript" src="../js/handlebarsJS/handlebars.js"></script>
    <link rel="stylesheet" type="text/css" href="../js/jqueryUI-AutoComplete/jquery-ui-1.10.3.custom.css">

Here in the second example too I am referencing the resources present in my war.

Now considering both of the above two cases, the first case is given more significance as a best practise.

Why?

and what are the drawbacks of using the second case?

Does using the second case, our project gets more tightly coupled with the contextpath?

Please explain to me.

1
  • Where is the context path in second case ? Nov 1, 2013 at 10:11

1 Answer 1

62

request.getContextPath()- returns root path of your application, while ../ - returns parent directory of a file.

You use request.getContextPath(), as it will always points to root of your application. If you were to move your jsp file from one directory to another, nothing needs to be changed. Now, consider the second approach. If you were to move your jsp files from one folder to another, you'd have to make changes at every location where you are referring your files.

Also, better approach of using request.getContextPath() will be to set 'request.getContextPath()' in a variable and use that variable for referring your path.

<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>

PS- This is the one reason I can figure out. Don't know if there is any more significance to it.

8
  • 1
    Is "${pageContext.servletContext.contextPath}" and different than what you suggest? Which one should be used and why? Apr 20, 2014 at 8:31
  • ${pageContext.servletContext.contextPath} and ${pageContext.request.contextPath} will give same contextPath except for one condition, if servlet container matches a context by more than one context path i.e your single application is accessed using multiple context paths. It is advisable to use ${pageContext.servletContext.contextPath} in such case. Apr 21, 2014 at 9:06
  • Also, better approach of using request.getContextPath() will be to set 'request.getContextPath()' in a variable and use that variable for referring your path. Can somebody highlight why its is?
    – Dhruv Pal
    Jul 4, 2014 at 6:10
  • 2
    @DhruvPal Accessing local variable is faster(negligible) than accessing instance variable, so there we can save some time(stackoverflow.com/questions/21613098/…). Also, This approach will help your links look clean. It helps when you have lot of link on a page. If you going to refer contextPath only once then, one should not care about setting it in a variable. However, when you use it multiple them,it helps. In original post,OP used <%=request.getContextPath()%> i.e scriplet. Certainly, using EL tags than scriplet is better approach. Jul 4, 2014 at 6:34
  • 1
    @DhruvPal Yes, you can put variable in common.jsp or your default layout if you are using Tiles. Yes, its not heavy method. Performance difference will be negligible, if any. Jul 4, 2014 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.