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
This JSP imports the library for the App Engine
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
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:
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.
Note: If you get a runtime error when you first visit `localhost:8080`,
referring to a restricted class, for example,
java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets is a restricted class.,
check the settings for
guestbook/pom.xml. The App Engine version
must be set to the most recent App Engine SDK version: 1.9.21.
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.
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:
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 in the
guestbook/src/main/webapp/WEB-INF directory. Edit the file, then give it the following contents:
This deployment descriptor does three things:
<servlet>directive declares that the
GuestbookServletclass is a servlet named
<servlet-mapping>directive associates the
guestbookservlet with the URL path
<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:
/guestbook URL to see the results of the
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.