Hello World with Google App Engine (Java 8) + Servlet API 3.1 + Gradle

Overview

--Google App Engine Java 8 standard environment + Servlet API 3.1 configuration to deploy simple Servlets and JSPs (Java Server Pages)

environment

--Google App Engine Java 8 standard environment

Google App Engine Java 8 standard environment

Jetty 9 + Servlet API 3.1 (or 2.5) can be used. As a developer, you can create a war file and deploy it, so you don't have to worry about Jetty.

Migrating from Java 7 to Java 8 Runtime  |  App Engine standard environment for Java 8  |  Google Cloud

Jetty 9 supports both Servlet 2.5 and 3.1 web applications, including servlet annotations.

Source code

Source code list

├── build.gradle
├── settings.gradle
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── HelloServlet.java
        └── webapp
            ├── WEB-INF
            │   └── appengine-web.xml
            └── index.jsp

build.gradle

Gradle build configuration file.

build.gradle


buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    //Use Google App Engine Gradle plugin
    classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.+'
  }
}

repositories {
  mavenCentral()
}

apply plugin: 'java'
apply plugin: 'war'

// App Engine tasks
apply plugin: 'com.google.cloud.tools.appengine'

dependencies {

  //Use Apache Commons Text for HTML escaping
  implementation 'org.apache.commons:commons-text:1.8'

  // Servlet API 3.Use 1
  providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
}

group = "com.example" // Generated output GroupId
version = "1.0.0" // Version in generated output

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

settings.gradle

settings.gradle


rootProject.name = 'hello'

HelloServlet.java

A Servlet that returns HTML when accessing https://project ID.appspot.com/hello.

package com.example;

import org.apache.commons.text.StringEscapeUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    resp.setContentType("text/html; charset=utf-8");

    PrintWriter out = resp.getWriter();
    out.println("<html><body>");
    out.println("<h1>Hello, Servlet World!</h1>");

    //Print system properties
    out.println("<h2>System Properties</h2>");
    Properties props = System.getProperties();
    for (Object k : new TreeMap<>(props).keySet()) {
      String key = (String) k;
      out.println(h(key) + ": " + h((String) props.get(key)) + "<br>");
    }

    //Output environment variables
    out.println("<h2>Environment Variables</h2>");
    Map<String, String> env = System.getenv();
    for (String key : new TreeMap<>(env).keySet()) {
      out.println(h(key) + ": " + h(env.get(key)) + "<br>");
    }

    //Output runtime information
    out.println("<h2>Runtime.getRuntime()</h2>");
    Runtime r = Runtime.getRuntime();
    out.println("freeMemory: " + r.freeMemory() + "<br>");
    out.println("maxMemory: " + r.maxMemory() + "<br>");
    out.println("totalMemory: " + r.totalMemory() + "<br>");

    out.println("</body></html>");
    out.flush();
    out.close();
  }

  private static String h(String s) {
    return StringEscapeUtils.escapeHtml4(s);
  }
}

index.jsp

A JSP file that returns HTML when accessing https://project ID.appspot.com/.

index.jsp


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hello, JSP!</title>
</head>
<body>
<p>Hello, JSP!</p>
</body>
</html>

appengine-web.xml

appengine-web.xml


<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <runtime>java8</runtime>
  <threadsafe>true</threadsafe>
  <instance-class>F1</instance-class>
  <automatic-scaling>
    <target-cpu-utilization>0.95</target-cpu-utilization>
    <min-instances>0</min-instances>
    <max-instances>1</max-instances>
    <max-concurrent-requests>10</max-concurrent-requests>
  </automatic-scaling>
  <!--System properties can be set-->
  <system-properties>
    <property name="com.example.HelloServlet.message" value="Hello, system property."/>
  </system-properties>
  <!--Environment variables can be set-->
  <env-variables>
    <env-var name="COM_EXAMPLE_HELLOSERVLET_MESSAGE" value="Hello, environment variable." />
  </env-variables>
</appengine-web-app>

operation

Google App Engine Gradle plugin tasks

You can deploy using the functions of Google App Engine Gradle plugin. You can check the tasks that can be executed with the gradle tasks command.

$ gradle tasks

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

App Engine Standard environment tasks
-------------------------------------
appengineDeploy - Deploy an App Engine application
appengineDeployCron - Deploy Cron configuration
appengineDeployDispatch - Deploy Dispatch configuration
appengineDeployDos - Deploy Dos configuration
appengineDeployIndex - Deploy Index configuration
appengineDeployQueue - Deploy Queue configuration
appengineRun - Run an App Engine standard environment application locally
appengineShowConfiguration - Show current App Engine plugin configuration
appengineStage - Stage an App Engine standard environment application for deployment
appengineStart - Run an App Engine standard environment application locally in the background
appengineStop - Stop a locally running App Engine standard environment application
explodeWar - Explode a war into a directory
(The following is omitted)

Start the server locally with gradle appengineRun

You can start the local server http: // localhost: 8080 / with gradle appengineRun.

$ gradle appengineRun

Deploy to Google App Engine with gradle appengineDeploy

You can deploy to Google App Engine with gradle appengineDeploy. Before deploying, specify the project ID of the deployment destination in "gcloud config set project project ID".

$ gradle appengineDeploy

Reference material

Recommended Posts

Hello World with Google App Engine (Java 8) + Servlet API 3.1 + Gradle
Hello World with Google App Engine (Java 8) + Spring Boot + Gradle
Hello World with Google App Engine (Java 11) + Spring Boot + Gradle
Google App Engine development with Docker
Java 1 1 support from Google App Engine
Building a development environment with Maven on Google App Engine [Java]
Using properties files with Flexible Environment Java 8 on Google App Engine
I can't deploy with google app engine
Tweet (API 1.1) on Google App Engine for Python
Google App Engine Datastore and Search API integration
hello world with ctypes
Hello, World with Docker
Hello world with flask
PIL with Python on Windows 8 (for Google App Engine)
Getting Started with Google App Engine for Python & PHP
Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Until hello world with zappa
Deploy your Go app on Google App Engine with GitHub Actions
Python starting with Hello world!
Hello, world! With virtual CAN communication
[Note] Hello world output with python
Use ndb.tasklet on Google App Engine
Hello World! By QPython with Braincrash
Introducing Google Map API with rails
Deploy a Python app on Google App Engine and integrate it with GitHub
Hello World and face detection with opencv-python 4.2
Streaming speech recognition with Google Cloud Speech API
Hello World with Raspberry Pi + Minecraft Pi Edition
[Python] Run Flask on Google App Engine
[Google App Engine] User Objects (Japanese translation)
Use external modules on Google App Engine
Get holidays with the Google Calendar API
Hello World! By QPython with Brainfu * k
A web app that just does Hello World with Go's net / http package