You are on page 1of 9

1 mbeddIng jetty jetty Codehaus

07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty


Added by Jan Bartel, lat edited by M Flax on Jun 25, 2009
Embedding Jetty
COTACT THE CORE JETTY DEVELOPERS AT WWW.WEBTIDE.COM
private support for your interna/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scaabiity
guidance for your apps and Ajax/Comet projects ... deveopment services from 1 day to fu product deivery
Embedding Jetty
Quick Start
Quick Start - HandIers
The following code implement an anonymou hello handler and tart a jetty erver with it:
Handler handler=new AbtractHandler()
{
public void handle(String target, HttpServletRequet requet, HttpServletRepone
repone, int dipatch)
throw IOException, ServletException
{
repone.etContentType("text/html");
repone.etStatu(HttpServletRepone.SC_OK);
repone.getWriter().println("<h1>Hello</h1>");
((Requet)requet).etHandled(true);
}
};
Server erver = new Server(8080);
erver.etHandler(handler);
erver.tart();
Quick Start - ServIets
Here i a imple example of embedding Jetty uing a Hello world ervlet:
Server erver = new Server(8080);
Context root = new Context(erver,"/",Context.SESSIONS);
root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/*");
erver.tart();
2 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
Quick Start - jetty.xmI
You can programatically create a jetty Server, and till ue a jetty.xml file to configure it:
Server erver = new Server();
XmlConfiguration configuration = new XmlConfiguration(new File("myJetty.xml").toURL()); //
or ue new XmlConfiguration(new FileInputStream("myJetty.xml"));
configuration.configure(erver);
erver.tart();
Quick Start - ServIets and .jsp pages (hosted within the same .jar as the
embedder)
Here i a imple example of embedding Jetty uing a Hello world ervlet:
// aume that thi directory contain .html and .jp file
// Thi i jut a directory within your ource tree, and can be exported a part of your
normal .jar
final String WEBAPPDIR = "com/xxx/yyy/webapp";
final Server erver = new Server(httpServerPort);
final String CONTEXTPATH = "/admin";
// for localhot:port/admin/index.html and whatever ele i in the webapp directory
final URL warUrl = thi.cla.getClaLoader().getReource(WEBAPPDIR);
final String warUrlString = warUrl.toExternalForm();
erver.etHandler(new WebAppContext(warUrlString, CONTEXTPATH));
// for localhot:port/ervlet/cut, etc.
final Context context = new Context(erver, "/ervlet", Context.SESSIONS);
context.addServlet(new ServletHolder(new CutomerServlet(whatever)), "/cut");
context.addServlet(new ServletHolder(new UerServlet(whatever)), "/uer");
erver.tart();
More ExampIes
Thee example are all included a part of the tandard Jetty ditribution in the $JETTY_HOME/example/embedded ub
project.
FileServer - imple HTTP file erver
OneHandler - Embed a ingle handler - ueful when there i only a ingle imple ource of content.
ManyHandler - Embed multiple handler, one called after the other on each requet.
OneContext - An example of a handler within a context, which allow a contextPath, reourceBae and cla loader
to be et.
MinimalServlet - An example of a ServletHandler without a context.
OneServletContext - A ervletHandler within a context, which ha been contructed with a eion handler.
ManyServletContext - Multiple ervlet context uing context path to elect the context.
LikeJettyXml - a code example that mimic the configuration of the tandard jetty.xml file
FromXmlConfiguration - a example that how how XML fragment may be ued to build a erver and/or context.
3 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
OneWebApp - an example howing a ingle webapp being added programmatically.
Setting up the cIasspath
To run Jetty you need only the following jar on the clapath:
ervlet-api-2.5-6.x.jar
jetty-util-6.x.jar
jetty-6.x.jar
Thi give you the capability to handle HTTP with handler, ervlet and webapplication.
Additional feature uch a JSP and AJP require additional jar (normally found in ubdirectorie of $JETTY_HOME/lib).
Java Server Pages
JSP2.1
Remember!
You need to ue J2SE5 (aka jdk 1.5) if you wih to ue JSP2.1.
Thi i the jp verion mandated by ervlet pecification 2.5. You will need thee jar:
ant-1.6.5.jar
core-3.1.1.jar
jp-2.1.jar
jp-api-2.1.jar
JSP2.0
JSP 2.0 i ued with verion 2.4 of the ervlet pecification. Depending on the functionality you require, you may be able
to ue thi verion rather than the newer JSP2.1
Thee are the dependencie when JSP 2.0 i ued:
ant-1.6.4.jar
japer-compiler-5.5.15.jar
japer-runtime-5.5.15.jar
jp-api-2.0.jar
common-el-1.0.jar
jcl104-over-lf4j-1.0-rc5.jar
lf4j-imple-1.0-rc5.jar
xmlParerAP-2.6.2.jar
xercempl-2.6.2.jar
Comments (14)
1.
4 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
May 24, 2006
Christian d'Heureuse says:
have written an open-ource application that ue Jetty6 embedded: http:...
have written an open-ource application that ue Jetty6 embedded:
http://www.ource-code.biz/ServletViewer/
Maybe thi could be ueful for other to get a tart.
2.
May 28, 2006
Stefan KIeineikenscheidt says:
A long there i not tutorial you might want to have a look at: Quick Guide to E...
A long there i not tutorial you might want to have a look at: Quick Guide to Embedding Jetty
3.
Dec 12, 2006
CIinton Foster says:
n the jetty.xml example, the argument for the XmlConfiguration contructor hou...
n the jetty.xml example, the argument for the XmlConfiguration contructor hould be: new FilenputStream
("myJetty.xml").
4.
Dec 13, 2006
Jan BarteI says:
Clinton, Thank for pointing thi out. Fixed now. Jan
Clinton,
Thank for pointing thi out. Fixed now.
Jan
5.
Dec 13, 2006
Tim BarIotta says:
The example for MinimalServlet ha an error. t never call: handler.init...
5 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
The example for MinimalServlet ha an error. t never call:
handler.initialize();
HTH,
Tim
6.
Dec 14, 2006
Jan BarteI says:
Tim, fixed in vn trunk. BTW the fix wa in the ServletHandler cla to enure t...
Tim, fixed in vn trunk. BTW the fix wa in the ServletHandler cla to enure that the ServletHolder get initialized
in the abence of any aociated Context. Thank for noticing thi.
7.
Jan 20, 2007
Damiano says:
have ported my application from Jetty5 to Jetty6 and while doing o "dicove...
have ported my application from Jetty5 to Jetty6 and while doing o "dicovered" a few thing that wih to
hare with all of you.
You can find the log and comment of it at the following
http://www.engidea.com/blog/informatica/jetty6/jetty6-explored.html
hope you find it ueful.
8.
Jul 17, 2007
Iarry h. says:
For my tet rig, found the example provided in the link a bit confuing. h...
For my tet rig, found the example provided in the link a bit confuing. have an expanded, ingle webapp that
need to tet. t ha a web.xml that ha pecification to et up Spring MVC, etc.
The good new i that, from the comment above, found Chritian' ource very brief and undertandable. For my
ingle-context tet rig, implified even more:

erver = new Server();
Connector connector = new SelectChannelConnector();
connector.etPort(PORT);
connector.etHot("127.0.0.1");
erver.addConnector(connector);
6 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
WebAppContext wac = new WebAppContext();
wac.etContextPath("/");
wac.etWar("./web"); // thi i path to .war OR TO expanded, exiting
webapp; WILL FIND web.xml and pare it
erver.etHandler(wac);
erver.etStopAtShutdown(true);
erver.tart();
Conider adding an example which jut doe thi mot imple invocation.
larry
9.
Aug 06, 2007
AIexander says:
want to ue Heian protocol http://www.caucho.com/heian/ between Server and...
want to ue Heian protocol http://www.caucho.com/heian/ between Server and Client. For erver i need
ervlet container (Tomcat, Jetty...). chooe Jetty and write next code to realize Heian Server into Jetty
Embeded ervlet container:
public cla HeianServlet {
public tatic void main(String[] arg) throw Exception
{
Server erver = new Server();
Connector connector=new SocketConnector();
connector.etPort(8888);
erver.etConnector(new Connector[]{connector});
ServletHandler handler=new ServletHandler();
erver.etHandler(handler);
handler.addServletWithMapping
("com.caucho.heian.erver.HeianServlet", "/hello");
erver.tart();
erver.join();
}
}
But i need to to pa 2 parameter for com.caucho.heian.erver.HeianServlet Servlet - thi parameter i to pa
over imple web.xml in imple deploy ervlet:
<web-app>
<ervlet>
<ervlet-name>hello</ervlet-name>
<ervlet-cla>com.caucho.heian.erver.HeianServlet</ervlet-cla>
<init-param>
<param-name>home-cla</param-name>
7 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
<param-value>ervice.BaicService</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>ervice.Baic</param-value>
</init-param>
</ervlet>
<ervlet-mapping>
<url-pattern>/hello</url-pattern>
<ervlet-name>hello</ervlet-name>
</ervlet-mapping>
</web-app>
How i can to pa thi parameter for ervlet over uual code (for example in my code in higher)???
10.
Aug 08, 2007
AIik EIiashberg says:
For ue of Jetty a an embedded container, where the full power of Servlet i n...
For ue of Jetty a an embedded container, where the full power of Servlet i not needed, ContextHandler i an
extremely ueful lightweight cla. n particular it allow defining cutom error handler for the application.
11.
Aug 14, 2007
john doe says:
Running Apache Axi in Embedded Jetty import org.apache.axi.tranport.http.Adm...
Running Apache Axi in Embedded Jetty
import org.apache.axi.tranport.http.AdminServlet;
import org.apache.axi.tranport.http.AxiServlet;
import org.apache.log4j.Logger;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.ecurity.SlSocketConnector;
import org.mortbay.jetty.ervlet.Context;
import org.mortbay.jetty.ervlet.ServletHolder;

...

erver = new Server();
SlSocketConnector lConnector = new SlSocketConnector();
lConnector.etKeytore(KEYSTORE);
lConnector.etTruttore(KEYSTORE);
lConnector.etPaword(KEYPASSWORD);
lConnector.etKeyPaword(KEYPASSWORD);
8 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
lConnector.etTrutPaword(KEYPASSWORD);
lConnector.etMaxIdleTime(30000);
lConnector.etPort(443);
erver.addConnector(lConnector);
ServletHolder axiServletholder = new ServletHolder(new AxiServlet());
ServletHolder axiAdminServletholder = new ServletHolder(
new AdminServlet());
Context root = new Context(erver, "/", Context.SESSIONS);
root.addServlet(axiServletholder, "/ervlet/AxiServlet");
root.addServlet(axiServletholder, "/ervice/*");
root.addServlet(axiServletholder, "*.jw");
root.addServlet(axiAdminServletholder, "/ervlet/AdminServlet");
try {
log.info("Starting Web Container");
erver.tart();
//erver.join();
} catch (Exception e) {
log.info("Error Starting Web Container", e);
return;
}
Alo erver-config.wdd ha to be in the clapath, i put it in the top level of my rc jar.
12.
Sep 17, 2007
Ichiro Furusato says:
We've been uing Jetty in an embedded application for over five year, but upon ...
We've been uing Jetty in an embedded application for over five year, but upon upgrading to 6.1.x it i now running
in a eparate JVM from the parent application, which i cauing all ort of headache (e.g., we can't pa our own
object acro the JVM boundary). there a way to force ue of the ame JVM a the parent app? We're currently
uing Jetty 6.1.5 with Java 5.
Thank for any help! -- chiro Furuato
13.
Oct 12, 2007
Christophe HamerIing says:
Hi it poible to add ervlet to a context when the erver i running ? Th...
Hi
it poible to add ervlet to a context when the erver i running ?
9 mbeddIng jetty jetty Codehaus
07/07/2011 02.12.05 AM http.//docs.codehaus.org/dIspIay/jTTY/mbeddIng+jetty
Thank
14.
Nov 25, 2007
L. Oproman says:
Can omeone help me put together an example of creating a URL rewriting handler ...
Can omeone help me put together an example of creating a URL rewriting handler in embedded mode? need to
have a URL like:
http://localhot:8080/group/1234567
rewrite to
http://localhot:8080/group/group?id=1234567
The "pattern" argument confue me becaue it eem to be proprietary to Jetty v. being baed on regular
expreion. couldn't find documentation on thi anywhere, and there are not many comment in the code.
COTACT THE CORE JETTY DEVELOPERS AT WWW.WEBTIDE.COM
private support for your interna/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scaabiity
guidance for your apps and Ajax/Comet projects ... deveopment services from 1 day to fu product deivery
Powered by a free AtIassian ConfIuence Open Source Project License granted to Codehau. Evaluate Confluence today.
Printed by Atassian Confuence 3.3, the Enterprise Wiki.

You might also like