Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
М_указания к лабам_230400.68_совр_тенденции.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
9.28 Mб
Скачать

Practice №6 Creating a data warehouse environment Google App Engine

In this practice, you'll extend the simple application created earlier by adding the ability to post greetings and display previously posted greetings. You'll add a new servlet that handles the form submission and save the data.

The database we are using is Google Cloud Datastore, a scalable, highly reliable data storage service that's easy to use from App Engine. Unlike a relational database, the Datastore is a schemaless object store, with an index-based query engine and transactional capabilities.

This tutorial uses the Objectify library, a Java data modeling library for the Datastore. There are several alternative APIs you can use: a simple low-level API, implementations of the Java Data Objects (JDO) and Java Persistence API (JPA) interfaces, or the Slim3 library.

This is what the app will look like after you add the message submission form:

You'll make these changes to your existing application:

  • Set up Objectify, and create data model classes for the guestbook.

  • 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 that handles the form submission.

  • Add entries in web.xml so requests can be routed to the new servlet.

6.1 Setting up Objectify

To use the Objectify library, you must declare the dependency in the pom.xml file, so Maven can install it. In the application root directory (guestbook/), edit pom.xml, locate the <dependencies> section, then add the following:

<dependency>     <groupId>com.googlecode.objectify</groupId>     <artifactId>objectify</artifactId>     <version>4.0.1</version> </dependency>

Maven will install the library the next time you run a target that needs it (such as the development server).

In order to use Objectify in a JSP, we need a helper class that registers the model classes in the JSP servlet context. In guestbook/src/main/java/com/example/guestbook/, create a file named OfyHelper.java with the following contents:

package com.example.guestbook; import com.googlecode.objectify.Objectify; import com.googlecode.objectify.ObjectifyFactory; import com.googlecode.objectify.ObjectifyService; import javax.servlet.ServletContextListener; import javax.servlet.ServletContextEvent; /**  * OfyHelper, a ServletContextListener, is setup in web.xml to run before a JSP is run.  This is  * required to let JSP's access Ofy.  **/ public class OfyHelper implements ServletContextListener {   public void contextInitialized(ServletContextEvent event) {     // This will be invoked as part of a warmup request, or the first user request if no warmup     // request.     ObjectifyService.register(Guestbook.class);     ObjectifyService.register(Greeting.class);   }   public void contextDestroyed(ServletContextEvent event) {     // App Engine does not currently invoke this method.   } }

This file refers to the Greeting and Guestbook model classes that you'll create in the next section.

Finally, in guestbook/src/main/webapp/WEB-INF/, edit web.xml, and add the following lines inside <web-app> to set up the JSP helper:

<filter>   <filter-name>ObjectifyFilter</filter-name>   <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class> </filter> <filter-mapping>   <filter-name>ObjectifyFilter</filter-name>   <url-pattern>/*</url-pattern> </filter-mapping> <listener>   <listener-class>com.example.guestbook.OfyHelper</listener-class> </listener>