|
在做游戏开发的时候,很多游戏都会提供一个全球分数排名的的功能,这就需要使用服务器了,当然,你可以选择搭建自己的服务器,但从成本,稳定性和开发周期上看,我认为Google AppEngine是个很不错的选择.
java代码:
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;
//标记对象可以持久化
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
//标记主键,作以后查询之用
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private String firstName;
@Persistent
private String lastName;
@Persistent
private Date birthDate;
public Employee(String firstName, String lastName, Date birthDate) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
public String getFirstName() {
return firstName;
}
}
复制代码
步骤2.存取Bean(可以写在Servlet的doGet()或者doPost()里面):
java代码:
import java.io.IOException;
import java.util.Date;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse resp)throws IOException {
resp.setContentType("text/plain");
Employee e = new Employee("Alfred", "Smith", new Date());
//获取用以存取对象的PersistenceManager
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
//存储对象
resp.getWriter().println("saving object!");
pm.makePersistent(e);
}catch(Exception ex){
resp.getWriter().println("cannot persistent!");
ex.printStackTrace();
}
finally {
pm.close();
}
try{
//再次获取用以存取对象的PersistenceManager
pm = PMF.get().getPersistenceManager();
//由主键检索对象,Alfred之前被定义为主键的
Employee newE = pm.getObjectById(Employee.class, "Alfred");
//读取新对象的信息检测对象检索是否成功
resp.getWriter().println("Employee:"+newE.getFirstName());
}catch(Exception ex){
ex.printStackTrace();
resp.getWriter().println("cannot get data!");
pm.close();
}
}
@Override
protected void doPost(HttpServletRequest req,
HttpServletResponse resp)throws ServletException, IOException {
this.doGet(req, resp);
}
}
复制代码
其中PMF类的代码如下,没看错应该是个单例模式:
java代码:
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;
}
}
复制代码
使用Google 账户认证服务:
java代码:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
public class GoogleAccount extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//创建用户服务
UserService userService = UserServiceFactory.getUserService();
String thisURL = req.getRequestURI();
//通过请求对象的 getUserPrincipal() 方法测试用户是否使用 Google 帐户登录
if (req.getUserPrincipal() != null) {
//如果有登录,则创建Login地址
resp.getWriter().println("<p>Hello, " +
req.getUserPrincipal().getName() +
"! You can <a href=\"" +
userService.createLogoutURL(thisURL) +
"\">sign out</a>.</p>");
} else {
//如果没有登录,则创建Login地址
resp.getWriter().println("<p>lease <a href=\"" +
userService.createLoginURL(thisURL) +
"\">sign in</a>.</p>");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
复制代码 |
|