aspose file tools*
The moose likes Servlets and the fly likes embedding a servlet server in a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "embedding a servlet server in a jar" Watch "embedding a servlet server in a jar" New topic
Author

embedding a servlet server in a jar

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20635
    ∞

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?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
I haven't used Jetty before, either, but if you're familiar with Tomcat, why not use Tomcat as the embedded server?


Ping & DNS - my free Android networking tools app
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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

Joined: Dec 14, 1998
Posts: 20635
    ∞

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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20635
    ∞

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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
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.
 
Consider Paul's rocket mass heater.
 
subject: embedding a servlet server in a jar