About Steve Hanson

Steve is a software developer interested in web development and new technologies. He currently works as a Java consultant at Credera in Dallas, TX.

Simple Gradle Web Application

Gradle easily supports Java web applications through the “war” and “jetty” plugins. This tutorial will show you how to quickly make a simple Java Servlet web application.
 
 
 
 
 
 
 
 
 
 
Our basic project structure will be:

basic-web
  src
    main
      java
        com
          codetutr
            HelloWorldServlet.java
      webapp
        WEB-INF
          web.xml

First, create a folder called basic-web, and then let’s create the Gradle build file inside the folder: build.gradle

apply plugin: 'java' 
apply plugin: 'war' 
apply plugin: 'jetty' 
apply plugin: 'eclipse-wtp'

repositories {
  mavenCentral()
}

dependencies {
  providedCompile 'javax.servlet:servlet-api:2.5'
  runtime 'javax.servlet:jstl:1.1.2'
}

The eclipse-wtp plugin allows us to import the project into Eclipse as a Dynamic Web Project to be run from Eclipse if desired. The war plugin extends the java plugin and adds support for packaging the WAR file. The jetty plugin adds tasks to support running our Gradle project in an embedded Jetty instance from our build-file. Notice that we are including the servlet-api as a “providedCompile” dependency. This means we do not want it to be packaged in the WAR, because it will be included in the application server in which it will run. Now, let’s create the web.xml descriptor file in src/main/webapp/WEB-INF: web.xml

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

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

</web-app>

We set up a simple servlet backed by com.codetutr.HelloWorldServlet and mapped to “/”. Finally, let’s create the servlet in src/main/java package com.codetutr: HelloWorldServlet.java

package com.codetutr;

import java.io.IOException;

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

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());
    }
}

That’s all. We can now launch the jetty server and see our web application in action:

$ gradle jettyRunWar
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:war
:jettyRunWar

Now go to http://localhost:8080/basic-web to access your servlet. You should see “Hello, World.” on the screen. Full source: basic-web.zip Unpack the archive. Open command prompt at unarchived root. Type gradle jettyRunWar. Go to http://localhost:8080/basic-web.
 

Reference: Simple Gradle Web Application from our JCG partner Steve Hanson at the CodeTutr blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


one × = 7



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close