Adding Application Code and UI

In this section, you'll create an app that integrates with Google Accounts so users can sign in using their Google accounts. An app can integrate with Google accounts using the App Engine Users service. You'll use this to personalize our application's greeting.

This is what the app will look like at the end of this section:


We will look at two ways to implement request handlers:

Creating a JSP template

Let's start with a simple JSP template. In guestbook/src/main/webapp, create a file named guestbook.jsp with the following contents:

By default, any file in webapp/ or in a subdirectory other than WEB-INF/ that has the file suffix .jsp is automatically mapped to a URL path consisting of the path to the .jsp file, including the filename. This JSP will be mapped automatically to the URL /guestbook.jsp.

This JSP imports the library for the App Engine Users service, and calls the service to detect whether the user is signed in, fetch the user's "nickname," and calculate URLs that the user can visit to sign in or sign out.

This template uses a CSS file. Create a directory named stylesheets in guestbook/src/main/webapp. In this new directory, create a file named main.css with the following contents:

Testing the app in a development server

You can test this JSP file in the App Engine development server without any further changes. From the guestbook directory, run the following Maven command:

mvn appengine:devserver

Wait for the success message, which looks something like this:

[INFO] INFO: Module instance default is running at http://localhost:8080/
[INFO] Apr 29, 2015 3:28:38 PM com.google.appengine.tools.development.AbstractModule startup
[INFO] INFO: The admin console is running at http://localhost:8080/_ah/admin
[INFO] Apr 29, 2015 3:28:38 PM com.google.appengine.tools.development.DevAppServerImpl doStart
[INFO] INFO: Dev App Server is now running

In your browser, visit this URL:


The app serves a page inviting you to sign in. Try clicking the "Sign in" link, then sign in with any email address. The development server emulates the Google Account sign-in process for testing purposes. While signed in, the app displays a greeting with the email address you entered.

You may notice that visiting the URL http://localhost:8080 (with no path) returns an error. So far, we have only defined a handler for the /guestbook.jsp URL path by creating this file in guestbook/src/main/webapp/. We will configure more URL handlers in the next section.

You can terminate the development server by pressing Control-C.

Creating a servlet class

App Engine Java applications use the Java Servlet API to interact with the web server. As above, JSP files act as servlets mapped to URL paths similar to their file paths. You can also implement servlets using Java servlet classes. A servlet class extends either the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class.

In the guestbook/src/main/java directory, create a package path for your servlet class called com/example/guestbook. In Linux or Mac OS X, you can run the following command:

mkdir -p com/example/guestbook

In this new directory, create a file named GuestbookServlet.java, and give it the following contents:

Your project directory structure now looks like this:

Simple Hello World

Configuring URL paths

The servlet engine needs to know which URL paths go to each servlet class. You specify this using the deployment descriptor, a file named web.xml.

Locate web.xml in the guestbook/src/main/webapp/WEB-INF directory. Edit the file, then give it the following contents:

This deployment descriptor does three things:

  • The <servlet> directive declares that the GuestbookServlet class is a servlet named guestbook.
  • The <servlet-mapping> directive associates the guestbook servlet with the URL path /guestbook.
  • The <welcome-file-list> directive tells the server to use the JSP file you created earlier to serve requests for the home page.

Stop the development server with Control-C if it is still running, then restart it (again, from the guestbook directory):

mvn appengine:devserver

Visit the /guestbook URL to see the results of the GuestbookServlet class:


This servlet demonstrates how to read query parameters with the getParameter() method. Try this by adding a testing parameter to the URL:


You can also visit the root (/) URL to see that the JSP file is now used for the home page:


Continue to the next section to add persistent data storage functionality to the guestbook app.

Storing Data in Datastore >>