Google App Engine(4)Java Start and Datastore

Google App Engine(4)Java Start and Datastore
One Tip: sometimes, if you want to kill a app engine development server, just use this command to grep the information.
>ps -ef | grep appengine

A scalable web application will face a user interacting with any of dozens of web servers at a given time, and the user's next request could go to a different web server than the previous request.

App Engine includes support for 2 different API standards for the Datastore: Java Data Objects(JDO) and the Java Persistence API(JPA).

Updating the Servlet to Store Data
String guestbookName = req.getParameter("guestbookName");


Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); // database

log.info("Greeting posted by user " + user.getNickname() + ": "
+ guestbookName);

String content = req.getParameter("content");
Date date = new Date();
Entity greeting = new Entity("Greeting", guestbookKey); //table
greeting.setProperty("user", user);
greeting.setProperty("date", date);
greeting.setProperty("content", content);

DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
datastore.put(greeting);

resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);

Storing the Submitted Greetings
…snip…

Updating the JSP
<%
     String guestbookName = request.getParameter("guestbookName");
     if (guestbookName == null) {
          guestbookName = "default";
     }
     pageContext.setAttribute("guestbookName", guestbookName);
     UserService userService = UserServiceFactory.getUserService();
     User user = userService.getCurrentUser();
     if (user != null) {
          pageContext.setAttribute("user", user);
%>
<p>
Hello, ${fn:escapeXml(user.nickname)}! (You can <a
href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign
out</a>.)
</p>
<%
     } else {
%>
<p>
     Hello! <a
          href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign
          in</a> to include your name with greetings you post.
     </p>
<%
     }
%>

<%
     DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
     Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); //database
     Query query = new Query("Greeting", guestbookKey).addSort("date",
     Query.SortDirection.DESCENDING);   //table
     List<Entity> greetings = datastore.prepare(query).asList(
          FetchOptions.Builder.withLimit(5));
     if (greetings.isEmpty()) {
%>
     <p>Guestbook '${fn:escapeXml(guestbookName)}' has no messages.</p>
<%
     } else {
%>
<p>Messages in Guestbook '${fn:escapeXml(guestbookName)}'.</p>
<%
     for (Entity greeting : greetings) {
     pageContext.setAttribute("greeting_content", greeting.getProperty("content"));
     if (greeting.getProperty("user") == null) {
%>
     <p>An anonymous person wrote:</p>
<%
     } else {
     pageContext.setAttribute("greeting_user",
     greeting.getProperty("user"));
%>
<p>
<b>${fn:escapeXml(greeting_user.nickname)}</b> wrote:
</p>
<%
}
%>
<blockquote>${fn:escapeXml(greeting_content)}</blockquote>
<%
}
}
%>

<form action="/sign"method="post">
<div>
<textarea name="content"rows="3"cols="60"></textarea>
</div>
<div>
<input type="submit"value="Post Greeting"/>
</div>
<input type="hidden"name="guestbookName" value="${fn:escapeXml(guestbookName)}"/>
</form>

Retrieving the Stored Greetings
Read this in detail
https://developers.google.com/appengine/docs/java/datastore/

A Word About Datastore Indexes
https://developers.google.com/appengine/docs/java/datastore/indexes

Clearing the Datastore
delete the file WEB-INF/appengine-generated/local_db.bin

Use Static Files
By default, App Engine makes all files in the WAR available as static files except JSPs and files in WEB-INF/,

A Simple Stylesheet
create a directory named stylesheets/ under war/. File main.css.
body {
    font-family: Verdana,Helvetica,sans-serif;
    background-color: #FFFFCC;
}

Directly use that URL in JSP files:
<head>
     <link type="text/css"rel="stylesheet"href="/stylesheets/main.css"/>
</head>

Uploading Your Application
Create Application from here: https://appengine.google.com/

Uploading From Eclipse
click 'Deploy to App Engine', and I already enter my google account and choose the project 4mymessage.

And I place the application Id in the configuration file

Verify the front page from URL http://4mymessage.appspot.com

When I have time, I will go on the sample in this URL http://googcloudlabs.appspot.com/.

References:
https://developers.google.com/appengine/docs/java/gettingstarted/usingdatastore
https://developers.google.com/appengine/docs/java/gettingstarted/staticfiles
https://developers.google.com/appengine/docs/java/gettingstarted/uploading

http://googcloudlabs.appspot.com/

你可能感兴趣的:(Google)