user1985273 user1985273 - 3 months ago 33
Java Question

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, "/");

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.


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();

HandlerList handlerList=new HandlerList();

Server server = new Server(threadPool);

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.