创建实体并设置属性

package pack.exp;
import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class IkaiLanServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws...
{
    Entity alice = new Entity("Person", "Alice");
    alice.setProperty("gender", "female");
    alice.setProperty("age", 20);

    Key bobKey = KeyFactory.createKey("Person", "Bob");
    Entity bob = new Entity(bobKey);
    bob.setProperty("gender", "male");
    bob.setProperty("age", "23");

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(alice);
    datastore.put(bob);


    resp.setContentType("text/plain");
    resp.getWriter().println("Bye Bye");
}
}


在同一包中创建另一个servlet

package pack.exp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class Read extends HttpServlet
{

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws...
{
    // TODO Auto-generated method stub
    super.doGet(req, resp);

    Key bobKey = KeyFactory.createKey("Person", "Bob");
    Key aliceKey = KeyFactory.createKey("Person", "Alice");

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity alice, bob;

    try {
        alice = datastore.get(aliceKey);
        bob = datastore.get(bobKey);

        Long aliceAge = (Long) alice.getProperty("age");
        Long bobAge = (Long) bob.getProperty("age");
        System.out.println("Alice’s age: " + aliceAge);
        System.out.println("Bob’s age: " + bobAge);
    }

    catch (EntityNotFoundException e)
    {
        // Alice or Bob doesn't exist!
    }
}
}


当我部署应用程序时,输出为“再见”。为什么它不读取实体。
请帮助我,我是Google App Engine数据存储的新手。

最佳答案

当您转到已映射到IkaiLanServlet的任何URL时,由于您调用resp.getWriter().println("Bye Bye"),它将以“ Bye Bye”响应。要读取实体,请更改

System.out.println("Alice’s age: " + aliceAge);
System.out.println("Bob’s age: " + bobAge);




resp.setContentType("text/plain");
resp.getWriter().println("Alice’s age: " + aliceAge);
resp.getWriter().println("Bob’s age: " + bobAge);


根据文档(https://developers.google.com/appengine/docs/java/#Java_Logging):


Servlet写入标准输出流(System.out)和标准错误流(System.err)的所有内容均由App Engine捕获,并记录在应用程序日志中。写入标准输出流的行以“ INFO”级别记录,而写入标准错误流的行则以“ WARNING”级别记录。


要在浏览器中查看输出,必须使用resp

另外,请确保依次访问IkaiLanServletRead的URL,以确保实体位于数据存储区中。

10-08 01:12