Question

I have a jar library which contains images, css, etc.. This jar is based on a maven project. All files are placed in src/main/resources/web directory.

The jar is placed in shared/lib of Tomcat 5.5 in order to be shared to all web applications.

Now, in a standard web application, how to access to a css (for example) into stylesheet link of a JSP page ?

Thank you.

nota: with Zk framework, we can access with a simple linke i.e "~./web/css/header.css" but it is not working with JSP :(

Was it helpful?

Solution

You can but you need some manual coding, as default servlet (part of container /tomcat/ that serves static resources) does know nothing about your jars. You need to

  1. implement your own servlet that can read data from classpath
  2. map it to some URL
  3. use URL in JSP with some parameter / path identifying the requested file

I implemented simple prototype, it does handle only CSS files and it does not cover any corner cases. But it works and you can extend it as you need:

Servlet is simple, it just takes part or URL behind its mapping and loads resource from classpath (e.g. jar):

package cz.literak.demo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

public class JarServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getPathInfo();
        setContentType(path, response);

        InputStream streamIn = null;
        try {
            streamIn = getClass().getResourceAsStream(path);
            PrintWriter writer = response.getWriter();
            int c;
            while ((c = streamIn.read()) != -1) {
                writer.write(c);
            }
        } catch (IOException e) {
            streamIn.close();
        }
    }

    private void setContentType(String path, HttpServletResponse response) {
        if (path.toLowerCase().endsWith(".css")) {
            response.setContentType("text/css");
        }
        // TODO other mime types
    }
}

You need to register the servlet and its mapping:

<servlet>
    <servlet-name>JarDefault</servlet-name>
    <servlet-class>cz.literak.demo.JarServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>JarDefault</servlet-name>
    <url-pattern>/jar/*</url-pattern>
</servlet-mapping>

And you can use it this way:

<link rel="stylesheet" type="text/css" href="jar/styles/layout.css"/>

I copied file layout.css under directory styles in one jar that is part of my war. Easy, is not it?

OTHER TIPS

The problem was that I was going a step too far in calling the parse method of XMLReader. The parse method accepts an InputSource, so there was no reason to even use a FileReader. Changing the last line of the code above to

xr.parse( new InputSource( filename ));

click this link

Another one sample code: Sometimes it is useful to distribute an application in a jar file through Java Web Start or any other way. So, you could have to read some resource (images or properties file) from inside a jar. How can you do it ? It’s very simple, here’s an example to retrieve an image:

ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));

In general, you can retrieve an InputStream in the following way:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("yourpackage/mypackage/myfile.xml");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top