Quickstart for App Engine Standard Environment for Java

This quickstart shows you how to create a small App Engine application that displays a short message.

Before you begin

Before running this sample, you must:

  1. Download and install the Java SE Development Kit (JDK):

    Download JDK

  2. Download Apache Maven version 3.3.9 or greater:

    Download Apache Maven

  3. Install and configure Maven for your local development environment.

Download the Hello World app

We've created a simple Hello World app for Java so you can quickly get a feel for deploying an app to Google Cloud Platform. Follow these steps to download Hello World to your local machine.

  1. Clone the Hello World sample app repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    
  2. Go to the directory that contains the sample code:

    cd java-docs-samples/appengine/helloworld
    
  3. In the resulting helloworld files you'll find the src directory for a package called com.example.appengine.helloworld that implements a simple HTTPServlet.

Alternatively, you can download the sample as a zip file and extract it.

Test the application

Test the application using the local development server, which is included with the App Engine SDK.

  1. From within your helloworld directory, run the following Maven command to compile your app and start the local development server:

    mvn appengine:devserver
    

    The development server is now listening for requests on port 8080.

  2. Visit http://localhost:8080/ in your web browser to see the app in action.

For more information about running the local development server, see the Java Development Server reference.

Make a change

You can leave the development server running while you develop your application. When you make a change, use the mvn clean package command to build and update your app.

  1. Try it now: Leave the development server running, then edit HelloServlet.java to change Hello, world to something else.
  2. Run mvn clean package, then reload http://localhost:8080/ to see the results.

Deploy your app

To deploy your app to App Engine, you will need to register a project to create your project ID, which will determine the URL for the app.

  1. In the Cloud Platform Console, go to the Projects page and select or create a new project.

    Go to the Projects page

  2. Note the project ID you created above, and enter it in src/main/webapp/WEB-INF/appengine-web.xml. You can also set the app version in this file.
  3. Upload your application to Google App Engine by invoking the following command.
    mvn appengine:update
  4. Your app is now deployed and ready to serve traffic at http://<YOUR_PROJECT_ID>.appspot.com/.

Congratulations!

You have completed this quickstart.

The full URL for your application is http://[YOUR_PROJECT_ID].appspot.com/. Optionally, you can purchase and use a top-level domain name for your app, or use one that you have already registered.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Go to the Cloud Platform Console.
  2. Click the trash can icon next to the project you want to shut down: this schedules the project for deletion.

What's next

Use a custom domain

You can serve your App Engine app using your own custom domain instead of appspot.com. For more information, see Using Custom Domains and SSL.

Hello World code review

Hello World is the simplest possible App Engine app: it contains only one service, and has only one version. This section describes each of the app files in detail.

HelloServlet.java

This servlet responds to any request by sending a response containing the message Hello, world!.

@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    out.println("Hello, world");
  }
}

pom.xml

The helloworld app uses Maven, which means you must specify a Project Object Model, or POM, which contains information about the project and configuration details used by Maven to build the project.

<project>
  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <groupId>com.example.appengine</groupId>
  <artifactId>appengine-helloworld</artifactId>
  <parent>
    <groupId>com.google.cloud</groupId>
    <artifactId>doc-samples</artifactId>
    <version>1.0.0</version>
    <relativePath>../..</relativePath>
  </parent>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <!-- for hot reload of the web application -->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <version>3.3</version>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
      <plugin>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${appengine.sdk.version}</version>
      </plugin>
    </plugins>
  </build>
</project>

appengine-web.xml

You will also need to configure App Engine's behavior using appengine-web.xml. Use this file to specify a version for your application, and to enter the Project ID for your Google Cloud Platform project prior to uploading the application. For now it's okay to leave the file as it is.

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>YOUR-PROJECT-ID</application>
  <version>YOUR-VERSION-ID</version>
  <threadsafe>true</threadsafe>
</appengine-web-app>

web.xml

Java web applications use a deployment descriptor file to determine how URLs map to servlets, which URLs require authentication, and other information. This file is named web.xml, and resides in the app's WAR under the WEB-INF/ directory. web.xml is part of the servlet standard for web applications.

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web=