Jettyを使ってWebアプリケーションをスタンドアロン動作可能にする
Eclipseで開発しているときに、特別なプラグインを入れることなく Webアプリケーションを Run なり Debugなりで単独走行させる方法。
Servletだけ動けばいい人は下記をclasspathに追加
jetty-6.1.1.jar
jetty-util-6.1.1.jar
commons-logging-1.1.jar
JSPも要る人は下記も追加
jasper-compiler-5.5.15.jar
jasper-runtime-5.5.15.jar
jsp-api-2.0.jar
commons-el-1.0.jar
jasper-compiler-jdt-5.5.15.jar
自分のWebアプリケーションに適当なMainクラスを作ってやる。これみたいに。
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.webapp.WebAppContext;
public class Main {
public static void main(String[] args) throws Exception
{
Server server = new Server();
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
SocketConnector connector = new SocketConnector();
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
WebAppContext webapp = new WebAppContext();
webapp.setResourceBase("src/webapp");
webapp.setClassLoader(Main.class.getClassLoader());
webapp.setContextPath("/mycontext");
// web.xmlの内容をオーバーライドしたい時はこういう風にする
java.util.TreeMap map = new java.util.TreeMap();
map.put("contextConfigLocation", "/WEB-INF/applicationContext.mylocal.xml");
webapp.setInitParams(map);
contexts.addHandler(webapp);
try {
server.start();
}
catch (java.net.BindException ex) {
System.exit(-1);
}
}
}
追記:WebアプリケーションからJNDIデータソースも利用したい場合
Servletだけ動けばいい人は下記をclasspathに追加
jetty-6.1.1.jar
jetty-util-6.1.1.jar
commons-logging-1.1.jar
JSPも要る人は下記も追加
jasper-compiler-5.5.15.jar
jasper-runtime-5.5.15.jar
jsp-api-2.0.jar
commons-el-1.0.jar
jasper-compiler-jdt-5.5.15.jar
自分のWebアプリケーションに適当なMainクラスを作ってやる。これみたいに。
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.webapp.WebAppContext;
public class Main {
public static void main(String[] args) throws Exception
{
Server server = new Server();
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
SocketConnector connector = new SocketConnector();
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
WebAppContext webapp = new WebAppContext();
webapp.setResourceBase("src/webapp");
webapp.setClassLoader(Main.class.getClassLoader());
webapp.setContextPath("/mycontext");
// web.xmlの内容をオーバーライドしたい時はこういう風にする
java.util.TreeMap map = new java.util.TreeMap();
map.put("contextConfigLocation", "/WEB-INF/applicationContext.mylocal.xml");
webapp.setInitParams(map);
contexts.addHandler(webapp);
try {
server.start();
}
catch (java.net.BindException ex) {
System.exit(-1);
}
}
}
追記:WebアプリケーションからJNDIデータソースも利用したい場合

0 Comments:
Post a Comment
<< Home