Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using web.xml to conf programmatically started jetty

I created an eclipse maven project and added jetty dependency. Next I made a simple servlet and a class that starts the jetty server. Here is what i got so far:

package com.example.jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class App {
    public static void main(String[] args) throws Exception {
        Server server = new Server(80);
        ServletContextHandler servletContext = new ServletContextHandler(server, "/");
        servletContext.addServlet(MyServlet.class, "/");
        server.start();
    }
}

My problem is that most tutorials I see have a web.xml to configure servlets and such. I can't find programmatic ways to do the some of these. Can I create a web.xml and still start my jetty programatically and somehow use that web.xml for configuration?

To be more specific i need to write true in web.xml. I did not find any way to do it programatically.

like image 587
user1985273 Avatar asked Oct 14 '16 14:10

user1985273


1 Answers

I'll start with an example that you may interested in. If you want to use web.xml with programmatic way Jetty server, then you can do the following:

WebAppContext context = new WebAppContext();
context.setContextPath("/myWebApp");
context.setExtractWAR(false);
context.setDescriptor("/file/system/path/to/your/wab/app/WEB-INF/web.xml");
context.setResourceBase("/file/system/path/to/your/wab/app");
context.setConfigurationDiscovered(false);

HandlerList handlerList=new HandlerList();
handlerList.addHandler(webAppContext);

Server server = new Server(threadPool);
server.setHandler(handlerList);
server.start();

As regards to programmatically configuration you can try to use Servlet 3.x API, that is supported from Jetty 8.x (current Jetty version 9.x) and can be fully configured programmatically.

like image 93
Sergey Bespalov Avatar answered Oct 28 '22 09:10

Sergey Bespalov