Google App Engine如何从servlet中获取对象?

时间:2010-04-27 13:49:51

标签: google-app-engine servlets object

我在Google App Engine的dadastore中有以下类对象,我可以从“数据存储查看器”中看到它们:

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() { }
......
}

我的java应用程序如何从servlet url获取对象?例如,如果有一个Contact_Info_Entry实例谁的Contact_Id是“ABC-123”,我的App ID是:nm-java

当我的java程序访问url时:

 "http://nm-java.appspot.com/Check_Contact_Info?Contact_Id=ABC-123

Check_Contact_Info servlet如何从数据存储中获取对象并将其返回给我的应用程序?

public class Check_Contact_Info_Servlet extends HttpServlet
{
  static boolean Debug=true;

  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException
  {

  }
...
  protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,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" ???


  }

1 个答案:

答案 0 :(得分:0)

由于Contact_Id不是主键,因此您需要创建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();
}

如果您可以使用Long Id值(它是主键),您可以直接通过实体键加载它。

如果要将实体从Servlet发送到Java客户端,可以使用Java的序列化(您的类是可序列化的)。