?
Solved

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

Posted on 2004-03-29
4
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

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

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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