If you're only using the web functionality provided by an application server, or you want to optimize HTTP performance, it would be a good idea to consider embedding the Jetty HTTP server inside your application. This way, you don't even have to generate a WAR-file and a web.xml file. The application is just a basic Java application, which you can run on any server with a Java VM.
Maven setup
If you're using a Maven project, add the following dependencies to the pom-file in the "dependencies"-element in order to start working right away:
<dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-hightide</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-core</artifactId> <version>1.5.3</version> </dependency>
Minimal Wicket application
For our example, we use a minimal Wicket application that consists of one page.
The ExampleWicketApplication.java file:package com.javaeenotes; import org.apache.wicket.Page; import org.apache.wicket.protocol.http.WebApplication; public class ExampleWicketApplication extends WebApplication { @Override public Class<? extends Page> getHomePage() { return HomePage.class; } }The HomePage.java file:
package com.javaeenotes; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.basic.Label; public class HomePage extends WebPage { public HomePage() { add(new Label("message", "Hello World!")); } }The HomePage.html file:
<html> <body> <span wicket:id="message"></span> </body> </html>
The final class we need is a main class with the static main-method, in which we tie the Wicket servlet to the Jetty server.
The Main.java file:package com.javaeenotes; import org.apache.wicket.protocol.http.WicketFilter; import org.apache.wicket.protocol.http.WicketServlet; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.bio.SocketConnector; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppContext; public class Main { public static void main(String[] args) { // Object that holds, and configures the WicketServlet. ServletHolder servletHolder = new ServletHolder(new WicketServlet()); servletHolder.setInitParameter( "applicationClassName", "com.javaeenotes.ExampleWicketApplication"); servletHolder.setInitParameter( WicketFilter.FILTER_MAPPING_PARAM, "/*"); servletHolder.setInitOrder(1); // Web context configuration. WebAppContext context = new WebAppContext(); context.addServlet(servletHolder, "/*"); context.setResourceBase("."); // Web root directory. // The HTTP-server on port 8080. Server server = new Server(); SocketConnector connector = new SocketConnector(); connector.setPort(8080); server.setConnectors(new Connector[]{connector}); server.setHandler(context); try { // Start HTTP-server. server.start(); } catch (Exception e) { e.printStackTrace(); } } }
Run the main method in a VM, and connect to port 8080 with a browser to check that the application is working.
This comment has been removed by the author.
ReplyDeleteHello and thank you very much for such a good article! It really helped me a lot.
DeleteThere was a small problem when I tried to run code described here. Wicket started, but I stuck with error: "Last cause: Can not determine Markup. Component is not yet connected to a parent".
After some research I found that *.html files are not copied to the build target.
So I added a "src/main/java" directory to resources section in the *.pom, and it solved the problem.
Maybe for an experienced professional it's obvious, but I think for newbies like me this information could be useful. Hope it helps somebody :)
Hello and thank you very much for such a good article! It really helped me a lot.
ReplyDelete