0

I am using eclipse WTP for developing web applications on on Ubuntu OS being deployed on a tomcat server. I want to make use of images in my file system in the web app (display them). How can I do it efficiently ? Is it by using context path to location on drive ? or is it by loading them (or something like that) using streaming ? Also, I could not find any web.xml or server.xml file in WTP project (since newer version doesn't even require them).

Rephrasing : I want to use images (static content) from file system in my web app. Using JSTL on the front-end.

Edit :

If web app is xyz then its location is : /home/webaapp/xyz/..... and images are at /home/akshay/images/.......

I want to access a folder far away (in same hard drive) from web app

Community
  • 1
  • 1
akshayb
  • 1,219
  • 2
  • 18
  • 44

1 Answers1

2

You can use Tomcat Default Servlet to serve static resources.

web.xml:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

Better use c:url and use prefix / in the value to make the url relative the context path.

<img id="logo" src="<c:url value='/resources/images/logo.png'/>" />

Dynamic project structure:

WebContent
         |
         |__resources
         |          |
         |          |__images
         |                   |
         |                   |__logo.png
         |
         |__WEB-INF
                  |
                  |__web.xml

EDIT

Since the images are not part of the war hence you can try with Servlet. Just store the path somewhere in properties file or pass it as VM arguments or make it constant.

JSP:

<img src="${pageContext.servletContext.contextPath}/servletURL?name=logo.png"/>

Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String imageName=request.getParameter("name")
    String path = "absolute path of the image directory"+imageName;
    BufferedInputStream inputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(new File(path)));
        OutputStream outputStream = response.getOutputStream();
        byte[] bytes = new byte[1024 * 2];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, bytesRead);
        }
        outputStream.flush();
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

Use The try-with-resources Statement to handle the resources, if you are using Java 7.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • but the images are not not the same directory as web app. If web app is xyz then its location is : /home/webaapp/xyz/..... and images are at /home/akshay/images/....... I want to access a folder far away (in same hard drive) from web app. – akshayb Aug 02 '14 at 17:55
  • OK then store the path somewhere in properties or pass it as system enviroment variable. Read the path at application context up. Let me update my post. – Braj Aug 02 '14 at 17:56
  • post is updated with code as well. Try it and let me know the result. – Braj Aug 02 '14 at 18:08