Solved

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

Posted on 2004-03-29
4
318 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
phpmyadmin installation 3 62
Apache timeout remote connection if not port 80 10 82
Apache Issues 9 80
Apache/PHP: Cannot make phpinfo() and phpmyadmin work 4 51
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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