I'm confused about where Tomcat maps html paths to within a Servlet. Suppose I have a servlet the outputs some HTML like
<body background="images/bg.gif" bgcolor="#FFFFFF">
I put the Servlet into \webapps\ROOT\WEB-INF\classes for the sake of argument.
I can run the servlet by pointing a browser to http://localhost:8080/servlet/welcome
but where do I need to put the Images directory so "bg.gif" is picked up? (It isn't \webapps\ROOT\WEB-INF\classes\images" for instance).
-
Tomcat and Servlet relative path (6 messages)
- Posted by: Fred Bloggs
- Posted on: April 15 2004 12:18 EDT
Threaded Messages (6)
- Tomcat and Servlet Relative Path by Trevor Brosnan on April 15 2004 13:24 EDT
- Tomcat and Servlet Relative Path by Fred Bloggs on April 15 2004 16:04 EDT
- Tomcat and Servlet Relative Path by Trevor Brosnan on April 16 2004 02:27 EDT
- Tomcat context by Ro Gilhespy on April 16 2004 04:12 EDT
- Tomcat and Servlet Relative Path by Fred Bloggs on April 15 2004 16:04 EDT
- Tomcat and Servlet relative path by Kanagaraj Kuttiannan on April 17 2004 14:50 EDT
- Tomcat and Servlet relative path by Praveen Balakrishnan on April 26 2004 07:03 EDT
-
Tomcat and Servlet Relative Path[ Go to top ]
- Posted by: Trevor Brosnan
- Posted on: April 15 2004 13:24 EDT
- in response to Fred Bloggs
Yep - you can't put static resources such as html docs, images, or javascript files into the classes directory - it's stated in Servlet2.4 spec that anything under WEB-INF cannot be accessed directly by a browser.(so you will place JSP pages in there - it prevents a user from accessing a JSP directly - that is all requests will have to go through a front-controller servlet beforehand)
You will need to put your images somewhere that the webbrowser (which will download the image) can see it, which will be at the same 'level' as WEB-INF. Hopefully this primitive directory structure diagram will help:
--webapps\ROOT
- WEB-INF
- images
- bg.gif -
Tomcat and Servlet Relative Path[ Go to top ]
- Posted by: Fred Bloggs
- Posted on: April 15 2004 16:04 EDT
- in response to Trevor Brosnan
OK makes sense. Is there anyway I can avoid having to go up a directory from the location of the servlet in the Path
E..G:
<body background="../images/bg.gif" bgcolor="#FFFFFF">
Instead of <body background="images/bg.gif" bgcolor="#FFFFFF">
?
Charles -
Tomcat and Servlet Relative Path[ Go to top ]
- Posted by: Trevor Brosnan
- Posted on: April 16 2004 02:27 EDT
- in response to Fred Bloggs
You don't need to go up a directory - it is the web browser that is going to resolve and load the image, not the servlet. The servlet will just generate HTML, which may refer to other resources, that the browser will need to load. So the web-browser sees the "root" of your web application as the directory containing WEB-INF and Images directory in my last post.
So if your servlet reads:
<body background="images/bg.gif" bgcolor="#FFFFFF">
when you run the servlet in a web browser, the image should load.
Hope this helps -
Tomcat context[ Go to top ]
- Posted by: Ro Gilhespy
- Posted on: April 16 2004 04:12 EDT
- in response to Trevor Brosnan
When you define a context for this webapp, you give it a document root.
So for instance:-
http://localhost/mywebapp could point to:-
/var/www/html/website_files/
for sake of argument.
For tomcat, under here you will have a WEB-INF folder, where you can put your classes and jar files, together with a web.xml file.
You map the servlet URL to your servlet using a mapping in the web.xml. This is a virtual mapping, no physical directories need to exist. Tomcat knows that the files it wants will be in WEB-INF/classes or WEB-INF/lib
So in a nutshell, you ordinary files sit in website_files, and your servlets under WEB-INF. -
Tomcat and Servlet relative path[ Go to top ]
- Posted by: Kanagaraj Kuttiannan
- Posted on: April 17 2004 14:50 EDT
- in response to Fred Bloggs
Hi
Every web application has the following directory structure
ROOT(Folder)
| - Where you place the dynamic content namely html, jsp and images.
|
WEB-INF(Folder)
web.xml
- xml file describing the web applications and the dynamic content.
lib(Folder)
- where you place the jar files used by the application.
classes(Folder)
- where you place the dynamic content namely your servlets.
Any relative reference like "images/bg.gif" in a jsp will refer to the same folder where jsp resides. For example if the jsp resides in "<Root>/web/jsp/" then a reference to "images/bg.gif" will look for the image in "<Root>/web/jsp/images/" folder.
In case of servlets any relative reference will always refer to the root directory. Hence the html code "<body background="images/bg.gif" bgcolor="#FFFFFF">" will look for the image in "<Root>/images/" folder.
Cheers
Kanag -
Tomcat and Servlet relative path[ Go to top ]
- Posted by: Praveen Balakrishnan
- Posted on: April 26 2004 07:03 EDT
- in response to Fred Bloggs
Great replies :)
Regards,
Praveen