You are on page 1of 19

MC LC

1 Gii thiu Google App Engine.........................................................................................................2


2 Hng dn ci t............................................................................................................................2
2.1 Yu cu:..................................................................................................................................................2
2.2 ng k ti khon Google App Engine:................................................................................................2

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

4 Trin khai ng dng.......................................................................................................................16


4.1 Chun b:..............................................................................................................................................16
4.2 Thc hin upload.................................................................................................................................16

5 Chy chng trnh..........................................................................................................................17

1 Gii thiu Google App Engine


Google App Engine (GAE) l mt nn tng hosting bao gm web server, c s d
liu BigTable and kho lu tr file GFS. GAE cho php bn vit ng dng web da
trn c s h tng ca Google. Ngha l bn khng cn quan tm l trang web bn
c lu tr nh th no (k c database i km), m ch cn quan tm n vic pht
trin ng dng theo cc API do Google cung cp. Vi App Engine, NSD ch cn ti
ln cc ng dng ca bn, v n sn sng phc v ngi dng ca bn.
NSD c th s dng tn min ring ca mnh (nh http://www.example.com/) thng qua
Google Apps. Hoc c th dng sub-domain min ph ca appspot.com.
GAE cho php c host min ph vi dung lng 500 MB lu tr v cho php 10
GB bng thng lu chuyn mi ngy hay tng ng 5 triu pageview hng
thng,Vt qua mc ny bn s phi tr ph. Dng GAE, chng ta khi phi thit k
database, vit SQL truy vn data, map data v object. Chng ta ch cn design cc
class v GAE t ng lo phn lm vic vi database.
NSD ch cn phi ngh ra v vit nhng ng dng ri mi s dng. Tuy nhin, mt
tri ca vic xy dng ng dng trn GAE l bn s ph thuc hon ton vo cc
cng ngh ca Google v rt kh c th tch ra thnh mt ng dng c lp.
Hin AppEngine h tr 2 loi ngn ng l: Python v Java. Mt s ngn ng khc
nh PHP cng c th chy c nu ci cng vi b chuyn t PHP sang Java.

2 Hng dn ci t
2.1
-

Yu cu:
Eclipse hay jcreator
Google plugin cho Eclipese
AppEngine-java-SDK-1.3.8.zip

2.2 ng k ti khon Google App Engine:


Bc 1: trin khai cc ng dng ca bn vi cc m my ca Google, cn mt
ti khon AppEngine thng qua ti khon email ca Google.
M http://appengine.google.com/ v ng nhp vi thng tin ti khon Gmail

Bc 2: Chn nt Create Application

Bc 3: Xc minh ti khon ca bn thng qua mt s in thoi hp l. Google s


nhn mt m xc minh qua SMS.

Bc 4: Nhp m xc nhn ca google

Bc 5: Tin hnh to mt ng dng. NSD c php to c 10 ng dng cho


mt ti khon gmail.

Giao din chnh ca ng dng nh sau:

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

3.2 The servlet class


Cc ng dng App Engine Java s dng java servlet API tng tc vi my
ch web. Mt HTTP Servlet l mt lp ng dng c kh nng x l v phn hi cc
yu cu web. Lp ny tha k lp javax.servlet.GenericServlet hoc lp
javax.servlet.http.HttpServlet.
Trong th mc src/guestbook to mt file c tn GuestbookServlet.java c ni
dung:

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()));
}
}
}

Google App Engine cung cp mt s dch v hu ch da trn c s h tng Google,


c th truy cp bi cc ng dng bng cch s dng th vin c trong SDK.
Trong c dch v ngi dng cho php tch hp cc ng dng ca bn vi ti
khon ngi dng Google. Cc NSD khc c th s dng Google cc ti khon m
h c ng nhp vo ng dng ca bn.
Trong on code trn, class GuestbookServlet s dng Users API kim tra xem
ngi dng ng nhp vi ti khon Google. Nu cha, th ngi dng s c
chuyn n mn hnh ng nhp ti khon Google. userService.createLoginURL(...) s tr v URL
ca mn hnh ng nhp.

3.3 Signgustbookservlet.java file


Trong src/guestbook/ to 1 file SignGuestbookServlet.java c ni dung sau:

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");
}
}

on code ny to i tng Greeting mi bng vic gi hm. lu i tng


vo kho d liu, n to ra PersistenceManager nh vo vic s dng PersistenceManagerFactory. Khi
makePersistent c tr v, cc i tng mi c lu trong kho d liu.

3.4 PMF file


Mi khi yu cu s dng, kho d liu s to ra mt i tng mi ca lp
PersistenceManager thong qua th class PersistenceManagerFactory.
Trong th mc src/guestbook tao mt file PMF.java c ni dung:
10

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;
}
}

Lp ny s nh ngha 3 thuc tnh: author, content v date. Ba trng ny s


c ch thch @Persistence thng bo cho DataNucleus lu chng nh 3 i
tng thuc tnh trong Goolge App.
3.6 jdoconfig.xml file
Trong th mc war/WEB-INF/classes/META-INF/, ta to file jdoconfig.xml cu
hnh Goole APP thnh nn tng try xut.

12

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


<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/
jdoconfig">
<persistence-manager-factory name="transactions-optional">
<property
name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersist
enceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL"
value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead"
value="true"/>
<property
name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues"
value="true"/>
<property
name="datanucleus.appengine.autoCreateDatastoreTxns"
value="true"/>
</persistence-manager-factory>
</jdoconfig>

3.7 guestbook.jsp file


Trong war/ ta to 1 file guestbook.jsp lm giao din cho ngi s dng post cc
bnh lun.

13

<p>The guestbook has no messages.</p>


<%
} else {
for (Greeting g : greetings) {
if (g.getAuthor() == null) {
%>
<p>An anonymous person wrote:</p>
<%
} else {
%>
<p><b><%= g.getAuthor().getNickname() %></b> wrote:</p>
<%
}
%>
<blockquote><%= g.getContent() %></blockquote>
<%
}
}
pm.close();
%>
<form action="/sign" method="post">
<div><textarea name="content" rows="3"
cols="60"></textarea></div>
<div><input type="submit" value="Post Greeting" /></div>
</form>
</body>
</html>
-Test case

1: Nhp sai cc thng tin SQL server connect.


Server thng bo, kt ni khng thnh cng.

3.8 web.xml file


Khi my ch web nhn c yu cu s xc nh servlet class gi bng cch s
dng mt file cu hnh c gi l "m t trin khai ng dng web."
File ny c t tn l web.xml , v nm trong war/WEB-INF/ th mc trong
WAR. WEB-INF/ v web.xml l mt phn ca c t servlet.

14

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


<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
version="2.5">
<servlet>
<servlet-name>guestbook</servlet-name>
<servlet-class>guestbook.GuestbookServlet</servletclass>
</servlet>
<servlet-mapping>
<servlet-name>guestbook</servlet-name>
<url-pattern>/guestbook</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sign</servlet-name>
<servletclass>guestbook.SignGuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>guestbook.jsp</welcome-file>
</welcome-file-list>
</web-app>

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

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


<appengine-web-app
xmlns="http://appengine.google.com/ns/1.0">
<application></application>
<version>1</version>
<system-properties>
<property name="java.util.logging.config.file"
value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>

4 Trin khai ng dng


upload code v cc file chng trnh ca bn ln Google App Engine ta dng mt
tp lnh c trong SKD c tn: appcfg.cmd
4.1 Chun b:
a ID vo ng dng: Cn 1 ID application c ng k trn google upload
ng dng, theo nh cch ng k phn trn, ta c c ID application l : le-thao
Vo thc mc war/WEB-INF/appengine-web.xml m file appengine-web.xml ln,
t ID application vo gia th <application> </application> ri save li nh hnh v

4.2 Thc hin upload


Gii nn ra D ta c D:\appengine-java-sdk, m cmd, chuyn v D:

16

G lnh upload:

D:\\appengine-java-sdk\bin\appcfg.cmd update D:\guestbook\war

in mail v pass nu chng trnh hi, v upload thnh cng nh hnh di:

5 Chy chng trnh


M trnh duyt, truy cp vo a ch:

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

You might also like