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 prompts the user to log in and then displays a personalized greeting.
Creating the UI using JSP
To create the UI:
-
In
guestbook/guestbook-war/src/main/webapp
, create a file namedguestbook.jsp
with the following contents:Notice the imports for the App Engine
Users
service. Also, by default, any file inwar/
or in a subdirectory ofwar/
(other thanWEB-INF/
) whose name ends in.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. -
Proceed to
web.xml
configuration, described next.
Configuring
web.xml
To configure the
web.xml
file:
-
In
guestbook/guestbook-war/src/main/webapp/WEB-INF
, openweb.xml
in 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. -
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/guestbook-war/src/main/java
, create the subdirectoriescom/google/appengine/demos/guestbook
by invoking the following command (in a Linux or Mac OSX terminal window):mkdir -p com/google/appengine/demos/guestbook
-
In
guestbook/guestbook-war/src/main/java/com/google/appengine/demos/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.
-
In
guestbook/guestbook-war/src/main/webapp
, create a directory namedstylesheets
, and create a file namedmain.css
with the following contents: -
You are 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 install
Wait for the build to complete.
-
Change directory to
guestbook/guestbook-ear
, ( not toguestbook-war
) and run the app in the development server on your local machine by invoking this command:mvn appengine:devserver
Wait for the success message, which looks something like this:
[INFO] INFO: The admin console is running at http://localhost:8080/_ah/admin [INFO] Mar 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:8080
to access the app on your local machine. When prompted, click Sign in . -
In the login form supply an email or accept the
[email protected]
dummy email and click Log In . (When run locally, there is no validity check.) -
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.