<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fatkun&#039;s blog &#187; google app engine</title>
	<atom:link href="http://fatkun.com/tag/google-app-engine/feed" rel="self" type="application/rss+xml" />
	<link>http://fatkun.com</link>
	<description>又一个 WordPress 站点</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:21:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>GAE-使用cron计划任务(java)时无法上传cron.xml，500错误</title>
		<link>http://fatkun.com/2010/05/google-app-engine-java-cron.html</link>
		<comments>http://fatkun.com/2010/05/google-app-engine-java-cron.html#comments</comments>
		<pubDate>Tue, 11 May 2010 11:50:22 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[google app engine]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=301</guid>
		<description><![CDATA[错误信息 com.google.appengine.tools.admin.AdminException: Unable to update app: Error posting to URL: https://appengine.google.com/api/datastore/cron/update?app_id=fatkuns&#38;version=4&#38; 500 Internal Server Error Server Error (500) A server error has occurred. at com.google.appengine.tools.admin.AppAdminImpl.update(AppAdminImpl.java:62) at com.google.appengine.eclipse.core.proxy.AppEngineBridgeImpl.deploy(AppEngineBridgeImpl.java:271) at com.google.appengine.eclipse.core.deploy.DeployProjectJob.runInWorkspace(DeployProjectJob.java:145) at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38) at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55) Caused by: java.io.IOException: Error posting to URL: https://appengine.google.com/api/datastore/cron/update?app_id=fatkuns&#38;version=4&#38; 500 Internal Server Error Server Error (500) A server error has occurred. at com.google.appengine.tools.admin.ServerConnection.send(ServerConnection.java:149) [...]]]></description>
			<content:encoded><![CDATA[<h2>错误信息</h2>
<blockquote><p>com.google.appengine.tools.admin.AdminException: Unable to update app: Error posting to URL: https://appengine.google.com/api/datastore/cron/update?app_id=fatkuns&amp;version=4&amp;<br />
500 Internal Server Error</p>
<p>Server Error (500)<br />
A server error has occurred.</p>
<p>at com.google.appengine.tools.admin.AppAdminImpl.update(AppAdminImpl.java:62)</p>
<p>at com.google.appengine.eclipse.core.proxy.AppEngineBridgeImpl.deploy(AppEngineBridgeImpl.java:271)</p>
<p>at com.google.appengine.eclipse.core.deploy.DeployProjectJob.runInWorkspace(DeployProjectJob.java:145)</p>
<p>at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)</p>
<p>at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)</p>
<p>Caused by: java.io.IOException: Error posting to URL: https://appengine.google.com/api/datastore/cron/update?app_id=fatkuns&amp;version=4&amp;<br />
500 Internal Server Error</p>
<p>Server Error (500)<br />
A server error has occurred.</p>
<p>at com.google.appengine.tools.admin.ServerConnection.send(ServerConnection.java:149)</p>
<p>at com.google.appengine.tools.admin.ServerConnection.post(ServerConnection.java:82)</p>
<p>at com.google.appengine.tools.admin.AppVersionUpload.send(AppVersionUpload.java:559)</p>
<p>at com.google.appengine.tools.admin.AppVersionUpload.updateCron(AppVersionUpload.java:260)</p>
<p>at com.google.appengine.tools.admin.AppVersionUpload.doUpload(AppVersionUpload.java:133)</p>
<p>at com.google.appengine.tools.admin.AppAdminImpl.update(AppAdminImpl.java:56)</p>
<p>&#8230; 4 more</p></blockquote>
<h2>原因：</h2>
<p><span style="font-weight: normal; font-size: 13px;">cron.xml文件书写不正确</span></p>
<p><span style="font-size: 13px;"><strong>记得&lt;timezone&gt;Asia/Shanghai&lt;/timezone&gt;这个是必须写的，不然无法上传，官方的例子居然有一个没写</strong></span></p>
<blockquote><p>&lt;?xml version=”1.0&#8243; encoding=”UTF-8&#8243;?&gt;</p>
<p>&lt;cronentries&gt;</p>
<p>&lt;cron&gt;</p>
<p>&lt;url&gt;/cron&lt;/url&gt;</p>
<p>&lt;description&gt;描述&lt;/description&gt;</p>
<p>&lt;schedule&gt;every 2 minutes&lt;/schedule&gt;</p>
<p>&lt;timezone&gt;Asia/Shanghai&lt;/timezone&gt;</p>
<p>&lt;/cron&gt;</p>
<p>&lt;/cronentries&gt;</p></blockquote>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?xml version=”1.0&#8243; encoding=”UTF-8&#8243;?&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;cronentries&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;cron&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;url&gt;/cron&lt;/url&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;description&gt;描述&lt;/description&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;schedule&gt;every 2 minutes&lt;/schedule&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;timezone&gt;Asia/Shanghai&lt;/timezone&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;/cron&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 597px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;/cronentries&gt;</div>
<p><strong>更新：</strong>发现如果 url填写错误也会上传失败，正确的方式是写“/xxxx”这样的路径，不能写全路径..</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/05/google-app-engine-java-cron.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>GAE-Google App Engine网址抓取(java.net.UrlConnection)</title>
		<link>http://fatkun.com/2010/01/get-website-source-using-google-app-engine.html</link>
		<comments>http://fatkun.com/2010/01/get-website-source-using-google-app-engine.html#comments</comments>
		<pubDate>Sat, 23 Jan 2010 08:17:12 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[中文乱码]]></category>
		<category><![CDATA[网址抓取]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=254</guid>
		<description><![CDATA[Google App Engine 的网址抓取挺方便的，可以使用java.net.UrlConnection这个类。有了这个我们可以干什么？例如可以从某处获取天气信息等等~ (提醒一下，上面的是图片。。不要误点了啊。。。) 看看例子：http://2.latest.fatkuns.appspot.com/ GAE网址抓取是什么？ App Engine 应用程序可以抓取资源，并通过互联网使用 HTTP 和 HTTPS 请求与其他主机通信。应用程序使用网址抓取服务来进行请求。 我觉得其实就是可以通过它抓取别人网页的源代码。 使用URL获取源码 package com.fatkun; /** * 在GAE上抓取网址 * @author Fatkun * @site http://fatkun.com */ &#160; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; &#160; import javax.servlet.http.*; &#160; @SuppressWarnings&#40;&#34;serial&#34;&#41; public class URL2Servlet extends HttpServlet &#123; public void doGet&#40;HttpServletRequest req, HttpServletResponse resp&#41; throws IOException &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Google App Engine 的网址抓取挺方便的，可以使用java.net.UrlConnection这个类。有了这个我们可以干什么？例如可以从某处获取天气信息等等~<br />
<img src="http://farm3.static.flickr.com/2724/4297317270_500f2c34b3.jpg" alt="" /><br />
(提醒一下，上面的是图片。。不要误点了啊。。。)<br />
看看例子：<a href="http://2.latest.fatkuns.appspot.com/">http://2.latest.fatkuns.appspot.com/</a><br />
<span id="more-254"></span></p>
<h2>GAE网址抓取是什么？</h2>
<blockquote><p>App Engine 应用程序可以抓取资源，并通过互联网使用 HTTP 和 HTTPS 请求与其他主机通信。应用程序使用网址抓取服务来进行请求。</p></blockquote>
<p>我觉得其实就是可以通过它抓取别人网页的源代码。</p>
<h2>使用URL获取源码</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun</span><span style="color: #339933;">;</span>
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 在GAE上抓取网址
 * @author Fatkun
 * @site http://fatkun.com
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.*</span><span style="color: #339933;">;</span>
&nbsp;
@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serial&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> URL2Servlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		resp.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/plain; charset=utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//显示编码</span>
&nbsp;
		<span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://fatkun.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// 读取源码</span>
		<span style="color: #666666; font-style: italic;">//读取中文时，使用Reader类是每次读出两个字节的，不会出现中文乱码</span>
		<span style="color: #003399;">InputStreamReader</span> in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">openStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2048</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//缓存</span>
		<span style="color: #003399;">StringBuffer</span> sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> len <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>len <span style="color: #339933;">=</span> in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//当没到文档尽头继续读取</span>
			sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>buf, <span style="color: #cc66cc;">0</span>, len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 输出在网页上</span>
		resp.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>使用HttpURLConnection 来POST内容</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 此处的地址请换成你的，在本地测试时可以填入http://localhost:8888/request.jsp</span>
<span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://2.latest.fatkuns.appspot.com/request.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">HttpURLConnection</span> connection <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
connection.<span style="color: #006633;">setDoOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 使用 URL 连接进行输出</span>
connection.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 取得输出流</span>
<span style="color: #003399;">OutputStreamWriter</span> writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">OutputStreamWriter</span><span style="color: #009900;">&#40;</span>connection.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 用UTF-8编码，保证中文传递正常</span>
<span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;你好，I'm Fatkun!&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 写入发送的内容</span>
writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;msg=&quot;</span> <span style="color: #339933;">+</span> message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
writer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>上面是主要的代码，看注释好了，都很清楚。</p>
<h2>Google App Engine中文乱码问题</h2>
<p>注意在读取中文的网页时，由于编码是使用UTF或者GBK,GB2312等编码，使用InputStream类不太方便，另外有可以出现错误。<br />
试过使用InputStream类，然后用new String(bytes[],”utf-8&#8243;)来转换编码，不过出现一点问题，不知道是我不会用还是怎么的。<br />
不过使用这样的写法就方便多了。<br />
InputStreamReader in = new InputStreamReader(url.openStream(), “UTF-8&#8243;);<br />
编码都不用转换了~指定它的编码就行。<br />
注意这里要加上“UTF-8”，虽然不加在本地测试时没问题，不过上传到GAE上就不能显示中文了。<br />
PS2:这里的UTF-8是代表你抓取网页的编码。如果你抓取的网页是gb2312的需要根据实质需求改变。</p>
<p>附上我做的例子：<a href="http://2.latest.fatkuns.appspot.com/">http://2.latest.fatkuns.appspot.com/</a><br />
源码在这里：<a href="http://2.latest.fatkuns.appspot.com/source.rar">http://2.latest.fatkuns.appspot.com/source.rar</a>,里面的lib目录下的我删除了，请自行添加。</p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/01/get-website-source-using-google-app-engine.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>GAE-简单的留言板(Simple Guestbook)</title>
		<link>http://fatkun.com/2009/12/gae-guestbook.html</link>
		<comments>http://fatkun.com/2009/12/gae-guestbook.html#comments</comments>
		<pubDate>Sun, 20 Dec 2009 06:32:26 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[留言本]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=154</guid>
		<description><![CDATA[这个例子其实就是Google官方给出来的例子~你可以在文档中看到，我在原例子的基础上添加了修改与删除功能~（...]]></description>
			<content:encoded><![CDATA[<p>这个例子其实就是Google官方给出来的例子~你可以在<a href="http://code.google.com/intl/zh-CN/appengine/docs/java/gettingstarted/" target="_blank">文档</a>中看到，我在原例子的基础上添加了修改与删除功能~（没判断权限~谁都可以删除~要判断也很简单&#8230;判断一下登陆的email）</p>
<p>我就不说那么详细了·官方例子说的已经很清楚了，需要源码的留下邮箱即可。</p>
<p>这个例子显示效果在此：<a href="http://1.latest.fatkuns.appspot.com/" target="_blank">http://1.latest.fatkuns.appspot.com/</a><br />
<img src="http://farm3.static.flickr.com/2771/4199535918_3d3cf61b53_o.jpg" alt="" /></p>
<p>这个例子涉及了</p>
<ol>
<li>如何使用用户服务（也就是使用google账号登陆~我有点担心的是别人伪造一个假的登录页面怎么办？放心，我做的那个是真的&#8230;- -!）</li>
<li>如何存储数据（使用JDO）</li>
<li>JSP</li>
<li>Servlet</li>
</ol>
<p><span id="more-154"></span></p>
<p>好~开始了~那些准备工作就不说了，创建项目，包名这些~</p>
<h2>一、要保存留言，我们需要创建一个这样的文件~Greeting.java</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun.guestbook</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.annotations.IdGeneratorStrategy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.annotations.IdentityType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.annotations.PersistenceCapable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.annotations.Persistent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.annotations.PrimaryKey</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.appengine.api.users.User</span><span style="color: #339933;">;</span>
&nbsp;
@PersistenceCapable<span style="color: #009900;">&#40;</span>identityType <span style="color: #339933;">=</span> IdentityType.<span style="color: #006633;">APPLICATION</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Greeting <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//主键的设置，自增的方式</span>
	@PrimaryKey
	@Persistent<span style="color: #009900;">&#40;</span>valueStrategy <span style="color: #339933;">=</span> IdGeneratorStrategy.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//其他的值设置一个@Persistent在前面就会保存到数据库里了</span>
	@Persistent
	<span style="color: #000000; font-weight: bold;">private</span> User author<span style="color: #339933;">;</span>
	@Persistent
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> content<span style="color: #339933;">;</span>
	@Persistent
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> date<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//下面是一些Getter,Setter方法，对于JDO是不需要的，只是为了方便你写程序</span>
	<span style="color: #000000; font-weight: bold;">public</span> Greeting<span style="color: #009900;">&#40;</span>User author, <span style="color: #003399;">String</span> content, <span style="color: #003399;">Date</span> date<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">author</span> <span style="color: #339933;">=</span> author<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span> <span style="color: #339933;">=</span> content<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">date</span> <span style="color: #339933;">=</span> date<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> User getAuthor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> author<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAuthor<span style="color: #009900;">&#40;</span>User author<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">author</span> <span style="color: #339933;">=</span> author<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> content<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setContent<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span> <span style="color: #339933;">=</span> content<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Date</span> getDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> date<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Date</span> date<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">date</span> <span style="color: #339933;">=</span> date<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>二、我们还要创建PMF.java用来取得PersistenceManager。官方说取这个东西要花费很多资源，所以只获得一个就好，单例模式~</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun.guestbook</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.JDOHelper</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.PersistenceManagerFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 用来取得PersistenceManager
 * @author Administrator
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> PMF <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> PersistenceManagerFactory pmfInstance <span style="color: #339933;">=</span> JDOHelper.<span style="color: #006633;">getPersistenceManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;transactions-optional&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> PMF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> PersistenceManagerFactory get<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> pmfInstance<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>三、我们在war目录下新建一个guestbook.jsp用来显示所有留言</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span> <span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java.util.List&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javax.jdo.PersistenceManager&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.google.appengine.api.users.User&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.google.appengine.api.users.UserService&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.google.appengine.api.users.UserServiceFactory&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.fatkun.guestbook.Greeting&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.fatkun.guestbook.PMF&quot;</span><span style="color: #339933;">%&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Google App Engine留言板 <span style="color: #339933;">-</span> fatkun.<span style="color: #006633;">com</span><span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
	UserService userService <span style="color: #339933;">=</span> UserServiceFactory.<span style="color: #006633;">getUserService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	User user <span style="color: #339933;">=</span> userService.<span style="color: #006633;">getCurrentUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>user <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>你好, <span style="color: #339933;">&lt;%=</span>user.<span style="color: #006633;">getNickname</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%&gt;!</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=userService.createLogoutURL(request.getRequestURI())%&gt;&quot;</span><span style="color: #339933;">&gt;</span>退出<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>.<span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>你好<span style="color: #339933;">!</span> 你可以使用google账号<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=userService.createLoginURL(request.getRequestURI())%&gt;&quot;</span><span style="color: #339933;">&gt;</span>登陆<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">%&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;%</span>
	PersistenceManager pm <span style="color: #339933;">=</span> PMF.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPersistenceManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//String query = &quot;select from &quot; + Greeting.class.getName()+&quot; order by id desc&quot;;</span>
	<span style="color: #666666; font-style: italic;">//也可以写成下面的形式</span>
	<span style="color: #003399;">String</span> query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select from com.fatkun.guestbook.Greeting order by id desc&quot;</span><span style="color: #339933;">;</span>
	List<span style="color: #339933;">&lt;</span>Greeting<span style="color: #339933;">&gt;</span> greetings <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Greeting<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> pm.<span style="color: #006633;">newQuery</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>greetings.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>还没有留言。<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Greeting g <span style="color: #339933;">:</span> greetings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>g.<span style="color: #006633;">getAuthor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				out.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;p&gt;一位匿名人士说:&lt;/p&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				out.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;p&gt;&lt;b&gt;&quot;</span><span style="color: #339933;">+</span>g.<span style="color: #006633;">getAuthor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getNickname</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&lt;/b&gt; 说:&lt;/p&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;del?id=&lt;%=g.getId()%&gt;&quot;</span><span style="color: #339933;">&gt;</span>删除<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span> <span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;guestbook-modify.jsp?id=&lt;%=g.getId()%&gt;&quot;</span><span style="color: #339933;">&gt;</span>修改<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>blockquote<span style="color: #339933;">&gt;&lt;%=</span>g.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%&gt;&lt;/</span>blockquote<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	pm.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">%&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/add&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;&lt;</span>textarea name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span> rows<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;3&quot;</span> cols<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">&gt;&lt;/</span>textarea<span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;我要留言&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>这里注意要引入自己使用的包~~GQL是针对对象来写查询语句的~~有点儿像Hibernate的HQL~</p>
<h2>四、没有内容怎么显示呢~我们加一个添加到数据库的GuestbookAddServlet.java</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.fatkun.guestbook</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jdo.PersistenceManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.appengine.api.users.User</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.appengine.api.users.UserService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.appengine.api.users.UserServiceFactory</span><span style="color: #339933;">;</span>
&nbsp;
@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serial&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GuestbookAddServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//取得用户Service，这里是Google的用户管理</span>
		UserService userService <span style="color: #339933;">=</span> UserServiceFactory.<span style="color: #006633;">getUserService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//取得当前登录的用户，当不存在的时候为null</span>
		User user <span style="color: #339933;">=</span> userService.<span style="color: #006633;">getCurrentUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">String</span> content <span style="color: #339933;">=</span> req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//创建一个Greeting对象</span>
		Greeting greeting <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Greeting<span style="color: #009900;">&#40;</span>user, content, date<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		PersistenceManager pm <span style="color: #339933;">=</span> PMF.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPersistenceManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//持久化~也就是把greeting添加进数据库</span>
			pm.<span style="color: #006633;">makePersistent</span><span style="color: #009900;">&#40;</span>greeting<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
			pm.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		resp.<span style="color: #006633;">sendRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;guestbook.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>写完了上面这个文件，注意还得在web.xml加入下面的，要告诉服务器说，我在这里啊，你可以通过这个链接找我~~</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>add<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.fatkun.guestbook.GuestbookAddServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>add<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/add<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>这时，你应该就可以留言啦~~运行一下试试~~可以先在本地测试，再上传到GAE服务器上。</p>
<p>伍、我最后给出修改和删除的部分代码~其实就主要是数据库的问题~~</p>
<p>GuestbookModifyServlet.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">parseLong</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> content <span style="color: #339933;">=</span> req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//先获取PersistenceManager</span>
		PersistenceManager pm <span style="color: #339933;">=</span> PMF.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPersistenceManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//根据ID从数据库得到一条记录</span>
			Greeting greeting <span style="color: #339933;">=</span> pm.<span style="color: #006633;">getObjectById</span><span style="color: #009900;">&#40;</span>Greeting.<span style="color: #000000; font-weight: bold;">class</span>, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//修改记录</span>
			greeting.<span style="color: #006633;">setContent</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//当关闭pm的时候，会自动把已经修改的记录更新到数据库</span>
			pm.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		resp.<span style="color: #006633;">sendRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;guestbook.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>GuestbookDeleteServlet.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">parseLong</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		PersistenceManager pm <span style="color: #339933;">=</span> PMF.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPersistenceManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//取得这条记录</span>
			Greeting greeting <span style="color: #339933;">=</span> pm.<span style="color: #006633;">getObjectById</span><span style="color: #009900;">&#40;</span>Greeting.<span style="color: #000000; font-weight: bold;">class</span>, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//删除</span>
			pm.<span style="color: #006633;">deletePersistent</span><span style="color: #009900;">&#40;</span>greeting<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//关闭</span>
			pm.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		resp.<span style="color: #006633;">sendRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;guestbook.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>好，一个简单的留言板就这样诞生了~~如果想弄得更加漂亮，就自己弄吧~~^_^<br />
我做的例子：<a href="http://1.latest.fatkuns.appspot.com/">http://1.latest.fatkuns.appspot.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2009/12/gae-guestbook.html/feed</wfw:commentRss>
		<slash:comments>59</slash:comments>
		</item>
	</channel>
</rss>

