In this part of the tutorial, we'll create an app that integrates with Google Accounts so users can sign in using their Google accounts. In App Engine, the integration with Google accounts is achieved via the App Engine Users service. We'll use this to personalize our application's greeting.
This is what the app will look like, in this part of the tutorial (we'll add more later):

This application consists of these main logical "parts":
- A JSP page that the user interacts with to make requests to the app.
- A servlet named GuestbookServlet.java that, if necessary, prompts the user to log in and, once the user is logged in, displays a personalized greeting.
Creating the UI using JSP
To create the UI:
-
In
guestbook/src/main/webapp, create a file namedguestbook.jspwith the following contents:Notice the imports for the App Engine
Usersservice. Also, by default, any file inwebapp/or in a subdirectory other thanWEB-INF/that has the file suffix.jspis automatically mapped to a URL path consisting of the path to the.jspfile, including the filename. This JSP will be mapped automatically to the URL/guestbook.jsp. -
In
guestbook/src/main/webapp, create a directory namedstylesheets, and create a file namedmain.csswith the following contents: -
Proceed to
web.xmlconfiguration, described next.
Configuring web.xml
To configure the web.xml file:
-
In
guestbook/src/main/webapp/WEB-INF, openweb.xmlin a text editor, and replace the contents of the file with the following: This configuration maps the servlet to its serving location and specifies the JSP file you created to be the application home page. For more information about theweb.xmlfile and how to use it, see the Deployment Descriptor page. -
Proceed to servlet creation, described next.
Creating the servlet GuestbookServlet.java
App Engine Java applications use the Java Servlet API to interact with the web server. An HTTP servlet is an application class that can process and respond to web requests. This class extends either the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class.
To create the servlet:
-
In
guestbook/src/main/java, create the subdirectoriescom/example/guestbookby invoking the following command (in a Linux or Mac OSX terminal window):mkdir -p com/example/guestbook -
In
guestbook/src/main/java/com/example/guestbook, create a file namedGuestbookServlet.java. -
Add the following contents to the file: The servlet checks whether the user is logged on. If the user is logged on, the servlet displays a personalized greeting; otherwise the user is redirected to the logon page.
-
You project should look like this:

Your app is now ready to build and test the app locally on the development server, which is described next.
Building and testing the app
To build and test the app:
-
Change directory to
guestbook, and invoke the command:mvn clean installWait for the build to complete.
-
Run the app in the development server on your local machine by invoking this command from
/guestbook:mvn appengine:devserverWait for the success message, which looks something like this:
[INFO] INFO: The admin console is running at http://localhost:8080/_ah/admin [INFO] Aug 18, 2014 5:35:04 PM com.google.appengine.tools.development.DevAppServerImpl doStart [INFO] INFO: Dev App Server is now running -
In a browser that is running on the same machine as your terminal window, visit
localhost:8080to access the app on your local machine. If prompted, click Sign in. -
In the login form supply an email or accept the
test@example.comdummy email and click Log In. (When run locally, there is no validity check.) Note that no login form appears if you're already logged in to the app (as might happen due to cookies retained in your browser from previous interactions); in this case you'll be take directly to step 5. -
Observe that the greeting now displays your email address.
-
You have successfully created a simple Java App Engine app. You are ready to do something a bit more useful next, adding the ability to accept and store user posts in a database.