Saturday, July 28, 2012

Static ContextPath to Dynamic ContextPaths in Jsp and JS pages

Changing Static ContextPath to Dynamic ContextPath references in JSP and JS pages in our project. If you are working with any project on java and jsp technologies, if the requirement may come to change the static context paths to dynamic context path.

What is the need to change context path from static to dynamic context path?

While developing our application we use to develop to put our contextPath directly in some areas like for links and mostly for images we give absolute paths with context. We put this in developing the project. After developing the project , it goes to Testing after then to production.
Let us assume our project contextPath is "/myproj"

Now this project is going for testing, then the testers change the contextPath as theirs purpose like "/myprojTest" then the images and links all will broken. For this purpose the dynamic context references will be use full.

1. Change to Dynamic ContextPaths in Pager Tags of JSP pages.

The Old Code having Static ContextPath:

<pg:pager maxPageItems="10"




Here the ContextPath is /myproj need to change to Dynamic path as given below:

use jsp:useBean tags to define the variable name and get the contextPath then assign the link to the variables pnUrlName and urlName in the scriptlets.

<jsp:useBean id="pnUrlName" class="java.lang.String" scope="page" />

<jsp:useBean id="urlName" class="java.lang.String" scope="page" />

      <%pnUrlName = request.getContextPath()+"/auto/"; %>

      <%urlName = request.getContextPath()+"/auto/"; %>

      <pg:pager maxPageItems="10"

            prevNextUrl="<%=pnUrlName %>"

            url="<%=urlName %>" 


2. Change ContextPath for images in Jsp page

<img src="/myproj/images/calbtn.gif" border="0" alt="popup selection calendar ">
We have to change the static contextPath /myproj to dynamically.

for that we have to declare in jsp page below to the taglib tags. as given below:
<%@ taglib uri="/WEB-INF/tlds/struts-core.tld" prefix="c" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>

Convert the image with static contextPath to dynamically as given below

<img src="<c:out value="${path}"/>/images/calbtn.gif" border="0" alt="popup selection calendar">

3. Change ContextPath in javascript file to open a new window.

caseWindow ='/myapp/cases/'+jsessionId,'

Then change the contextPath /myproj to dynamic contextPath

As we did before for the jsp page we have to define the contextPath to a variable path using jsp core tag library tags.
<c:set var="path" value="${pageContext.request.contextPath}"/>
use the contextPath in the javascript function:
<script type="text/javascript">
      contextPath = '<c:out value="${path}"/>';
modalCaseWindow ='/cases/'+jsessionId,'DN_modalEms',

4.Change contextPath for hyperlinks:

<a href="/myproj/' + ent.deviceId + '">';
Change the static context root reference to dynamic contextPath for the hyperlink in jsp file as given below.

Use the same defined contextPath variable to use here. Actually the contextPath is global accessible in an application any where. NO need to define in every page of jsp.
<a href="'+ contextPath +'/' + ent.deviceId + '">';

5.Change contextPath in javascript file for image.

if (data.status == 1) {

                  $('#circle').attr('src', '/myproj/images/green-circle.png')
Change the context path in the if condition. For this define a variable as ctx to get contextPath using request.getContextPath() method.

<script>var ctx = "<%=request.getContextPath()%>"</script>
Declare the ctx variable below to the taglib uri tags then we can use any where in the program.

if (data.status == 1) {

You might also like:     


  1. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

    Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

  2. This comment has been removed by the author.

  3. THANK YOU for this amazing and for sharing this blog with us, it is very helpful.
    please keep updated us more about like this type of blog.
    If someone is looking for the best java training institute for software training in Ghaziabad, java training institute
    It is the best place from where you get the practical knowledge of java training institute here. You will be an expert in this field after doing the java training.