13

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.

user1985273
  • 1,817
  • 15
  • 50
  • 85
  • Why not just use maven to do the job? Maven jetty-plugin can be used as an alternative solution. https://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html – qtopierw Oct 14 '16 at 15:19
  • Hum... Not quite clear to me. The programmatic way is always different of the declarative way. What do you exactly want to do? – Little Santi Oct 16 '16 at 05:18
  • To be more specific i need to write true in web.xml. I did not find any way to do it programatically. – user1985273 Oct 16 '16 at 13:48
  • 1
    Possible dup of [Configure embedded jetty with web.xml?](http://stackoverflow.com/q/20786661/2646526) – heenenee Oct 16 '16 at 17:26

1 Answers1

7

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.

Sergey Bespalov
  • 1,746
  • 1
  • 12
  • 29
  • Thank you for the anwser. Do I understand correctly that i need this servlet 3.x api dependency only if i want to configure it programmatically? Do you also know of a example how to use this api to configure the async-supported property? – user1985273 Oct 17 '16 at 05:13
  • To enable `Servlet 3.0` you need to specify `version="3.0"` in your `web.xml` everything else can be left blank, so you can place such `web.xml` in class path instead of file system. `@WebServlet` with parameter `asyncSupported = true` can be used for `async-supported`. – Sergey Bespalov Oct 17 '16 at 05:29
  • [here](https://github.com/jetty-project/embedded-servlet-3.1) you can find an example for `Jetty` with `Servlet 3.0` – Sergey Bespalov Oct 17 '16 at 05:34