Add/Retrieve

DataStore

JavaBean/Servlet/JSP


System Requirements:

Download Eclipse Helios (DDE)

http://www.eclipse.org/downloads/


Google App Engine Account:

https://developers.google.com/appengine/


Introduction:

Create a complete application and host it on Google Cloud space. Submit records to DataStore and retrieve back into the application.


Disclaimer: Information contained in the following is presented as is. This tutorial assumes you have basic programming knowledge. All tutorials are based on an Eclipse/Eclipse-based software. Should you need to familiarize yourself with your environment, prior to continuing this tutorial, please stop now and see our Self-Help pages...


GuestBook Demo Application

At this point we assume Eclipse Helios is downloaded/installed and a copy of GuestBook Demo file have been retrieve from Google Code. Copy and paste samples provided below, areas of interest have been highlighted for your convenience.





/**

* Created from copy: 2011.04.30.5.25.PM

* Bean to be used to access datastore

* for various Java Applications

* Program: Greeting.java

*/


package guestbook;


/**

* @author Dököll Solutions, Inc.

* @version: 2011.04.30.5.25.PM

*

*/

import com.google.appengine.api.datastore.Key;

import com.google.appengine.api.users.User;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;

import javax.jdo.annotations.PersistenceCapable;

import javax.jdo.annotations.Persistent;

import javax.jdo.annotations.PrimaryKey;


@PersistenceCapable

public class Greeting {

@PrimaryKey

@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)

private Key key;

@Persistent

private User author;

@Persistent

private String content;

@Persistent

private Date date;


public Key getKey() {

return key;

}


public User getAuthor() {

return author;

}


public String getContent() {

return content;

}


public Date getDate() {

return date;

}


public void setAuthor(User author) {

this.author = author;

}


public void setContent(String content) {

this.content = content;

}


public void setDate(Date date) {

this.date = date;

}


public Greeting(User author, String content, Date date) {

this.author = author;

this.content = content;

this.date = date;

}

}





/**

* Created: 2011.01.22.11.56.AM

* Servlet Class to allow content to load from user

*/

package dokoll.solutions.Servlets;


/**

* @author Dököll Solutions, Inc.

* @version: 2011.01.22.11.56.AM

*/


import java.io.IOException;

import java.util.Date;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import javax.servlet.http.*;

import com.google.appengine.api.users.User;

import com.google.appengine.api.users.UserService;

import com.google.appengine.api.users.UserServiceFactory;

import guestbook.Greeting;

import dokoll.solutions.Main.PMF;


public class GuestbookServlet extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 1L;

// maintain a log for audit log purposes

private static final Logger log = Logger

.getLogger(GuestbookServlet.class.getName());


// grab items via post and fill up data fields for jsp form

public void doPost(HttpServletRequest req, HttpServletResponse resp)

throws IOException {

UserService userService = UserServiceFactory.getUserService();

//current user

User user = userService.getCurrentUser();

//field item set for multiline textarea

String content = req.getParameter("content");

Date date = new java.util.Date();


Greeting greetings = new Greeting(user, content, date);

PersistenceManager pm = PMF.get().getPersistenceManager();

try {

pm.makePersistent(postings);

} finally {

pm.close();

}

resp.sendRedirect("/guestbook.jsp");

}


}






/**

* @AppName GuestBook

* Program: guestbook.jsp

* Create from copy: 2011.04.30.5.25.PM

* JSP Code for GuestBook Application

*

*/


/**

* @author Dököll Solutions, Inc.

* @version: 2011.04.30.5.25.PM

*

*/


<%@ page contentType="text/html;charset=UTF-8" language="java"%>

<%@ page import="java.util.List"%>

<%@ page import="javax.jdo.PersistenceManager"%>

<%@ page import="com.google.appengine.api.users.User"%>

<%@ page import="com.google.appengine.api.users.UserService"%>

<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>

<%@ page import="guestbook.Greeting"%>

<%@ page import="guestbook.PMF"%>


<html>


<body>


<%

UserService userService = UserServiceFactory.getUserService();

User user = userService.getCurrentUser();

if (user != null) {

%>


<div id="photolayer" style="z-index: 1; left: 5px; top: 50px; width: 100%;

height: 20px; background-color:linen; layer-background-color:#003366; visibility: visible">


<p><b>User</b> :: <%= user.getNickname() %> :: <a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">Logout</a></p></div>

<%

} else {

%>

<div id="photolayer" style="z-index: 1; left: 5px; top: 50px; width: 100%;

height: 20px; background-color:linen; layer-background-color:#003366; visibility: visible"> <p><b>User</b> :: Anonymous :: <a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Login</a></p></div>

<%

}

%>

<%

PersistenceManager pm = PMF.get().getPersistenceManager();

String query = "select from " + Greeting.class.getName();

List<Greeting> greetings = (List<Greeting>) pm.newQuery(query)

.execute();

if (greetings.isEmpty()) {

%>

<p>No Journal Entries...</p>

<%

} else {

for (Greeting g : greetings) {

if (g.getAuthor() == null) {

%>


<p><b>Anonymous</b><br><img src ="files/images/JournalEntryCommLogo.bmp"></img> </p>

<%

} else {

%>

<p><b><%=g.getAuthor().getNickname()%></b><br><img src ="files/images/JournalEntryCommLogo.bmp"></img> </p>




<%

}

%>

<div id="photolayer" style="z-index: 1; left: 5px; top: 50px; width: 100%;

height: 100px; background-color:beige; layer-background-color:#003366; visibility: visible">

<p><blockquote><font color="navy"><%=g.getContent()%></font></blockquote></p></div>


<%

}

}

pm.close();

%>


<form action="/gsign" method="post">

<a href="#" title ="Please add only sample/tutorial trial results...

Questions will not be answered(see example)." >What's this:</a> No Questions, just results... <a href="#" title="Thank you for this tutorial. However, I had an error: 'MyBean.java cannot be found', the following attempt did not work:


(1) referenced JavaBean in faces-config.xml file

(2) cleaned the project, restarted IDE and App

(3) URL: www.dokollsolutionsinc.com/CutAndPasteLinks.html


Just reporting my findings, thanks again!">Posting Example</a>

<div><textarea name="content" rows="3" cols="60"></textarea></div>

<div><input type="submit" value="Submit" /> </div>

</form>

</body>

</html>


/**

* web.xml

* Servlet code for GuestBook Application

*

*/


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">


<!-- Servlets -->

<servlet>

<servlet-name>greetServlet</servlet-name>

<servlet-class>dokoll.solutions.Main.server.GreetingServiceImpl</servlet-class>

</servlet>


<servlet>

<servlet-name>gsign</servlet-name>

<servlet-class>guestbook.GuestbookServlet</servlet-class>

</servlet>


<servlet>

<servlet-name>HelperGuestbookServlet</servlet-name>

<servlet-class>dokoll.solutions.Servlets.HelperGuestbookServlet</servlet-class>

</servlet>


<servlet-mapping>

<servlet-name>greetServlet</servlet-name>

<url-pattern>/systems_development/greet</url-pattern>

</servlet-mapping>


<servlet-mapping>

<servlet-name>gsign</servlet-name>

<url-pattern>/gsign</url-pattern>

</servlet-mapping>



<servlet-mapping>

<servlet-name>HelperGuestbookServlet</servlet-name>

<url-pattern>/helperguestbookServlet</url-pattern>

</servlet-mapping>


<!-- Default page to serve -->

<welcome-file-list>

<welcome-file>guestbook.jsp</welcome-file>

</welcome-file-list>



</web-app>



/**

* appengine-web.xml

* Servlet code for Application Cloud Space

*

*/


<?xml version="1.0" encoding="utf-8"?>

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

<application>your-appengine-appID</application>

<version>1</version>


<!-- Configure serving/caching of GWT files -->

<static-files>

<include path="**" />


<!-- The following line requires App Engine 1.3.2 SDK -->

<include path="**.nocache.*" expiration="0s" />


<include path="**.cache.*" expiration="365d" />

<exclude path="**.gwt.rpc" />

</static-files>

<!-- Adding sessions 2011.05.28.9.22.PM -->

<sessions-enabled>true</sessions-enabled>


<!-- Configure java.util.logging -->

<system-properties>

<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>

</system-properties>


</appengine-web-app>



/**

* Created: 2011.01.22.11.56.AM

* Servlet Class to allow content to load from user

*/

package dokoll.solutions.Servlets;


/**

* @author Dököll Solutions, Inc.

* @version: 2011.01.22.11.56.AM

*/


import java.io.IOException;

import java.util.Date;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import javax.servlet.http.*;

import com.google.appengine.api.users.User;

import com.google.appengine.api.users.UserService;

import com.google.appengine.api.users.UserServiceFactory;

import guestbook.Greeting;

import dokoll.solutions.Main.PMF;


public class GuestbookServlet extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 1L;

// maintain a log for audit log purposes

private static final Logger log = Logger

.getLogger(GuestbookServlet.class.getName());


// grab items via post and fill up data fields for jsp form

public void doPost(HttpServletRequest req, HttpServletResponse resp)

throws IOException {

UserService userService = UserServiceFactory.getUserService();

//current user

User user = userService.getCurrentUser();

//field item set for multiline textarea

String content = req.getParameter("content");

Date date = new java.util.Date();

Greeting greetings = new Greeting(user, content, date);

PersistenceManager pm = PMF.get().getPersistenceManager();

try {

pm.makePersistent(postings);

} finally {

pm.close();

}

resp.sendRedirect("/guestbook.jsp");

}


}


Step 1:

Create GuestBook Application:

Set up a JavaBean Greeting.java, connect it to GuestbookServlet.java Servlet and use with a Java Server Pages file to add/retrieve Google DataStore items.


  1. Go to Eclipse Helios Project Explorer

  2. Expand project folder, where necessary

  3. Right-click on 'src' folder

  4. Go to New, then Class

  5. Repeat above to create Servlet

  6. Create JSP page, add web XML samples

  7. Run the Application


Note:

At this point, you should have already set up a Google App Engine account to promote/deploy your application to appspot.com servers. See supporting documnetation below:


Added info:

https://developers.google.com/appengine/

https://developers.google.com/appengine/docs/java/gettingstarted/usingjsps


Conclusion:

We recommend writing a different version of this in JSF for scalability purposes...


Questions, comments, please post a brief message on our form. Thank you for coming...