简体   繁体   中英

Accessing and Displaying Data from the Datastore

I'm saving data to the datastore using:

    Entity pageText = new Entity("pageText");

    DatastoreService datastore =
            DatastoreServiceFactory.getDatastoreService();
    datastore.put(pageText);

But I'm unsure as of how to access and display that data from another servlet. Can anyone help out with this? Am I even saving this correctly?

Here is the full code for this servlet, just in case you want to take a look. Thank you!

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@SuppressWarnings("serial")
public class SaveServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {

        String pageOwner = (String) req.getAttribute("pageOwner");
        String pageName = (String) req.getAttribute("pageName");

        //String pageText = req.getParameter("pageText");

        Entity pageText = new Entity("pageText");

        DatastoreService datastore =
                DatastoreServiceFactory.getDatastoreService();
        datastore.put(pageText);

        Logger.getLogger("bleepbloop").warning("pageText = " + pageText);

        // Save the pageText into the datastore.

        resp.sendRedirect("/wiki/" + pageOwner + "/" + pageName);
    }
}

But I'm unsure as of how to access and display that data from another servlet.

So let's assume you have two App Engine apps deployed, app A and B . Now if I understand your question correctly, you would like to access app A 's datastore from within app B ? If this is the case, I'm afraid you cannot do this natively .

As you see in this similar question, each app has its own datastore which cannot natively be shared among other (separate) App Engine apps.

Now having said that, one way of sharing data between separate servlets would be to build an API on top of the datastore. So in your case, servlet A could expose getter and setter functions via a REST interface and servlet B could do whatever it needs to do with the data.

Note that in this scenario, servlet B does not necessarily have to be hosted on App Engine since all "datastore" calls now become simple HTTP calls.

PS If I've completely misunderstood your question and all you were actually asking was how to retrieve something from the datastore, here's one example :

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.PreparedQuery;
import com.google.appengine.api.datastore.Query;

// ...
// Get the Datastore Service
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// The Query interface assembles a query
Query q = new Query("pageText");

// PreparedQuery contains the methods for fetching query results
// from the datastore
PreparedQuery pq = datastore.prepare(q);

for (Entity result : pq.asIterable()) {
  String pageOwner = (String) result.getProperty("pageOwner");
  String pageName = (String) result.getProperty("pageName");
  System.out.println("Page " +pageName+ "'s owner is " +pageOwner);
}

This code should work

DatastoreService service = DatastoreServiceFactory.getDatastoreService();  
Query q = new Query("pageText"); 
PreparedQuery pq = service.prepare(q);
PrintWriter out = resp.getWriter();
for (Entity result : pq.asIterable())
{ 
            out.println(result.getProperty("pageOwner") + " " + result.getProperty("pageName"));  
}

I hope it helps /Dick Larsson

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM