Google App Engine how to get an object from the servlet?
I have the following class objects in Google App Engine's dadastore, I can see them from the "Datastore Viewer " :
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 Contact_Info_Entry implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
Long Id;
public static final long serialVersionUID=26362862L;
String Contact_Id="",First_Name="",Last_Name="",Company_Name="",Branch_Name="",Address_1="",Address_2="",City="",State="",Zip="",Country="";
double D_1,D_2;
boolean B_1,B_2;
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
How can my java applications get the object from a servlet url ? For instance if have an instance of Contact_Info_Entry who's Contact_Id is "ABC-123", and my App Id is : nm-java
When my java program accesses the url :
"http://nm-java.appspot.com/Check_Contact_Info?Contact_Id=ABC-123
How will the Check_Contact_Info servlet get the object from datastore and return it to my app ?
public class Check_Contact_Info_Servlet extends HttpServlet
{
static boolean Debug=true;
public void doGet(HttpServletRequest reque开发者_如何转开发st,HttpServletResponse response) throws IOException
{
}
...
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,response); }
}
Sorry I need to be more specific, how to send the object out in the response ?
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException
{
PrintWriter out=response.getWriter();
Contact_Info_Entry My_Contact_Entry;
... get My_Contact_Entry from datastore ...
??? How to send it out in the "response" ???
}
Frank
Since Contact_Id is not the primary key, you need to create a query:
Query query = pm.newQuery(Contact_Info_Entry.class);
query.setFilter("Contact_Id == idParam");
query.declareParameters("String idParam");
try {
List<Contact_Info_Entry> results = (List<Contact_Info_Entry>)
query.execute("ABC-123");
// note that this returns a list, there could be multiple,
// DataStore does not ensure uniqueness for non-primary key fields
} finally {
query.closeAll();
}
If you could use the Long Id value (which is the primary key) instead, you could load it directly by entity key.
If you want to send the entity from the Servlet to a Java client, you could use Java's serialization (your class is serializable).
精彩评论