Gradle Servlet Hello World example in Eclipse

The new project wizard in Gradle Eclipse plugin let you create Java project easily, unfortunately it only provides three templates. And there is no template for Java Web application for example the Servlet application.

Good news is you don't have to start from scratch, the Gradle distribution already contains a lot of sample applications that you can customize for your own needs.

There is a folder samples in Gradle's installation directory. What we need is gradle-2.5\samples\webApplication\quickstart.

Step 1. Import Gradle template project

Create a new directory in your Eclipse workspace, JavaServletExample . And then copy the contents of the quickstart directory to it.

Select File - Import... - Gradle project in Eclipse. Select the newly created directory and click Build Model.

Step 2. Add Servlet dependencies

The default template supports only jsp file, to add Servlet support, modify the build.gradle

 
apply plugin: 'java' 
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
 
repositories {
    mavenLocal()
    mavenCentral()
}
 
dependencies {
    compile group: 'commons-io', name: 'commons-io', version: '1.4'
    compile group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
    providedCompile 'javax.servlet:servlet-api:2.5'
    runtime 'javax.servlet:jstl:1.1.2'
 
}
 
httpPort = 8080
stopPort = 9451
stopKey = 'foo'
 
 

The servlet-api is providedCompile, it will not packed into the final war file because the Jetty servlet container already has it.

Step 3. Add Servlet and web.xml

Create a new package com.makble.servletexample and add a new class ServletHello that extends the HttpServlet, if you can find the class, click Gradle - Refresh All.

 
package com.makble.servletexample;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletHello extends HttpServlet {
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)    throws IOException{
        PrintWriter out = response.getWriter();
        out.println("<h1>Hello World</h1>");
    }
}
 
 

The quickstart template didn't provide web.xml. We should add it. Create a new folder WEB-INF under webapp and add a new file web.xml with the following content.

 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
 
  <servlet>
    <display-name>ServletHello</display-name>
    <servlet-name>ServletHello</servlet-name>
    <servlet-class>com.makble.servletexample.ServletHello</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>ServletHello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
 
</web-app>
 

Step 4. Run the application

To run the application, you can execute gradle jettyRun on command line or select tasks in Eclipse. The tasks are

 
:build, :jettyStop, :jettyRunWar
 

Now open a new tab in browser and go to http://localhost:8080/JavaServletExample/hello