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.jsp
with the following contents:Notice the imports for the App Engine
Users
service. Also, by default, any file inwebapp/
or in a subdirectory other thanWEB-INF/
that has the s 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
. -
In
guestbook/src/main/webapp
, create a directory namedstylesheets
, and create a file namedmain.css
with the following contents: -
Proceed to
web.xml
configuration, described next.
Configuring web.xml
To configure the web.xml
file:
-
In
guestbook/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. For more information about theweb.xml
file 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/guestbook
by 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 install
Wait for the build to complete.
-
Run the app in the development server on your local machine by invoking this command from
/guestbook
: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] 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:8080
to access the app on your local machine. If 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.) 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.