1

I am working on a simple java web application that needed to upload user images.

Firstly, I used getServletContext().getRealPath(). But then somebody suggested, it's not a good way as images will get lost on when application re-deployed. I considered it right.

Then I uploaded those images to outside root application folder i.e. somewhere on "D:\images" path on the server.

Then in order to display those images I read this thread that describes how to display images by setting <Context docBase="D:\images" path="/myprojimages" /> under <host> tag in server.xml under conf directory of tomcat.

But I was not able to display images on server startup. Below is my short HTML file.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    hello<br><br>
    <img src="<img src='http://localhost:9191/myprojimages/Facebook_icon.jpg" width="30" height="30" />
</body>
</html>

below is the entry in server.xml file

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <Context docBase="D:\dumpData"  path="/myprojimages" />
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

can anyone suggest or correct me where am I getting wrong..as the image is not getting displaying?

UPDATED

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    hello<br><br>
    <img src="<img src='/myprojimages/Facebook_icon.jpg" width="30" height="30" />
</body>
</html>
  1. no need to mention localhost:9191.
  2. make sure you are running this project outside eclipse, because eclipse creates context docBase entry on its own inside its local server.xml file.
Community
  • 1
  • 1
JPG
  • 1,247
  • 5
  • 31
  • 64

1 Answers1

1

I was able to solve this problem on my own, with below points in consideration:

  1. no need to mention localhost:9191 with img src
  2. make sure you are running this project outside eclipse, because eclipse creates context docBase entry on its own inside its local server.xml file.
JPG
  • 1,247
  • 5
  • 31
  • 64