GAE for Java 的留言版範例
GAE 雲端程式簡介環境安裝管理介面資料庫中文編碼程式範例HelloGuestbookWikiBoxTxtDBase訊息相關網站參考文獻最新修改簡體版English |
專案下載:guestbook.zip 最後成果操作方法請按照下列入門指南建立一個 GAE for Java 的留言版程式。
問題
程式範例檔案:guestbook.jsp
檔案:Web.xml <?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>Guestbook</servlet-name> <servlet-class>guestbook.GuestbookServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Guestbook</servlet-name> <url-pattern>/guestbook</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>guestbook.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>sign</servlet-name> <servlet-class>guestbook.SignGuestbookServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>sign</servlet-name> <url-pattern>/sign</url-pattern> </servlet-mapping> </web-app> 檔案:Greeting.java package guestbook; import java.util.Date; import javax.jdo.annotations.IdGeneratorStrategy; import javax.jdo.annotations.IdentityType; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey; import com.google.appengine.api.users.User; @PersistenceCapable(identityType = IdentityType.APPLICATION) public class Greeting { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @Persistent private User author; @Persistent private String content; @Persistent private Date date; public Greeting(User author, String content, Date date) { this.author = author; this.content = content; this.date = date; } public Long getId() { return id; } 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; } } 檔案:GuestbookServlet package guestbook; import java.io.IOException; 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; public class GuestbookServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if (user != null) { resp.setContentType("text/plain"); resp.getWriter().println("Hello, " + user.getNickname()); } else { resp.sendRedirect(userService.createLoginURL(req.getRequestURI())); } } } 檔案:PMF.java package guestbook; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManagerFactory; public final class PMF { private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional"); private PMF() {} public static PersistenceManagerFactory get() { return pmfInstance; } } 檔案:SignGuestbookServlet package guestbook; 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 guestbook.PMF; public class SignGuestbookServlet extends HttpServlet { private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName()); public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); String content = req.getParameter("content"); Date date = new Date(); Greeting greeting = new Greeting(user, content, date); PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(greeting); } finally { pm.close(); } resp.sendRedirect("/guestbook.jsp"); } } |
page revision: 7, last edited: 03 Oct 2010 05:18
很不错啊,正好可以学习借鉴一下,谢谢你。
請問在run程式的時候已出現
資訊: The server is running at localhost:8000/
但是瀏覽localhost:8000/之後就會跑出以下的錯誤
javax.servlet.ServletException: java.lang.VerifyError: Expecting a stackmap frame at branch target 21 in method guestbook.Greeting.jdoReplaceFlags()V at offset 4
因為初學GAE,GOOGLE搜尋也找不到資料,可以幫幫我媽?謝謝你 :)
Post preview:
Close preview