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.