There is a difference in how HTML references are interpreted depending on if the call is from a servlet or from a JSP that was called from a servlet
If the Servlet Invoker is being used, and the path contains /servlet/, then references to non-servlets using a relative reference (without the ..) from the current directory will fail. The Tomcat server will treat all files in such a path as servlets. In this case, use a path relative to the Document Root for non-servlets.
If the servlet has been given a name (in the web.xml file) that is in the root of the web app, then relative references will work. Refer to the Servlet Mapping page for more information about naming a servlet.
This is the strange one. The current directory is the directory that contains the servlet, not the directory that contains the JSP.
The simpest solution is to name the servlet with a name that is in the web app root. Refer to the Servlet Mapping page for more information about naming a servlet. In such a case, the following relative reference will work. It is referencing a styles directory that is a sub directory of the web app root.
<link rel="stylesheet" href="styles/hw.css" type="text/css">
If the servlet is not named, and the Servlet Invoker is being used, then a path relative to the Document Root would be needed. Notice that the name of the web app must be hard coded in the reference. This makes it difficult to develop the web app locally and deploy it remotely.
<link rel="stylesheet" href="/webapp-dir/styles/hw.css" type="text/css">
If the style sheet will be found in the styles subdirectory of the Document Root then the name of the web app is not needed. This is also difficult for devloping locally and deploying remotely, because it requires that the style sheet be located in two different locations. Every change to the style sheet would need to be done twice: once in each location.
<link rel="stylesheet" href="/styles/hw.css" type="text/css">
For the RequestDispatcher that is called from a servlet, the current directory
is the directory that contains the servlet. Assume that the servlet is in
the classes directory of the webapp cgs4854.
The first example is an example of a relative reference that will not work.
It is trying to access a JSP that is in the same directory as the servlet
.../cgs4854/WEB-INF/classes/login.jsp
.
dispatcher.forward("login.jsp");
However, it will not work. An error stating that
/cgs4854/servlet/login.jsp is unavailable
will appear.
The reason is that a path that includes servlet is for servlets, not
for JSPs. The following would work, but is rather tedious to code
dispatcher.forward("/WEB-INF/classes/login.jsp");
In the second example, the JSP should be in the base directory of the webapp
that contains the servlet: .../cgs4854/login.jsp
. The first
/ in the path of the forward call refers to the base directory for
the webapp.
dispatcher.forward("/login.jsp");
The <jsp:include />
and <%@ include %>
directives are the exception to the above rules. For these directives the
current directory is the directory where the JSP is located.
These are relative references to a file in the same directory as the JSP.
<jsp:inlcude page="header.html" /> <%@ inlcude file="header.html" %>
The second examples also work. The header file should be located in the webapp base directory.
<jsp:include page="/header.html" /> <%@ inlcude file="/header.html" %>