Adding Application Code and UI
Creating a JSP template
Creating a CSS file for the JSP
Testing the app in a development server
Creating a servlet class
Configuring URL paths
Testing the finished sample
Project directory structure and files
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:
A servlet class based on javax.servlet.http.HttpServlet
Note: If you're following along using the Github repo for this tutorial, the code in this section is in the phase1 subdirectory. In later steps, we will extend this example to match the code in the final subdirectory.
Creating a JSP template
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
:
Let's start with a simple JSP template. In guestbook/src/main/webapp
, create a file named guestbook.jsp
with the following contents:
phase1/src/main/webapp/guestbook.jsp
\[This section requires a browser that supports JavaScript and iframes.\]
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.
Creating a CSS file for the JSP
Our JSP template uses a CSS file, so create a directory named stylesheets
in guestbook/src/main/webapp
. In this new directory, create a file named main.css
with the following contents:
phase1/src/main/webapp/stylesheets/main.css
\[This section requires a browser that supports JavaScript and iframes.\]
Testing the app in a development server
Note: The feature shown here (Users service) is fully operational only when you deploy the application to App Engine. If you test in the local development server, as shown below, the current user isn't checked, and you can supply any name or email you want to the login form.
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:devserverWait 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 runningNote: If there is a failure indicating that the App Engine Maven plugin is missing, this could be due to the fact that a new version was recently pushed to the Maven repo. To remedy this, invoke Maven with the -U option:
mvn appengine:devserver -U
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 inguestbook/src/main/webapp/
. We will configure more URL handlers in the next section.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 forguestbook/pom.xml
. The App Engine version must be set to the most recent App Engine SDK version: 1.9.34.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:
In this new directory, create a file named GuestbookServlet.java
, and give it the following contents:
phase1/src/main/java/com/example/guestbook/GuestbookServlet.java
\[This section requires a browser that supports JavaScript and iframes.\]
The code above checks for the user currently logged into a Google Account, and if the user isn't logged in, displays a login form for the user to log in. Note that this behavior is what happens when you deploy. If you test it locally, the current user isn't checked, and you can supply any name or email you want to the login form.
The servlet also demonstrates how to read query parameters with the getParameter()
method.
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:
phase1/src/main/webapp/WEB-INF/web.xml
\[This section requires a browser that supports JavaScript and iframes.\]
This deployment descriptor does three things:
The
<servlet>
directive declares that theGuestbookServlet
class is a servlet namedguestbook
.The
<servlet-mapping>
directive associates theguestbook
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.
Testing the finished sample
You can test the complete sample (JSP and servlet) as follows:
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 theGuestbookServlet
class:This servlet demonstrates how to read query parameters with the
getParameter()
method. Try adding atesting
parameter to the URL, to display testing information:Try visiting the root (
/
) URL to see that JSP file that used for the home page:
Project directory structure and files
Your project directory structure looks like this:

You can see the JSP file (guestbook.jsp
) and the servlet (GuestBookServlet.java
) that we just added above, along with the stylesheet main.css
, and the web.xml
configuration file.
Notice also the file appengine-web.xml
. This is the location where the version is specified. If you use the Maven artifacts and sample projects described in this tutorial, the version value in this file is set using the pom.xml
file, as we'll show later in Uploading Your Application.
Finally, notice the file logging.properties
, which is used if you want to write application logs, as described in Writing application logs
Next
Continue to the next section to add persistent data storage functionality to the guestbook app.