How is using "<%=request.getContextPath()%>" better than "../"
Asked Answered
L

1

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.

Libelous answered 1/11, 2013 at 10:5 Comment(1)
Where is the context path in second case ?Chantey
D
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.

Darleen answered 1/11, 2013 at 10:21 Comment(8)
Is "${pageContext.servletContext.contextPath}" and different than what you suggest? Which one should be used and why?Caundra
${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.Darleen
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?Buonaparte
@DhruvPal Accessing local variable is faster(negligible) than accessing instance variable, so there we can save some time(#21613598). 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.Darleen
@Darleen Thanks so is it ok if I put this variable in common.jsp and use it on multiple jsps by including that common.jsp. My concern is we are creating a new variable assigning value to it and using it, rather than using implicit request.getContextPath(). Is it fine?Is getContextPath() is a heavy method(i guess its getter only).Buonaparte
@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.Darleen
It occurred to me that you could use a <base> HTML tag to help out with this.Kathlyn
@Kathlyn yeah, using <base> tag will indeed work. You won't have to explicitly specify ${context} in each URL. However, <base> tag prepends defined base URL to each URL, this causes problem if you are using named anchors. #1889576 has some nice discussion on usage of <base> tag. I prefer not to use <base> tag to keep things under control. However, if you feel that using <base> tag will be more appropriate, I'll edit answer to include it's usage. Thank you.Darleen

© 2022 - 2024 — McMap. All rights reserved.