File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Web Application Root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Web Application Root" Watch "Web Application Root" New topic
Author

Web Application Root

Thambi Rajah
Ranch Hand

Joined: Jul 19, 2002
Posts: 36
Assume that our deployed Web Application name is demo.
The following A.jsp file is in the directory demo/jsp.
The DispatcherServlet class is in the directory demo/WEB-INF/classes.

When I access A.jsp on the browser as: http://localhost:8080/demo/jsp/A.jsp,
I get an error saying DispatcherServlet not available (HTTP Status 404).
However, if I change action="../servlet/DispatcherServlet" in A.jsp, then it works fine and gives the response from
DispatcherServlet. I wonder why action="/servlet/DispatcherServlet" does not work although / means Web Application Root directory.
Any help or suggestion to understand this concept will be greatly appreciated.
Thanks in advance,
Thambi
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
In HTML if you dont't give a fully qualified path to a ressource (like 'http://myserver/myressource') all path information is generally considered to be relative to the current file.
This applies also to the action attribute of the form tag: it is searched relative to the location of the current jsp-file. Since this is placed in a subdirectory of your application root (namely the jsp subdirectory) you have to go one step up in the directory hierarchy (using ../) to come to the real webapps rootdirectory.
You must not mix up "/" in HTML (which referrs to a directory location relative to the current directory) with "/" in your application server (which referrs to the rootdirectory of your webapp).
sl
Hartmut
Thambi Rajah
Ranch Hand

Joined: Jul 19, 2002
Posts: 36
Hello Hartmut Ludwig
Thanks for your clear explanation. It gave me a good picture.
I totally agree with you if the above html code is a pure HTML page(A.html).
Still, I want to verify the following:
Since A.jsp is a JSP page, this page will be translated into a generated Servlet, A$jsp.java.
Will this generated Servlet understand / as Web Application Root directory?
Thanks in advance,
Thambi
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
Originally posted by Thambi Rajah:
Since A.jsp is a JSP page, this page will be translated into a generated Servlet, A$jsp.java.
Will this generated Servlet understand / as Web Application Root directory?

Within the JSP you mark the java-code with <% and %> and all code within those 'tags' will be integrated and executed in the generated Servlet. In this part you can refer to / as webapp root e.g. if you want to read from a property-file or something.
The HTML-Part of your code (outside of <% and %> ;) will be incorporated into the output of the servlet and sent to the browser "as is". Same applies to the parts you add with "out.print()" to the output-Stream of the Servlet.
Everything that is sent to the browser (basically what you see if you look at the page source) is plain HTML, no matter how it has been generated.
And any path-information you see there (in the page source) is treated relative to the location of the document.
I hope I was able to explain it clear enough.
The most important difference is: what is the java part and what is the HTML-part. Any path-references in the HTML-part (even if it has been generated from the java part) are treated as if it would be a normal HTML-Document.
sl
Hartmut
[ September 13, 2002: Message edited by: Hartmut Ludwig ]
Arun
Ranch Hand

Joined: Oct 03, 2003
Posts: 40
Elaborating further on the topic of web app root, what would be the most convenient way to refer to the web app root.
I have tomcat with a webapp named testapp.
If i use response.sendRedirect("/index.jsp") from anywhere in testapp or it's subfolders it would go to http://lcoalhost:8080/index.jsp, while I want it to go to http://lcoalhost:8080/testapp/index.jsp. I don't want it to be coded as response.sendRedirect("/testapp/index.jsp") as this would mean that the application will have to be deployed as testapp. If it were deployed as 'ABCApp' the redirect would fail. Nor do i want to code it as response.sendRedirect("../index.jsp") as this would mean i can't change the folder structure later. Is there some way to refer to my webapp root like / refers to the default root folder of tomcat??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Web Application Root
 
Similar Threads
simple JSP and JavaBean, error 500
servlet wont work
Servlets using beans' methods
Calling a servlet from a html page
How to run jsp on TOMCAT?