Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WAMP server installation, getting error popups, DLLs missing 5 84
NGINX/Apache redirect on 403 and 404 5 159
LINUX CENTOS + APACHE 9 73
PHP: Insert Data into MySQL 5 62
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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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