Link to home
Start Free TrialLog in
Avatar of adrian_walker
adrian_walker

asked on

Unable to view images in dynamically created html from java servlet running on tomcat

This is driving me nuts! If anyone can solve this for me I will be eternally grateful.

I'm using a java servlet to generate html with images in it, running on tomcat 5.
The image paths i'm using need to be relative otherwise I'm going to have to change and recompile vast ammounts of servlet code in the future.

A simple example of my problematic servlet code would be:

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class test extends HttpServlet
{
      public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
      {
            PrintWriter writer = response.getWriter();
            response.setContentType("text/html");

            writer.println("<html>");
            writer.println("<head>");
            writer.println("<title>Test</title>");
            writer.println("</head>");
            writer.println("<body>");
            writer.println("<img src=\"/images/image.gif\">");
            writer.println("</body>");
            writer.println("</html>");
      }
}

The generated page does not display the image, even though i'm sure i have it in the correct directory

I have tomcat installed in the default directory c:\jwsdp-1.3, thus i store my compiled servlets in:
  c:\jwsdp-1.3\webapps\ROOT\WEB-INF\classes

The tomcat manual states:

...you will end up with the following contents in your application's "document root" directory:

*.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript, stylesheet files, and images) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy, but for smaller apps, it is generally much simpler to maintain only a single directory for these files.

This leads me to believe if I put 'image.gif' in:
  c:\jwsdp-1.3\webapps\ROOT\images\image.gif

and use the above servlet to generate:
  <html>
  <head>
  <title>Test</title>
  </head>
  <body>
  <img src="/images/image.gif">
  </body>
  </html>

the graphic should be displayed? yeah? no!

I've tried changing the context path in the server.xml file, it didn't work, but i'm not totally convinced i know what i'm doing with regard to this and I can't understand why I would have too anyway as I'm using the default webapps directory.

Can I give this question a million point value and get it solved in ten minutes?  Serriously though, its reasonably urgent, if anyone could have a crack at it sooner rather than later I promise I will buy them a pint when I see them next in the pub.

Thanks in advance.

Best Regards, Adrian
Avatar of BigRat
BigRat
Flag of France image

URLs starting with a slash are actually called absolute URLs and consequently your /image/* URLs are absolute not relative.

The absolute URLs are resolved according to "document root" for the web server (unless these are "redirected" via a connector or something). What web server are you using? And where is "document root"?
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adrian_walker
adrian_walker

ASKER

BigRat,

Thank you very much for your comments, you just helped the penny drop for me!
It looks like I've been being amazingly stupid for the past two days!!!

Being new to this subject, I didn't even consider the fact that I would need to run a web server in addition to tomcat.  I assumed tomcat handled everything to do with the servlets it was running !?!?!

Why was I expecting to request files using http protocals without anything to serve these requests?  I don't know (No answers to this question please ;-))

A job well done BigRat, I owe you that pint.

Best Regards

Adrian
A little brie and a glass of Bordeaux is more the Rat's liking, thanks!