Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

What we need to do is simply configure embedded Jetty to exhibit the same behaviour in case of missing web.xml

...

Configure Jetty to start WebApplication

Using WicketServlet

Code Block
server = new Server();
/* Setup server (port, etc.) */
ServletContextHandler sch = new ServletContextHandler(ServletContextHandler.SESSIONS);
ServletHolder sh = new ServletHolder(WicketServlet.class);
sh.setInitParameter(ContextParamWebApplicationFactory.APP_CLASS_PARAM, WicketApplication.class.getName());
sh.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
/* Define a variable DEV_MODE and set to false
 * if wicket should be used in deployment mode
 */
if(!DEV_MODE) {
	sh.setInitParameter("wicket.configuration", "deployment");
}
sch.addServlet(sh, "/*");
server.setHandler(sch)

The Code above configures Jetty in a manner similar to given web.xml file. Note that we use a ServletHolder which encapsulates both context and class of a servlet. The ServletHolder is then added to ServletContextHandler which as finally added as the only handler to our Jetty Server.

You may wonder where WicketServlet came from, since it is not explicitly in web.xml. It is important to note that even if in web.xml there is only a filter defined, jetty needs a servlet to serve the requests. WicketServlet is an implementation of servlet, which contains WicketFilter to filter requests. If you want to have more control over WicketFilter see next part.

Using WicketFilter

Code Block

ServletContextHandler sch = new ServletContextHandler(ServletContextHandler.SESSIONS);
FilterHolder fh2 = new FilterHolder(WicketFilter.class);
fh2.setInitParameter(ContextParamWebApplicationFactory.APP_CLASS_PARAM, WicketApplication.class.getName());
fh2.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
sch.addFilter(fh2, "/*", EnumSet.of(DispatcherType.REQUEST,DispatcherType.ERROR));
sch.addServlet(DefaultServlet.class, "/*");

As you can see, since we need a servlet to serve the request we just added a DefaultServlet.

Extra Servlet for static resources

...