--Google App Engine Java 8 standard environment + Servlet API 3.1 configuration to deploy simple Servlets and JSPs (Java Server Pages)
--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.
├── 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>
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)
You can start the local server http: // localhost: 8080 / with gradle appengineRun.
$ gradle appengineRun
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
Recommended Posts