This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Wicket without web.xml (embedded Jetty)
Skip to end of metadata
Go to start of metadata

Table of contents

Motivation

There are situation, when you don't want to export your application as a WAR file and have the conventional WAR structure. Or for any other reason you want to avoid using web.xml file. In the following we configure an embedded version of Jetty without using web.xml.

The following code should work with Wicket 1.5 and Jetty 8.

Configuring Jetty

Following is an example of web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<display-name>myproject</display-name>

	<!--
		There are three means to configure Wickets configuration mode and they 
		are tested in the order given.
		
		1) A system property: -Dwicket.configuration 
		2) servlet specific <init-param> 
		3) context specific <context-param>

		The value might be either "development" (reloading when templates change) or 
		"deployment". If no configuration is found, "development" is the default. -->

	<filter>
		<filter-name>wicket.myproject</filter-name>
		<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
		<init-param>
			<param-name>applicationClassName</param-name>
			<param-value>com.mycompany.WicketApplication</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>wicket.myproject</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

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

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

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

WicketFilter automatically passes the requests, which it cannot (or does not want to) process down the chain. In that case you can simply define another Servlet and add it to ServletContextHandler. This can be used, for example, if you want to access static data accessible under /static/ folder of your application:

// Static resources
String staticPath = this.getClass().getClassLoader().getResource("static/").toExternalForm();
ServletHolder resourceServlet = new ServletHolder(DefaultServlet.class);
resourceServlet.setInitParameter("dirAllowed", "true");
resourceServlet.setInitParameter("resourceBase", staticPath);
resourceServlet.setInitParameter("pathInfoOnly", "true");

Using the following code if you haven't already configured Wicket to handle a request such as localhost:8080/static/js/jquery.js, it will be passed down the handler chain to resourceServlet and it will retrieve the desired file from staticPath. For more init parameters see DefaultServlet

  • No labels