All most all JSPs invariably contain script or link tags. Say, for example, we have the following in our JSP

<script src=”/js/foo.js” type=”text/javascript”></script>

This is fine as long as you are running the application under root context. Now, tomorrow, say you want to run the application under context “bar”, then your are in big trouble as you have to go to all the JSPs and change the script tag to include the bar application context, as below

<script src=”/bar/js/foo.js” type=”text/javascript”></script>

So, how do we make our JSPs independent of the application context?

Include the application context path in the script tag as below and you no longer have to worry about the application context.

<script src=”${pageContext.request.contextPath}/js/foo.js” type=”text/javascript”></script>

pageContext.request.contextPath gives you the context of the application.

Advertisements