2010-04-27 49 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

相關問題