Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

servlets

This module exists for strict environments where the ONLY option is to deploy into a Servlet Container. If you are free to deploy a new server technology, we strongly recommend to avoid this and go directly with netty, jetty or undertow.

prepare

In order to deploy into a Servlet Container, we need to generate a *.war file. The next steps guide you how to do it:

step 1

Add the jooby-servlet dependency to your pom.xml:

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-servlet</artifactId>
  <version>0.12.0</version>
  <scope>provided</scope>
</dependency>

IMPORTANT: Scope must be provided.

step 2

Find out the maven-assembly-plugin section in your pom.xml, it looks like this:

<!-- Build jooby.zip | jooby.war -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jooby.zip</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

Add a new descriptorRef: jooby.war

<!-- Build jooby.zip | jooby.war -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jooby.zip</descriptorRef>
      <descriptorRef>jooby.war</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

step 3

Run: mvn clean package and find the *.war file in the target directory.

limitations

  • web-sockets are not supported
  • some properties has no effect when deploying into a Servlet Container:
  • application.path / contextPath
  • appplication.port
  • max upload file sizes
  • any other server specific property: server., jetty., netty., undertow.

special note on contextPath

To avoid a headache... make sure to use the contextPath variable while loading static/dynamic resources (.css, .js, etc..).

For example:

<html>
<head>
  <link rel="stylesheet" text="text/css" href="{{contextPath}}/css/styles.css">
  <script src="{{contextPath}}/js/app.js"></script>
</head>
</html>

Here the expression: {{ "{{contextPath" }}}} correspond to the template engine (handlebars here) or ${contextPath} for Freemarker.

how it works?

The maven-assembly-plugin generates the *.war file. The assembly descriptor can be found here

web.xml

A default web.xml file is generated by the assembly plugin. File looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">
  <context-param>
    <param-name>application.class</param-name>
    <param-value>${application.class}</param-value>
  </context-param>

  <listener>
    <listener-class>org.jooby.servlet.ServerInitializer</listener-class>
  </listener>

  <servlet>
    <servlet-name>jooby</servlet-name>
    <servlet-class>org.jooby.servlet.ServletHandler</servlet-class>
    <load-on-startup>0</load-on-startup>
    <!-- MultiPart setup -->
    <multipart-config>
      <file-size-threshold>0</file-size-threshold>
      <!-- Default 200k -->
      <max-request-size>${war.maxRequestSize}</max-request-size>
    </multipart-config>
  </servlet>

  <servlet-mapping>
    <servlet-name>jooby</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

upload size

Default upload size is set to 204800b (200kb). If you need to increase the upload size, add the war.maxRequestSize property to pom.xml:

<properties>
  <war.maxRequestSize>1048576</war.maxRequestSize> <!-- 1mb -->
</properties>

custom web.xml

When the generated file isn't enough, follow these steps:

  1. create a dir: src/etc/war/WEB-INF
  2. save a web.xml file inside that dir
  3. run: mvn clean package

That's all folks! Enjoy it!!!