1

I have created a small Maven project, using servlets and Jetty. The servlet works fine and outputs a html page. However, the linked image is not displayed (missing).

This is a small piece of my code to setup the server ...

    // Set handler 1 (Display Html)
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(new ServletHolder(hws),"/hello");

    // Set handler 2 (For the images)
    WebAppContext wac = new WebAppContext();
    wac.setContextPath("/img");

    // Attach handlers to server
    handlerList.setHandlers(new Handler[]{context,wac});
    myServer.setHandler(handlerList);

A piece of the servlet that ouputs the html ...

    PrintWriter out = response.getWriter();
    response.setContentType( "text/html");

    out.println( "<html><head><title>Hellow World</title></head>");
    out.println( "<body><h1>Hello World</h1>" );   
    out.println( "<img src=\"/img/img.jpg\">" );
    out.println( "</body></html>");
    out.close();

The image file (img.jpg) is after the build, located in a subfolder "img" in the root of the jar file ...

I would like to use many more images, css files and javascripts. All of them will be embeded in the Jar file.

Does anyone have any experience in displaying images in the ouput of a servlet, and where the images are located within the jar file ?

Thanks

tmmls
  • 510
  • 1
  • 4
  • 16

2 Answers2

1

With the great help and feedback of Joakim, I was able to fix the problem.

To help others with the same problem, I post my solution ....

Step 1 I've created a folder, which after the build is located on the root level, inside the Jar file (The name of the folder is "webroot"). I've placed all my image, css and javascript files in this folder.

myApp.jar
    |_ "webroot" folder
            |_ LionelRichie.jpg
            |_ style.css
            |_ myscript.js
            |_ ...

step 2 I've created a basic setup of the servlet.

    // At the top of the class: setup the imports    
    import java.net.URL;
    import org.eclipse.jetty.server.Handler;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.server.handler.HandlerList;
    import org.eclipse.jetty.servlet.ServletContextHandler;
    import org.eclipse.jetty.servlet.ServletHolder;
    import org.eclipse.jetty.webapp.WebAppContext;

    ...

    // somewhere in your class ...          

    // Locate the path of the "webroot" folder, containing the images etc ...      
    URL warUrl = this.getClass().getClassLoader().getResource("webroot");
    String warUrlString = warUrl.toExternalForm();

    // Create a webAppContext, pointing the "webroot" folder to the "/files" url. 
    // The files will be available at <server>:<port>/files
    WebAppContext wac = new WebAppContext();
    wac.setResourceBase(warUrlString);
    wac.setContextPath("/files");

    // Setup your servlet ("myServlet"), so it can be accessed in the browser. 
    // The servlet can be used at <server>:<port>/hello
    // Note: for some unknown reason, I was forced to configure the servlet
    // with the root contextpath "/" last !  
    ServletContextHandler sch = new ServletContextHandler(ServletContextHandler.SESSIONS);
    sch.setContextPath("/");
    sch.setResourceBase(warUrlString);
    sch.addServlet(new ServletHolder(myServlet),"/hello");

    // Attach handlers to the server
    HandlerList handlerList = new HandlerList();
    handlerList.setHandlers(new Handler[]{wac, sch});
    myServer.setHandler(handlerList);

step 3 In the servlet, changed the url of the exteral files to the correct location.

Example:

           PrintWriter out = response.getWriter();
           response.setContentType( "text/html");

           // output the result
           out.println( "<html><head><title>Hello</title></head>");
           out.println( "<body><h1>Hello, is it me you're looking for ?</h1>" );   
           out.println( "<img src=\"/files/LionelRichie.jpg\">" ); 
           out.println( "</body></html>");
           out.close();

I hope this could be of help to someone ...

tmmls
  • 510
  • 1
  • 4
  • 16
0

Your WebAppContext has no resource base defined.

That means there's no files for it to serve.

Since you are using a ServletContextHandler, you might want to consider...

  1. Using a resource base on your ServletContextHandler that points to the URL for your /webroot in your jar file. Making /webroot/img/ just another directory in it.
  2. Or adding a DefaultServlet reference for serving your static files, with its own defined resource base.
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Hi Joakim. Adding a resourcebase to the 'WebAppContect' and the 'ServletContextHandler' didn't work. I've tried using a string and a url. It refuses to display the image ... – tmmls Apr 24 '15 at 17:30
  • Look at old answers about DefaultServlet ... http://stackoverflow.com/a/20223103/775715 and http://stackoverflow.com/a/28419106/775715 – Joakim Erdfelt Apr 24 '15 at 17:42