Hide
Java

Storing Data in Datastore

In this part of the tutorial, we'll extend the simple application created earlier by adding UI that allows the user to POST greetings and display previously posted greetings. To support the UI, we'll add a new servlet that handles the POST interaction with the database to store the data.

The database we are using is App Engine Datastore, which is a NoSQL, schema-less database available to your app without any further sign-up or activation. Because Datastore is schema-less, you don't need to define objects in the database in advance before you start writing to Datastore. You simply import the required Datastore classes via your import statements and write your data. (For a complete description of this powerful storage system, visit the App Engine Datastore pages.)

This is what the app will look like after we add the UI and support for Datastore:

Hello_UI

We'll make these changes to our existing application :

  • Edit the JSP page to allow users to post greetings to the datastore and to display all the greetings currently stored.
  • Create a new servlet named SignGuestbookServlet.java that handles the interactions with Datastore.
  • Add required entries in web.xml so requests can be routed to the new servlet.

Adding data POST and display UI to JSP

To add the new UI pieces:

  1. In guestbook/src/main/webapp, open guestbook.jsp in an editor to add the following imports to the imports section:

  2. Just above the line <form action="/guestbook.jsp" method="get"> add the following code:

    The query-related code results in a query when the page is loaded; Datastore is searched for all greetings currently stored for this app in Datastore, and lists them in the UI. (See the docs on Datastore Queries to learn more about how this works.)

    The form-related part of this code sends a POST request containing a new greeting from the user. That POST is handled by the post handler servlet SignGuestbookServlet.java, which you will create in the next procedure.

Creating the servlet SignGuestbookServlet.java

To create the servlet:

  1. In guestbook/src/main/java/com/example/guestbook, create a file named SignGuestbookServlet.java.

  2. Add the following contents to the file:

    This servlet is the POST handler for the greetings posted by users. It takes the the greeting (content) from the incoming request and stores it in Datastore as an entity called Greeting, and stores properties along with the Greeting, such as the e-mail address and the id of the user who posted the greeting and the date it was posted. (For more information about entities in App Engine, see Entities, Properties, and Keys).

  3. Open guestbook/src/main/webapp/WEB-INF/web.xml and ensure the new servlets have URLs mapped. The final version should look like this:

  4. You are ready to build and then test the app locally on the development server, which is described next.

Building your app

To build your app:

  1. Change directory to guestbook, and invoke the command:

    mvn clean install
    
  2. Wait for the build to complete.

Testing your app on the development server

To run and test your app in the development server:

  1. Invoke 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] Aug 18, 2014 5:35:04 PM com.google.appengine.tools.development.DevAppServerImpl doStart
    [INFO] INFO: Dev App Server is now running
    
  2. In a browser running on the same machine as your terminal window, visit localhost:8080 to access the app. If you aren't already signed-in, click Sign in, supply an email, and click Log In.

  3. Supply a text message in the textbox and click Post Greeting. Observe that the greeting is now displayed in the greetings list under your email name.

Creating required indexes

If your app uses the Datastore, when you run your app on the development server, the development server automatically creates any Datastore indexes required to run in production App Engine. These indexes are generated in guestbook/target/guestbook-1.0-SNAPSHOT/WEB-INF/appengine-generated/datastore-indexes-auto.xml. You'll also notice the file local_db.bin at that same location: this is local storage for the development server to persist your app data between development server sessions. The file datastore-indexes-auto.xml is automatically uploaded along with your app; local_db.bin is not uploaded.

For complete information about indexes, see Datastore Indexes.

Next...

We now have a working guest book application that authenticates users using Google accounts, lets them submit messages, and displays messages other users have left.

The only thing left to do is upload our application...

Uploading Your Application >>