This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Tomcat and the fly likes serve static HTML from app context rather than ROOT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "serve static HTML from app context rather than ROOT" Watch "serve static HTML from app context rather than ROOT" New topic
Author

serve static HTML from app context rather than ROOT

john findler
Greenhorn

Joined: Jan 23, 2006
Posts: 7
I have a JSP application (myapp) deployed successfully in Tomcat 5.5.12 with J2SDK1.5 VM. It successfully locates my compiled .class files and jsp's under my webapps/myapp application context directory, under WEB-INF and so forth.

My problem involves the static HTML and .gif files referenced in the jsp's.
Tomcat insists on finding them only under webapps/ROOT, rather than webapps/myapp . I have tried using relative and absolute URLs for the static HTMLs and GIFs. Tomcat only understands the relative URL (which is relative to the basepath of the application). Tomcat finds these HTML & GIF files when I copy them under webapps/ROOT instead of the webapps/myapp. I have verified that this is not a start-up, refresh, or cache problem. Tomcat instantly serves the referenced files if they are moved to webapps/ROOT when I resubmit the jsp application, and instantly sees that they are gone if I delete them from webapps/ROOT . Tomcat never sees the files when they are under webapps/myapp (which is where they are supposed to be).

Any suggestions?
[ January 23, 2006: Message edited by: johnfake temp ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

johnfake temp
Welcome to JavaRanch!

We're pleased to have you here with us in the Tomcat forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How are you referencing the images?
What is the syntax of your <img .. /> tags?
john findler
Greenhorn

Joined: Jan 23, 2006
Posts: 7
Originally posted by Ben Souther:
How are you referencing the images?
What is the syntax of your <img .. /> tags?


For static html references, this serves the file if its in webapps/ROOT:
See <a href="../Rules.html">Rules</a> <br>

For Gifs, for example, this serves the file if its in webapps/ROOT:

<body background="../libback.gif">


I have tried all combinations of
href="./Rules.html" <br>
href="../images/Rules.html"
href="<%=basePath%>Rules.html" (no extra "/" between the > and Rules)

and I have copied these html and gif files to everywhere under webapps/myapp:
...webapps/myapp
...webapps/myapp/WEB-INF
...webapps/myapp/images
...webapps/myapp/WEB-INF/classes

and in doing so, I have tried full shutdown of Tomcat, browser, and host,
and redeploy. Nothing makes Tomcat look in myapp for static files. It insists on looking in its ROOT for my static pages, even though it finds my jsp's references to jsp and classes under webapps/myapp just fine

Any suggestions now? (Thanks)

[ January 23, 2006: Message edited by: john findler ]
[ January 23, 2006: Message edited by: john findler ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think I see it.

First the parts of the URL:


If you refer to an image with a relative url that begins with a forward slash, the browser is going to build the url starting at the end of the domain.

For example, if you have a JSP in the root of the myApps webapp and you use:
<img src="/images/myImage.gif">
The browser will look in:
http://localhost/images/myImage.gif
Note your contextPath is missing.

try:
<img src="${pageContext.request.contextPath}/images/myImage.gif" />
john findler
Greenhorn

Joined: Jan 23, 2006
Posts: 7
Originally posted by Ben Souther:
... try:
<img src="${pageContext.request.contextPath}/images/myImage.gif" />


I agree with all of your facts. But Tomcat is not cooperating.

Here's some more detail - maybe you can help me find the problem:

1. I TRIED YOUR GOOD SUGGESTION

My href="<%=basePath%>Rules.html"
and img src="<%=basePath%>images/libback.gif"> (note that basePath has an ending slash in it)

have the identical effect as when I change to the syntax based on your suggestion:

... ="${pageContext.request.contextPath}/images/myImage.gif"

I verified that the browser's Properties on these links yields the correct absolute paths.

The resulting absolute URLs for my GIF and html become these according to the browser:
http://localhost/myapp/Rules.html
and
http://localhost/myapp/images/libback.gif
respectively.
These seem correct, but Tomcat does not want to serve them up.

But Tomcat does not treat these absolute URLs as expected.
The GIF displays as a broken [x] symbol on my jsp's page,
and if I click the html link, the browser just redisplays my jsp page.
It doesn't even given me a page not found or complain.


2. I TRIED ENTERING THE URLS DIRECTLY

I have even tried entering these absolute URLs in the browser address field directly.
Tomcat just does not revise the page, and doesn't complain.

As a sanity check, when I enter an absolute URL to the these same files which I copied again to webapps/ROOT, and webapps/ROOT/images,
Tomcat displays them fine. (e.g. http://localhost/Rules.html works fine). So there isn't anything wrong with the files themselves or the ability to serve them via an absolute URL to ROOT on purpose.


3. MAYBE A CLUE: TOMCAT DISPLAYS MY jsp page WHEN IT DOESN't FIND INTENDED FILES

I am wondering why Tomcat doesn't complain when it can't find the files instead of just redisplaying my jsp page.

It's as though Tomcat thinks the URL is addressing a directory and then displaying the default welcome page for .html or .gif .
But this can't be: I've checked all of the web.xml files in my Tomcat installation, and none of them
mention my application .jsp as the default welcome page or anything.

Thanks again in advance for your kind expert help

[ January 23, 2006: Message edited by: john findler ]
[ January 23, 2006: Message edited by: john findler ]
john findler
Greenhorn

Joined: Jan 23, 2006
Posts: 7
GOT IT !!! I'll describe the solution tomorrow. simple.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Let me guess:
You have a url-mapping that looks something like
<url-mapping>/*</urlmapping>
or
<url-mapping>*</urlmapping>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: serve static HTML from app context rather than ROOT