Tomcat can't find my image file when generating HTML

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Tomcat can't find my image file when generating HTML

Postby Mateo1041 » 02. January 2009 23:13

Hi all,

I'm using Eclipse 3.4.1 with XAMPP/Tomcat and am trying to reference an image in a Java web project using the following code:

Code: Select all
out.println("<img src=\"test.jpg\">");


or

Code: Select all
out.println("<img src=\"resources/test.jpg\">");


However, no matter where I try placing the test.jpg file, I just can't seem to reference it as I get a broken image. Here are a few of the places I've tried placing it:

* Directory (the project)
* Directory > resources
* Directory > WebContent
* Directory > WebContent > resources

Am I doing something wrong? If I manually try referencing the image from a browser, I just get the same main project page again:

Code: Select all
localhost:8080/Directory/test.jpg


While the default XAMPP/Tomcat setup appears to not use a specific port in the URL (per the Tomcat example files), Eclipse seems to use port 8080. My entire Eclipse workspace is under C:\Eclipse\workspace\Directory.

The Java project I'm working on works just fine, but I just cannot seem to find out how to reference external images or other files (CSS, XSL, etc).

Any help would really be appreciated! I'm a PHP developer by trade, so this is fairly frustrating.

Thanks,
- Matt
Mateo1041
 
Posts: 4
Joined: 02. January 2009 23:07

Re: Tomcat can't find my image file when generating HTML

Postby Sharley » 03. January 2009 01:10

Please paste the whole of the Tomcat configuration file here - you may find some clues to your issue by reading the conf file anyway.
.\xampp\apache\conf\extra\mod_jk.conf

You followed the installation instructions in the readme-addon-tomcat.txt file?

You started Tomcat using the tomcat_start.bat
You tested this installation from http://localhost by clicking on the Java menu item?
No problems?

Not sure if Eclipse and Tomcat can share the same port - perhaps change the port of one of them and try again after restarting the one you changed so it will recognize the new port.

You mounted the Directory in the conf file?

Remember to backup the conf file before editing so you can revert if required.
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

Re: Tomcat can't find my image file when generating HTML

Postby Mateo1041 » 07. January 2009 23:49

Thanks for the quick reply, Sharley. Here are some answers to your questions:

Sharley wrote:Please paste the whole of the Tomcat configuration file here - you may find some clues to your issue by reading the conf file anyway.
.\xampp\apache\conf\extra\mod_jk.conf


I have not changed anything since installing Tomcat as a XAMPP add-on. Here is the config file:

Code: Select all
<IfModule !mod_jk.c>
    LoadModule jk_module modules/mod_jk.so
</IfModule>

<IfModule mod_jk.c>
JkWorkersFile "c:/xampp/tomcat/conf/workers.properties"
JkLogFile "c:/xampp/tomcat/logs/mod_jk.log"
JkLogLevel info
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# Alias /examples "c:/xampp/tomcat/webapps/examples"

#<Directory "c:/xampp/tomcat/webapps/examples">
#       Options All
#   </Directory>

JkMount /*.jsp ajp13
JkMount /examples/* ajp13
JkMount /jsp/* ajp13
JkMount /servlets/* ajp13

#<VirtualHost 127.0.0.1:80>
#  DocumentRoot c:/xampp/tomcat/webapps/examples
#  ServerName test
#  JkMount /*.jsp ajp13
#  JkMount /servlet/* ajp13
#  Alias /examples "c:/xampp/tomcat/webapps/examples"

 #  <Directory "c:/xampp/tomcat/webapps/examples/">
 #       Options All
 #       Order allow,deny
 #       Allow from all
 #   </Directory>
# </VirtualHost>
</IfModule>


Sharley wrote:You followed the installation instructions in the readme-addon-tomcat.txt file?


Yes. Here is what I have listed under Add/Remove Programs:

* J2SE Runtime Environment 5.0 Update 6
* Java DB 10.3.1.4
* Java(TM) 6 Update 11
* Java(TM) 6 Update 3
* Java(TM) 6 Update 5
* Java(TM) 6 Update 7
* Java(TM) SE Development Kit 6 Update 7

Is this comparable to the SUN J2EE 5.0 SDK? Everything works fine except for displaying images or accessing external files.

Sharley wrote:You started Tomcat using the tomcat_start.bat


Yes. However, I'm now working in Eclipse and Eclipse is automating the start. In fact, Eclipse works with Java even if I haven't manually started Tomcat using tomcat_start.bat.

Sharley wrote:You tested this installation from http://localhost by clicking on the Java menu item?
No problems?


Yes, have tested. No problems. It all works. I just need to know what path to use so that images will show when Java writes HTML.

Sharley wrote:Not sure if Eclipse and Tomcat can share the same port - perhaps change the port of one of them and try again after restarting the one you changed so it will recognize the new port.


FYI, it looks like XAMPP Tomcat does not specify a port when viewing in a browser while Eclipse starts it off 8080.

Sharley wrote:You mounted the Directory in the conf file? Remember to backup the conf file before editing so you can revert if required.


Do you mean the mod_jk.conf file that was pasted above? It should have access to the directory under Windows as far as I know. Not quite sure what you mean.

Any help that can be offered would be appreciated. Thanks!

- Matt
Mateo1041
 
Posts: 4
Joined: 02. January 2009 23:07

Re: Tomcat can't find my image file when generating HTML

Postby Sharley » 08. January 2009 02:21

I have no experience working with Eclipse.

Try and have Tomcat working in XAMPP first if possible, then bring Eclipse into the equation. It may be possible that they will then work together. As it is right now it would be hard to define which of them to try and configure for best results.

The reason for starting Tomcat using the tomcat_start.bat file is it will return a more informative error message if it can't start.

Check, after you have started Tomcat, in the Task Manager for the running tomcat process - right click on the Task Bar>Processes tab look for tomcat.exe.

I believe that you should also start Tomcat as a service if you intend to use it regularly and especially during development but only after all is fine in XAMPP, see below.


Matt wrote:FYI, it looks like XAMPP Tomcat does not specify a port when viewing in a browser while Eclipse starts it off 8080.
readme-addon-tomcat.txt wrote:Tomcat Server Home = .\xampp\tomcat
Tomcat URL = http://localhost:8080
Tomcat Doc Root = .\xampp\tomcat\webapps
The default Tomcat Doc Root is all important just as it is in Apache as Tomcat will run apps with no problems inside this Doc Root, as does Apache serve files inside it's default DocumentRoot (htdocs) - extra configuration in the form of a VirtualHost may be needed for access to files outside the Tomcat Doc Root (again as does Apache) and a VirtualHost container will allow you to define a DocumentRoot to your Java project folder perhaps - not tested.


Drag the file JkWorkersFile "C:/xampp/tomcat/conf/workers.properties" into your text editor for more reading and for some more port details but don't edit it unless you know what you are doing and that you make a backup first.

Go to http://localhost and look for the Java item in the left menu of the Welcome Page - it will tell you if XAMPP has recognized Tomcat and what ports etc. are being used and has working examples if all is as it should be in XAMPP.

My partner Izzy wrote some useful replies to Tomcat inquiries which might help.

Mounting your Java project folder correctly in the mod_jdk.conf file may solve your main problem.
In the mod_jdk.conf file you will see the section in code below and here you would add the folder you wish to mount, which in effect, is telling Tomcat where to look for files and if you put your Java project files in the .\xampp\tomcat\webapps tree then the path would be easily inserted in a JkMount line and Tomcat would find them all.

You can also experiment in here with the file path to your existing Java project if you prefer to keep it's location so Eclipse can also find it or change it's new location in Eclipse's configuration.
(Perhaps the path should look similar to this JkMount "C:\Eclipse\workspace\Directory" - not tested)
Code: Select all
JkMount /*.jsp ajp13
JkMount /examples/* ajp13
JkMount /jsp/* ajp13
JkMount /servlets/* ajp13

If your folder is outside the tomcat tree then perhaps use a VirtualHost by experimenting with the commented example but I have never had the need for nor have I experimented with this aspect - other examples of vhosts can be found in the httpd-vhosts.conf file but would need the...
<IfModule !mod_jk.c>
(JkMount path inserted in here)
</IfModule>
...added to a Tomcat Java enabled vhost container.

Here is what my mod_jdk.conf file looks like and, as you can see, I have modified it somewhat to reflect the folder names in my Tomcat Doc Root (the first forward slash indicate the Doc Root which is the .\xampp\tomcat\webapps folder).
Code: Select all
JkMount /*.jsp ajp13
JkMount /examples/* ajp13
JkMount /java/* ajp13
JkMount /jsp-examples/* ajp13
JkMount /servlets-examples/* ajp13
JkMount /jforum/* ajp13
I place all my Java content in this Doc Root webapps folder.


If all is working as you intended in XAMPP then stop Tomcat and proceed, as it would be time to introduce Eclipse again into the equation.

Good luck.
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

Re: Tomcat can't find my image file when generating HTML

Postby Mateo1041 » 08. January 2009 21:58

Thank you for the reply, Sharley. It gave me a lot to think about. Here's what I've done so far and have narrowed this issue down to my WEB-INF/web.xml file.

1) I found out that Eclipse can run Tomcat in two different ways: Taking control of Tomcat --or-- without taking control of Tomcat:

http://wiki.eclipse.org/WTP_Tomcat_FAQ

I switched things around so that Eclipse will now publish to Tomcat directly using Eclipse's "Location" server option instead of running a separate instance.

2) I then added the following to the "mod_jdk.conf" file as you suggested:

Code: Select all
JkMount /Directory/* ajp13


After restarting everything, Eclipse now publishes to Tomcat itself and takes control of it. While the Java servlet runs, it still doesn't display images.

3) I began playing around with my project's WEB-INF/web.xml file. After some messing around, the following now appears to work whereas it did not before:

http://localhost/Directory/test.jpg

However, I still cannot get images to show when running the servlet itself.

http://localhost/Directory/

Here's my web.xml file:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   id="WebApp_ID" version="2.5">
   <display-name>Directory</display-name>
   <servlet>
      <servlet-name>Directory</servlet-name>
      <servlet-class>com.xxx.Directory</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Directory</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
</web-app>


This appears to be a url-pattern issue. If I use "/" for the url-pattern, the Java servlet runs, but the "http://localhost/Directory/test.jpg" link does not. If I use something like "/hello", however, the Java servlet doesn't work (404), but the image shows.

Any further ideas on how to improve my web.xml file?

Your help is very appreciated!

- Matt
Mateo1041
 
Posts: 4
Joined: 02. January 2009 23:07

Re: Tomcat can't find my image file when generating HTML

Postby Mateo1041 » 08. January 2009 22:26

This issue appears to now be solved! The following now appears to work:

Code: Select all
   <servlet>
      <servlet-name>Directory</servlet-name>
      <servlet-class>com.xxx.Directory</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Directory</servlet-name>
      <url-pattern>/index.do</url-pattern>
   </servlet-mapping>


This is then accessed from the following URL:

http://localhost/Directory/index.do

My trouble with displaying external files such as images was due to a combination of things, most noticeably not fully understanding the web.xml file, how Tomcat structures things, nor the proper URL to use.

You really helped, Sharley. Thank you!
Mateo1041
 
Posts: 4
Joined: 02. January 2009 23:07


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 79 guests