You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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

Adding Wicket Filter to Jetty

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, WinterfaceApplication.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.

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