Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple webroot folders with Jetty

Tags:

java

jetty

I'm using Jetty (version 6.1.22) to service a Java web application. I would like to make Jetty look in two different folders for web resources. Take this layout:

+- project1
|   +- src
|       +- main
|           +- webapp
|               +- first.jsp
|
+- project2
    +- src
        +- main
            +- webapp
                +- second.jsp

I would like to make Jetty serve both URLs:

  • http://localhost/web/first.jsp
  • http://localhost/web/second.jsp

I tried starting Jetty like this:

Server server = new Server();
SocketConnector connector = new SocketConnector();
connector.setPort(80);
server.setConnectors(new Connector[] { connector });

WebAppContext contextWeb1 = new WebAppContext();
contextWeb1.setContextPath("/web");
contextWeb1.setWar("project1/src/main/webapp");
server.addHandler(contextWeb1);

WebAppContext contextWeb2 = new WebAppContext();
contextWeb2.setContextPath("/web");
contextWeb2.setWar("project2/src/main/webapp");
server.addHandler(contextWeb2);

server.start();

But it only serves first.jsp, and it returns 404 for second.jsp.

How can I get this to work? I would also like to stay in the same context (i.e. same ClassLoader, same SessionManager etc.).

like image 278
Lóránt Pintér Avatar asked Jan 23 '23 11:01

Lóránt Pintér


1 Answers

Since 6.1.12, this is supported by using a ResourceCollection to the WebAppContext's base resource:

Server server = new Server(80);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
ResourceCollection resources = new ResourceCollection(new String[] {
    "project1/src/main/webapp", 
    "project2/src/main/webapp", 
});
context.setBaseResource(resources);
server.setHandler(context);
server.start();

More info: http://docs.codehaus.org/display/JETTY/Multiple+WebApp+Source+Directory

like image 152
Lóránt Pintér Avatar answered Feb 03 '23 08:02

Lóránt Pintér