java - Missing images when using a servlet and Jetty -
i have created small maven project, using servlets , jetty. servlet works fine , outputs html page. however, linked image not displayed (missing).
this small piece of code setup server ...
// set handler 1 (display html) servletcontexthandler context = new servletcontexthandler(servletcontexthandler.sessions); context.setcontextpath("/"); context.addservlet(new servletholder(hws),"/hello"); // set handler 2 (for images) webappcontext wac = new webappcontext(); wac.setcontextpath("/img"); // attach handlers server handlerlist.sethandlers(new handler[]{context,wac}); myserver.sethandler(handlerlist);
a piece of servlet ouputs 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) after build, located in subfolder "img" in root of jar file ...
i use many more images, css files , javascripts. of them embeded in jar file.
does have experience in displaying images in ouput of servlet, , images located within jar file ?
thanks
your webappcontext
has no resource base defined.
that means there's no files serve.
since using servletcontexthandler
, might want consider...
- using resource base on
servletcontexthandler
points url/webroot
in jar file. making/webroot/img/
directory in it. - or adding
defaultservlet
reference serving static files, own defined resource base.