/simple-webapp

This is a simple Java application declaring a single servlet using `WebServlet`.

Primary LanguageJava

Simple Jetty Webapp

This is a simple Java application declaring a single servlet using WebServlet. It uses:

  • Java 11

  • Jetty Maven plugin

Steps of building the application

  1. Generate project structure.

mvn archetype:generate -DgroupId=com.ic.web -DartifactId=simple-webapp -DarchetypeArtifactId=maven-archetype-webapp

  1. Load in IntelliJ IDEA.

  2. Add Servlet dependency in the pom.xml file.

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
</dependency>
  1. Add plugins configuration:

    • to build war

    • to configure app context

    • to declare Java version used

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <warSourceDirectory>src/main/webapp</warSourceDirectory>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.16.v20190411</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <webApp>
            <contextPath>/simple</contextPath>
          </webApp>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <source>12</source>
            <target>12</target>
          </configuration>
      </plugin>
</plugins>
  1. Modify the web.xml to:

<web-app
        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_3_0.xsd"
        version="3.0">
  <display-name>Archetype Created Web Application</display-name>
</web-app>
  1. Maybe test the configuration, by running:

$ mvn clean install
$ mvn jetty: run

And access http://localhost:8080/simple/. You should see 'Hello World!' in the page returned by the browser.

  1. Add the servlet class:

package com.ic.web;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


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

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter( ).write( "This is a simple Java application declaring a single servlet using `WebServlet` using Java 11 and Jetty Maven plugin." ) ;
    }
}
  1. Test the servlet by accessing http://localhost:8080/simple/info

  2. And now because the web.xml is empty anyway, just delete it.