To Tomcat 7

Getting Tomcat7 ready

Download tomcat .tar.gz file from here. Unzip it at any convenient location which has same permissions as yours, so that deployment can be done.

Setup following variables required for functioning of Tomcat:

  • CATALINA_BASE
  • CATALINA_HOME

They should point to the directory where Tomcat bin lib webapps directories are located.

Take a look at following directories to get familiar:

  • lib directory is where all common library files are kept and are by default loaded by tomcat on boot
  • webapps - here all the applications (.war) files are deployed. On bootup, these are expanded into their respective directories.
  • bin - here the startup.sh, shutdown.sh scripts are available for you to restart the server.

Adding deployment descriptor

Deployment descriptor has two important elements

  • Context paths and their mapping to servlet
  • Context paths and their mapping to filters

Also, it has to comply to the schema definition, which in our case shall comply to 3.1 version of javax.servlet-api

Note in the definitions of above, first a servlet/filter is defined and given a name and later this is used for mapping to a particular path.

Example from our code is given below:

<?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">
  <servlet>
    <display-name>HelloWorldServlet</display-name>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>com.dakinegroup.helloworld.HelloWorldServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

</web-app>

Adding Test Code for this servlet

HelloWorldServlet is created in another sub-package com.dakinegroup.helloworld. Also, notice here the use of annotation, which makes use of deployment descriptor redundant. You can experiment with that. For now, lets keep using deployment descriptor.

package com.dakinegroup.helloworld;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

//@WebServlet(name="hello",urlPatterns={"/hello"})
public class HelloWorldServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {
        resp.getOutputStream().write("Hello World.".getBytes());
    }
}

Modifying build.gradle

We need to add “providedCompile” dependency for javax.servlet-api:3.0.1, as that is provided by tomcat container by default. runtime dependency is required to run jetty container, if required. We will be using the pre-existing tomcat7 container in this case.

Caution Do not load 3.1 of servlet api, as it is compatible with Tomcat 8.0. If used, it can cause errors.

apply plugin: 'java'
...
apply plugin: 'war'
..
..
dependencies {
    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    runtime 'javax.servlet:jstl:1.1.2'
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    ...
    ..
}

Now run grad le build root project directory. Later deploy is manually by copying it to webapps directory in tomcat7 and restart container.

$ gradle war
$ cp build/lib/StoresWithMaven.war /opt/tomcat/webapps
$ /opt/tomcat/bin/shutdown.sh
$ /opt/tomcat/bin/startup.sh

Now goto, http://localhost:8080/StoresWithMaven/hello to see the page rendered by HelloWorldServlet.

Automate war deployment

Copy war

Lets modify war task using generic task methods, doLast, inputs.file, outputs.file. Inputs and outputs properties are used to determine if there is any change and hence need to execute the task. Later within doLast, we have written copy spec and called project.copy method.

war {
    inputs.file 'build/libs/StoresWithMaven.war'
    outputs.file '/opt/tomcat/apache-tomcat-7.0.47/webapps'

    doLast {
    println "Copying.."
    copy {
            from 'build/libs/StoresWithMaven.war'
            into '/opt/tomcat/apache-tomcat-7.0.47/webapps'
        }    
    }
}

Remove index.html

$ rm StoresWithMaven/src/main/resources/static/index.html

Reason: When we load / context, it shall give priority to index.html from static folder, than will come to context classes.

Restart Tomcat

We have to manually restart tomcat