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