{"id":257,"date":"2007-04-25T19:52:44","date_gmt":"2007-04-26T00:52:44","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2007\/04\/25\/embedding-jetty\/"},"modified":"2007-04-25T19:52:44","modified_gmt":"2007-04-26T00:52:44","slug":"embedding-jetty","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2007\/04\/25\/embedding-jetty\/","title":{"rendered":"Embedding Jetty"},"content":{"rendered":"<p><a href=\"http:\/\/jetty.mortbay.org\/\">Jetty<\/a> is a lightweight open source Java-based HTTP Server and Servlet container.  Jetty&#8217;s small footprint makes it perfect for embedding into larger Java applications, in fact Jetty is used by the likes of Jboss Application Server and Apache Geronimo.<\/p>\n<p>Embedding Jetty is extremely easy.  For the most part you just create a new instance of a Jetty server, indicate where the server can find the resource files, such as HTML\/PNG files, maybe add a <b>Web Application Resource<\/b> (WAR), and start the server.  Here is the code step by step, of course the following code assumes you have added the required jars.<\/p>\n<p>[source:java]<br \/>\nServer server = new Server(8080);<br \/>\n[\/source]<\/p>\n<p>The above creates an instance of the Jetty server (org.mortbay.jetty.Server) listening at port 8080, but does not start the server.  At this point we would need to add a default resource handler with the location of the directory which contains the public HTML, JPG, JS, and additional files which will can be requested by clients.<\/p>\n<p>[source:java]<br \/>\nResourceHandler publicDocs = new ResourceHandler();<br \/>\npublicDocs.setResourceBase(&#8220;c:\/path\/to\/public\/docs&#8221;);<br \/>\n[\/source]<\/p>\n<p>I would also like to add support for a Java-based web application.<\/p>\n<p>[source:java]<br \/>\nString webappPath = &#8220;c:\/path\/to\/webapp.war&#8221;;<br \/>\nString contextPath = &#8220;\/webapp&#8221;;<br \/>\nWebAppContext webapp = new WebAppContext(webappPath, contextPath);<br \/>\n[\/source]<\/p>\n<p>Now I need to add the public resource and web application handlers to the Jetty server.<\/p>\n<p>[source:java]<br \/>\nHandlerList hl = new HandlerList();<br \/>\nhl.setHandlers(new Handler[]{publicDocs, webapp});<br \/>\nserver.setHandler(hl);<br \/>\n[\/source]<\/p>\n<p>At this point, you can start the server simply by calling the start method.<\/p>\n<p>[source:java]<br \/>\nsever.start();<br \/>\n[\/source]<\/p>\n<p>Once you have the above code up and running point your browser to http:\/\/localhost:8080 to connect to the Jetty server, of course you would need some index.html in the public docs directory.  In this example, if where to connect to the web application described here you can do so by directing your browser to http:\/\/localhost:8080\/wepapp.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/embed\" rel=\"tag\">embed<\/a>, <a href=\"http:\/\/technorati.com\/tag\/embedding\" rel=\"tag\"> embedding<\/a>, <a href=\"http:\/\/technorati.com\/tag\/jetty\" rel=\"tag\"> jetty<\/a>, <a href=\"http:\/\/technorati.com\/tag\/jboss\" rel=\"tag\"> jboss<\/a>, <a href=\"http:\/\/technorati.com\/tag\/application+server\" rel=\"tag\"> application server<\/a>, <a href=\"http:\/\/technorati.com\/tag\/servlet+container\" rel=\"tag\"> servlet container<\/a>, <a href=\"http:\/\/technorati.com\/tag\/servlet\" rel=\"tag\"> servlet<\/a>, <a href=\"http:\/\/technorati.com\/tag\/jsp\" rel=\"tag\"> jsp<\/a>, <a href=\"http:\/\/technorati.com\/tag\/webapp\" rel=\"tag\"> webapp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jetty is a lightweight open source Java-based HTTP Server and Servlet container. Jetty&#8217;s small footprint makes it perfect for embedding into larger Java applications, in fact Jetty is used by the likes of Jboss Application Server and Apache Geronimo. Embedding Jetty is extremely easy. For the most part you just create a new instance of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-49","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/257"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}