• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

embedding a servlet server in a jar

 
Trailboss
Posts: 24082
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can deploy my war file to a bare bones, fresh tomcat installation and it will run just peachy.

And then came the idea of "What if we embedded Jetty into our app? Maybe instead of one war file and tomcat, we could have just one jar file. That would be so much easier for people to install!"

I've never used Jetty before. This idea sounds really cool. And today's the day to take two hours to try and figure it out.

So ... I got to the part where I'm going to stuff everything into a jar file ... and ... well ... where do you put your classes? Your libs? Your .html files ??? I see that I'm going to have to write a tiny main() to kick off the jetty stuff. That's easy. And I already know how to do the "java -jar trick" ...

I guess I thought there would be a web page spelling this sort of thing out: the jar file directory structure would like like .... this ... and the manifest file would look like ..... this .... and don't forget to ....

Anybody have a recipe for this sort of thing?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have only used jetty once for a small project, so take the following with a grain of salt.

As far as I can tell, regarding your classes and libraries, jetty doesn't really change anything. It is just another third party library for your application. If you want to have just one jar, you might want to take a look at http://one-jar.sourceforge.net (haven't ever used it myself, though).

I guess you have already found http://docs.codehaus.org/display/JETTY/Embedding+Jetty which explains how to, well, embed jetty.

For your html files, you need to add a ResourceHandler to your context: http://www.mortbay.org/apidocs/org/mortbay/jetty/handler/ResourceHandler.html - the resourceBase you have to configure is the root folder for your static content (that is, html files, images, etc. pp.) I don't see a handler that would be able to serve the static content directly from the jar file, but I guess it wouldn't be too hard to write your own.

Does that help?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't used Jetty before, either, but if you're familiar with Tomcat, why not use Tomcat as the embedded server?
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by paul wheaton:
I guess I thought there would be a web page spelling this sort of thing out.



Not exactly what you are looking for as it doesn't address the single jar issue - however you may be able to glean some insights from it for your "quest"
Benjy's Guide for Constructing CD-Based Dynamic Web Sites

On second thought it probably doesn't have any relevance ... :roll: never mind.

Originally posted by Ulf Dittmer:
why not use Tomcat as the embedded server?



One big advantage of Jetty 6 is that it can support Comet better than Tomcat because of its support for continuations.
[ March 24, 2008: Message edited by: Peer Reynders ]
 
paul wheaton
Trailboss
Posts: 24082
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja,

If you want to have just one jar, you might want to take a look at http://one-jar.sourceforge.net (haven't ever used it myself, though).



Oooo ooo ooo ooo !!! That's fantastic stuff!!!

I guess you have already found http://docs.codehaus.org/display/JETTY/Embedding+Jetty which explains how to, well, embed jetty.



Yup! I can see serving my servlet stuff just fine. I'm looking for the part where I say "and you can find my html files and my images in the jar file!"

And then I wanna make sure that hackers can't download my servlet class files.

But I wasn't seeing that sort of thing. And rather than spend eight hours experimenting, I thought (I can't be the first person to do this - surely somebody has been down this same road and there is a 30 minute recipe ....)

For your html files, you need to add a ResourceHandler to your context: http://www.mortbay.org/apidocs/org/mortbay/jetty/handler/ResourceHandler.html - the resourceBase you have to configure is the root folder for your static content (that is, html files, images, etc. pp.) I don't see a handler that would be able to serve the static content directly from the jar file, but I guess it wouldn't be too hard to write your own.



I guess my hopes were that it would be simpler than this. Something already cooked up and I just have to plug in my stuff.

Does that help?



Yes! That moves me waaaay forward.

Ulf,

why not use Tomcat as the embedded server?



Size, mostly. The tomcat solution just looks much bigger. And this is something where I want to find that quickest path and my impression is that more folks have done this sort of thing with Jetty than with tomcat.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the file size of the required JAR files to run an embedded jetty is roughly 4.6MB. Embedded Tomcat is about 5.8 I believe, last time I looked. You aren't saving much in the way of space.

That being said, I prefer Jetty for embedded purposes and I am in the processing of tweaking my own dev cycle to use Jetty. Because my own personal project structure is a bit non-standard I am writing my own Jetty Launcher customized to my needs. It's not that difficult but still having a few lingering issues.

I've never tried putting web accessible content in JAR files though so I can't help with that. I'd be willing to bet its not very common.
 
paul wheaton
Trailboss
Posts: 24082
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My math says that jetty would be 774k. Not so?

I suspect that it would be bigger if you are using JSP or some of the other stuff. My servlets are pretty standard and everything else is just served up as static files.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by paul wheaton:
My math says that jetty would be 774k. Not so?

I suspect that it would be bigger if you are using JSP or some of the other stuff. My servlets are pretty standard and everything else is just served up as static files.



agh. Ok at the very least you could get away with the servler jar and there are 2 jetty jar files. You might even be able to go to an earlier version. Servlrts havnt changed that much.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

if your application knows where your jar is in the local file system, and in which path your static content is in the jar, it might be worth a try using a ZipInputStream to read the static content from the jar.
Since you have to skip through the ZipEntry objects one by one until you find the one whose getName() method returns the sought name, this might be somewhat costly if the jar has many entries.
You might also unpack the static resources on-demand, storing them on the local file system on the first request, and search the jar file with the above method only if the sought static content is not found in the local file system.

Hope that helps,
Guido
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

I had the same idea for an upcoming project at work. I did some googling a while back and found this article describing how to use Winstone as the embedded container. http://www.itmill.com/articles/Packaging_web_applications_for_desktop_use.htm I think Winstone only provides the servlet container but there are instructions on their Sourceforge page for integrating Jasper to handle JSP content. I don't know if this would help you out with the static content or not. I don't recall reading anything in either direction about it being handled.
I've not had a chance to try any of this, so consider this a disclaimer I'd be interested to know if you have any success getting this to work regardless of which embedded container you end up using.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic