Using Jetty - MTDdk/jawn GitHub Wiki
Deploying an application of the jawn framework into a running Jetty web container is simply a matter of creating a WAR, which is easily done with either Gradle or Maven, but starting and maintaing a Jetty server is out of the scope of this wiki. See Jettys own documentation on this.
Embedding
One of the design mottoes of Jetty is:
Don’t put your application into Jetty, put Jetty into your application.
This means, that you can create a stand-alone service running the jawn framework on top of your own application, or you simply use the framework to serve a simple website.
For a stand-alone application you need a main-class that will start the server and serve the framework.
This is an in-production running main-class:
public class JettyMain {
private static final int DEFAULT_PORT = 20007;
public static void main(String... args) throws Exception {
if (args.length > 0) {
//defaults to development ([development | production])
System.setProperty("JAWN_ENV", args[0]);
String isDevelopment = Boolean.toString("development".equals(args[0]));
System.setProperty("jawn_reload", isDevelopment);
}
new JettyMain().startJetty();
}
private void startJetty() throws Exception {
Server server = new Server(DEFAULT_PORT);
// context
// includes standard serving of static content
// assigned by the framework
WebAppContext contextHandler = new WebAppContext();
// include the framework
// this is actually what you would set in the web.xml
FilterHolder filter = contextHandler.addFilter(
/*The framework*/
RequestDispatcher.class,
/*It should handle all requests*/
"/*",
/*Of all types*/
EnumSet.allOf(DispatcherType.class));
filter.setInitParameter("root_controller", "index");
// set the context path to just a slash
contextHandler.setContextPath("/");
// point to the webapp folder
contextHandler.setBaseResource(Resource.newResource("src/main/webapp"));
// disables directory listing
contextHandler
.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
contextHandler.setParentLoaderPriority(true);
contextHandler.configure();
// add the handler to the server
server.setHandler(contextHandler);
// start the server
server.start();
server.join();
}
}
All dependencies used in this example are of course included by standard in the framework.
Parameters
It can be seen that the framework uses some system variables explained in environments.
Furthermore it has to have the default name of the root controller set.
If you are not content with the standard controller named IndexController
, then change the
root_controller
parameter to something else.
Webapp
Notice that a reference to the webapp
is set. This could practically be anywhere on the disk.
If a web.xml is present in the webapp/WEB-INF
then the contextHandler.addFilter(..)
is not
necessary - the web.xml handles all of that.
Gradle
In the example project jawn-simple it is seen how to use Gradle as project management tool and it can also be used to start up a Jetty containerserver running the application.
The server is simply configured in the build.gradle
:
/* ******************* */
/* Jetty webserver */
/* ******************* */
jettyRun {
contextPath = ''
reload = 'manual' // 'manual' reloads the entire webapp when pressing 'enter'
scanIntervalSeconds = 100 // only used with reload = 'automatic'
// can be set as environment variables instead
System.setProperty("JAWN_ENV","development")
System.setProperty("jawn_reload","true")
}
Remember that the system properties can be set as a part of the server environment variables instead, if they have to be global for all applications on the server.
web.xml
When using a container to host your web application, you must have a web.xml defined
(conventionally placed at src/main/webapp/WEB-INF/web.xml
, but configurations can alter this).
An example hereof can be seen in Configuration options