How to change the contextPath of JSP?
Hi all,
I feel this is a common problem all new JSP developers will experience. But I couldn't find a feasible direct solution over the net. Before explaining the problem let me display my directory structure:
the war file has the following structure:
testurl.war\
|
+html\
||
|testhtml.html
|
+jsp\
||
|welcome.jsp
|
+images\
|
test.jpg
apart from this I have a servlet which is mapped in web.xml as \Welcome.
my problem is that I have a link in welcome.jsp to testhtml.html which is given as shown:
<a href="../html/testhtml.html" >Welcome </a>
1. It works fine when I directly access the url"//http:localhost/testurl/jsp/welcome.jsp"
2. The servlet mapped to /Welcome
just forwards the request to welcome.jsp
using:
request.getRequestDispatcher("jsp/welcome.jsp").forward(request,response);
when I access the servlet as "//http:localhost/testurl/Welcome", the jsp appears
but the link does not work.
in first case the link is shown as"//http:localhost/testurl/html/testhtml.html"(works fine)
in second case the link is shown as"//http:localhost/html/testhtml.html"(problem)
anyone please help me to find a workaround to this problem..
thanks and regards,
Gireesh.
# 1
<a href="<%="http://"+request.getServerName()+":"+request.getServerPort()+"/"+request.getContextPath()+"/html/testhtml.html">" >Welcome
</a>
i know this may not be a perfect solution for your question but i'm sure,tht you can try this as a workaround.
CONSEQUENCE : A new Session would be created by using this method after destorying the existing session which can be compensated by using another workaround technique which is by encoding a varible called jsession(Recognized by most of the Java Based Web/Application servers) in the following fashoin.
<%="http://"+request.getServerName()+":"+request.getServerPort()+"/"+request.getContextPath()+"/html/testhtml.html?jsessionid="+session.getId()%>
.
Hope this might be of some help for you.
REGARDS,
RaHuL
# 2
Hi Rahul,
Though, as u said, it was not an optimal solution, It gives me more insight to the problem.. But I have some more doubts...
1. Will the 'getContextPath()'
method always return the application name (the war file name)?
2. In the above exaple specified by you, is it necessary to give
<%="http://"+request.getServerName()+":"+request.getServerPort()+"/"+request.getContextPath()+"/html/testhtml.html %>
or, will this be enough?
request.getContextPath()+"/html/testhtml.html
so that there is no problem of session invalidation..
thnx,
Gireesh
[NB: hope u received 3 duke dollars assigned 2 u.. ;)]
# 3
>1. Will the
>'getContextPath()'
>method always return the application name (the >war file name)?
Well it returns the path where application(war) is being deployed on a WEB / APPSERVER
wid reference to TOMCAT it considers %CATLINA_HOME%\webapp as the root and it returns the offset PATH for that root.
just to get more insightful information try with request.getRealPath(""); which would return the whole path w.r.t server machine say something like
"C:\program file\tomcat\webapps\mywebapp" which cannot be applied to the above senario....
>or, will this be enough?
>request.getContextPath()+"/html/testhtml.html
>so that there is no problem of session invalidation..
Well i don't think so as that would add up to the present path which would be look like
"WEBAPP/jsp/"+request.getContextPath()+"/html/testhtml.html" well i haven't tried it myself its just a prediction which can happen.However you are always welcome to try it as that can even be my misconseption.... too :)