Quickstart for Java 7 for App Engine Standard Environment

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

Before you begin

Before running and deploying this sample, you must:

  1. Download Apache Maven version 3.3.9 or greater:

    Download Apache Maven

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

  3. Download and install git.

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 create a GCP project and App Engine application.

  1. Create a new GCP project and App Engine application using the GCP Console:

    Go to App Engine

    When prompted, select the region where you want your App Engine application located. After your App Engine application is created, the Dashboard opens.

  2. Open the src/main/webapp/WEB-INF/appengine-web.xml file in an editor and specify the project ID that you just created within the <application> elements. Tip: You can also use the <version> elements in this file to define an ID that uniquely identifies that version of your app.

  3. Upload your application to App Engine by running the following command:

    mvn appengine:update
    
  4. Your app is now running in App Engine and ready to serve traffic at http://[YOUR_PROJECT_ID].appspot.com/.

App Engine locations

App Engine is regional, which means the infrastructure that runs your apps is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your apps are run. You can generally select the region nearest to your app's users but you should consider the location of the other GCP products and services that are used by your app. Using services across multiple locations can affect your app's latency as well as pricing.

App Engine is available in the following regions:

  • us-central1 (Iowa)
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • southamerica-east1 (São Paulo) *
  • europe-west1 (Belgium)
  • europe-west2 (London)
  • europe-west3 (Frankfurt)
  • asia-northeast1 (Tokyo)
  • asia-south1 (Mumbai)
  • australia-southeast1 (Sydney)

* For customers using the São Paulo region, all regional product SLAs remain in force. However, multi-region and cross-region functionality that span across North America and South America might temporarily have reduced availability or performance.

You cannot change an app's region after you set it.

If you already created an App Engine application, you can view the region by running the gcloud app describe command or opening the App Engine Dashboard in the GCP Console. The region of your App Engine application is listed under 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, you can delete your GCP project to stop billing for all the resources used within that project.

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

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>appengine-doc-samples</artifactId>
    <version>1.0.0</version>
    <relativePath>..</relativePath>
  </parent>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <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="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.example.appengine.helloworld.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Learn the whole platform

Now that you know what it's like to develop and deploy App Engine apps, you can stretch out and see the rest of Google Cloud Platform. For a guided walkthrough that teaches you how to create an application that uses the entire platform, not just App Engine, check out our "Creating a Guestbook" quickstart in which you expand this simple application to become a fully-fledged Guestbook application that lets authenticated Google accounts post messages to a public page.

Learn more about App Engine standard environment

Here are some topics to help you to continue learning about App Engine.

Send feedback about...

App Engine standard environment for Java