Reputation: 1141
I'm have a Servlet which saves an entity into datastore and a jsp which displays the content of the table. I've noticed that datastore automatically creates an unique ID/Name for each new row added to a table. When I try to display the ID/Name value in my jsp, I get null value returned. Following is my code. I was wondering if anyone could tell me how to get the ID/Name value from the table. Thank you so much in advance.
public class RegisterForAccountServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String email = req.getParameter("email");
Key userKey = KeyFactory.createKey("RegisterUserKey", email);
Entity user = new Entity("RegisteredUsers", userKey);
user.setProperty("email", email);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(user);
resp.sendRedirect("/ListOfAllUsers.jsp");
}
}
following is the jsp which displays all the registered users:
<%
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
// Run an ancestor query to ensure we see the most up-to-date
// view of the Greetings belonging to the selected Guestbook.
Query query = new Query("RegisteredUsers");
List<Entity> users = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
if (users.isEmpty())
{
%>
<p>no registered users</p>
<%
}
else
{
%>
<%
for (Entity user : users)
{
%>
<table border="1">
<tr>
<td><%= user.getProperty("ID/Name") %></td>
<td><%= user.getProperty("email") %></td>
</tr>
</table>
<%
}//for
}//else
%>
Upvotes: 2
Views: 3376
Reputation: 80330
The Name/ID is part of the entity key. Use user.getKey().getName()
.
Upvotes: 5