Professional Documents
Culture Documents
3 To mt project.................................................................................................................................7
3.1 Cu trc th mc project......................................................................................................................7
3.2 The servlet class......................................................................................................................................8
3.3 Signgustbookservlet.java file.................................................................................................................9
3.4 PMF file................................................................................................................................................10
3.5 Getting.java..........................................................................................................................................11
3.6 jdoconfig.xml file..................................................................................................................................12
3.7 guestbook.jsp file..................................................................................................................................13
3.8 web.xml file...........................................................................................................................................14
3.9 theappengine-web.xml.........................................................................................................................15
2 Hng dn ci t
2.1
-
Yu cu:
Eclipse hay jcreator
Google plugin cho Eclipese
AppEngine-java-SDK-1.3.8.zip
3 To mt project
Cc ng dng App Engine Java s dng cc chun java servlet tng tc vi
mi trng my ch web. Cc file ca mt ng dng bao gm: cc file class c
bin dch t file java, cc file JAR ca b th vin, cc file tnh (css,) v cc file
xml cu hnh. Tt c c sp xp theo mt cu trc th mc v nm trong th mc
WAR
3.1 Cu trc th mc project
Mt th mc vi tn Guestbook c to cha d n. Bn trong l 2 th mc,
mt th mc mang tn /src cha m ngun java v mt th mc /war cha cc
file class c bin dch t file ngun java. Th mc war c xem l mt ng dng
hon chnh dng up ln Google App.
To cy th mc nh sau:
Guestbook
Greeting.java
Src
(m ngun)
GuestbookServlet.java
PMF.java
Guestbook
SignGuestbookServlet.java
META-INF
Jdoconfig.xml
(file cu hnh)
war
Guestbook.jsp
WEB-INF
Web.xml
Appengine-web.xml
classes
lib
Compiled classes
JARs for Libraries
Ti v m ngun ti:
https://code.google.com/p/googleappengine/source/browse/trunk/java/demos/?r=44#demos%2Fguestbook
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()));
}
}
}
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");
}
}
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;
}
}
3.5 Getting.java
Trong th mc src/guestbook ta to mt file Greeting.java c ni dung:
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;
11
@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;
}
}
12
13
14
3.9 theappengine-web.xml
App Engine cn mt tp tin cu hnh b sung tm ra cch trin khai v chy ng
dng. File ny c t tn appengine-web.xml , v nm trong WEB-INF/ cng vi web.xml .
N bao gm cc ID ng k ca cc ng dng ca bn (Eclipse to ra iu ny vi
mt ID trng bn in vo sau), s phin bn ca ng dng ca bn, v danh sch
cc file phi c i x nh cc file tnh (nh hnh nh v CSS) v cc file ti
nguyn (nh JSP v d liu ng dng khc).
Trong th mc war/WEB-INF/ , mt tp tin c tn appengine-web.xml c ni dung
sau y:
15
16
G lnh upload:
in mail v pass nu chng trnh hi, v upload thnh cng nh hnh di:
http://le-thao.appspot.com/
17
Hnh trn l giao din chnh ca ng dng . Ngi dng c th khng ng nhng
ngi khc khng bit bn l ai. V vy nn ng nhp trc khi vit bnh lun
Nh vy khi thnh vin vo sau s thy ni dung ngi vit trc v c th vit
nhng phn hi.v hnh thc cc bn thy ng dng nh mt din n hay blog
Khi thit k websie hay vit cc chng trnh i hi phi to CSDL v ni lu
CSDL .i vi ng dng ny bn khng cn phi quan tm n csdl lu u v truy
vn nh th no do c Google app engine lm.
V d ni dung bnh lun c lu trn Google app engine:
18
19