Solved

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

Posted on 2004-03-29
4
315 Views
Last Modified: 2010-03-04
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
0
Comment
Question by:adrian_walker
  • 3
4 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 10713519
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"?
0
 
LVL 27

Accepted Solution

by:
BigRat earned 125 total points
ID: 10713529
I meant "what version and platform are you using for your web server Apache"!
0
 

Author Comment

by:adrian_walker
ID: 10717242
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
0
 
LVL 27

Expert Comment

by:BigRat
ID: 10731126
A little brie and a glass of Bordeaux is more the Rat's liking, thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now