2010-04-27 50 views
-1

我在谷歌App Engine的dadastore以下类对象,我可以从“数据存储浏览器”看到他们:Google App Engine如何从servlet中获取对象?

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_ID Contact_Info_Entry的一个实例是 “ABC-123”,和我的应用程序ID为:NM-java的

当我的Java程序访问的网址:

"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" ??? 


    } 

弗兰克

回答

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(); 
} 

如果你可以用冗长的ID值(这是主键),而不是,你可以通过实体键直接加载它。

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

+0

谢谢,我也需要这部分! – Frank 2010-04-27 14:23:19

+0

@BalusC:是的,我在之前的其他程序中使用过OutputStream或Writer,但是我用它们发送了beck html字符串,而不是对象的实例,在这种情况下,“PrintWriter out = response.getWriter() ;”发回“Contact_Info_Entry My_Contact_Entry”?这是我卡住的地方。 – Frank 2010-04-27 14:44:22

+0

@Frank:看看ObjectOutputStream – Thilo 2010-04-27 14:57:51

相关问题