How to get thespecific entity from google app engine datastore according to the specific button click

242 views Asked by At

My Jsp file is like this. It has 2 buttons. When i click on 1st button then 1st entity from the datastore should be selected and same with the 2nd button.

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
 "http://www.w3.org/TR/html4/loose.dtd"> 
 <html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
 </head>
 <body>
    <body style="background-color:black;">


 <p>
  <p><input type="button" name="first" value="0C F0 0400" style="background-     
 color:#006600; color:#FFFFFF ; height:100px; width:200px"    
 onclick="location.href='hello';"> </p>
<p><input type="button" name="first" value="0D F0 0800" style="background-  
color:#006600; color:#FFFFFF ; height:100px; width:200px" 
 onclick="location.href='hello';">
</p>

</body>
</html>

This is the servlet. How to get the 1st entity on the 1st button click and 2nd entity on the 2nd button click.

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.EntityNotFoundException;
 import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet 
{
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws   
    IOException 
{
    Key k1 = KeyFactory.createKey("Employee","E1");    
    Key k2 = KeyFactory.createKey("Employee","E2");        

    String firstName1 = "Sam";
    String lastName1 = "Well";

    String firstName2 = "John";
    String lastName2 = "Morgan";


    Entity bs1 = new Entity(k1);
    Entity dm1 = new Entity(k2);

    bs1.setProperty("FN", firstName1);     
    bs1.setProperty("LN", lastName1); 

    dm1.setProperty("FN", firstName2);     
    dm1.setProperty("LN", lastName2);

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(bs1);
    datastore.put(dm1);

    Entity bs2 = null;
    Entity dm2 = null;

    try 
    {
        bs2= datastore.get(k1);
        dm2= datastore.get(k2);
    }

    catch (EntityNotFoundException e) 
    {
        e.printStackTrace();
    }

    String fName1= (String) bs2.getProperty("FN");
    String lName1= (String) bs2.getProperty("LN");

    String fName2= (String) dm2.getProperty("FN");
    String lName2= (String) dm2.getProperty("LN");

    resp.setContentType("text/plain");
    resp.getWriter().println("Person1 Details------> " + fName1 + "  " + lName1);   

    resp.getWriter().println("Person2 Details------> " + fName2 + "  " + lName2);   

}
}
1

There are 1 answers

2
Bharathi On

First you need to store the value into the datastore using doGet() method of servlet.

You can do that using this code:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws   
IOException 
{
   Entity employee1=new Entity("Employee","Employee1");  
   Entity employee2=new Entity("Employee","Employee2");
   employee1.setProperty("FN", "Sam");     
   employee1.setProperty("LN", "Sam"); 
   employee2.setProperty("FN", "John");     
   employee2.setProperty("LN", "Morgan");   
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
try{
       datastore.put(employee1);
      datastore.put(employee2);
   }
   catch(DatastoreFailureException d){
        d.printStackTrace();
    }
}

Then in the doPost Method get the value for the button clicked. JSP code I have answered in your other question.

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws   
IOException 
{
String key=req.getParameter("good");
Key key = KeyFactory.createKey("Employee", key);
Entity employee=datastore.get(key);
resp.setContentType("text/plain");
resp.getWriter().println("Person Details------> " + employee.getProperty("FN")+ "  " +employee.getProperty("LN"));
}