2010-04-29 70 views
0

我用下面的方法來從一個servlet發送的對象:爲什麼Java servlet不能發送一個對象?

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException 
    { 
    String Full_URL=request.getRequestURL().append("?"+request.getQueryString()).toString(); 

    String Contact_Id=request.getParameter("Contact_Id"); 
    String Time_Stamp=Get_Date_Format(6),query="select from "+Contact_Info_Entry.class.getName()+" where Contact_Id == '"+Contact_Id+"' order by Contact_Id desc"; 

    PersistenceManager pm=null; 
    try 
    { 
     pm=PMF.get().getPersistenceManager(); 

     // note that this returns a list, there could be multiple, DataStore does not ensure uniqueness for non-primary key fields 
     List<Contact_Info_Entry> results=(List<Contact_Info_Entry>)pm.newQuery(query).execute(); 

     Write_Serialized_XML(response.getOutputStream(),results.get(0)); 
    } 
    catch (Exception e) { Send_Email(Email_From,Email_To,"Check_License_Servlet Error [ "+Time_Stamp+" ]",new Text(e.toString()+"\n"+Get_Stack_Trace(e)),null); } 
    finally { pm.close(); } 
    } 

    /** Writes the object and CLOSES the stream. Uses the persistance delegate registered in this class. 
    * @param os The stream to write to. 
    * @param o The object to be serialized. 
    */ 
    public static void writeXMLObject(OutputStream os,Object o) 
    { 
    // Classloader reference must be set since netBeans uses another class loader to loead the bean wich will fail in some circumstances. 
    ClassLoader oldClassLoader=Thread.currentThread().getContextClassLoader(); 
    Thread.currentThread().setContextClassLoader(Check_License_Servlet.class.getClassLoader()); 

    XMLEncoder encoder=new XMLEncoder(os); 
    encoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception e) { e.printStackTrace(); }}); 
    encoder.writeObject(o); 
    encoder.flush(); 
    encoder.close(); 

    Thread.currentThread().setContextClassLoader(oldClassLoader); 
    } 

    private static ByteArrayOutputStream writeOutputStream=new ByteArrayOutputStream(16384); 

    /** Writes an object to XML. 
    * @param out The boject out to write to. [ Will not be closed. ] 
    * @param o The object to write. 
    */ 
    public static synchronized void writeAsXML(ObjectOutput out,Object o) throws IOException 
    { 
    writeOutputStream.reset(); 
    writeXMLObject(writeOutputStream,o); 
    byte[] Bt_1=writeOutputStream.toByteArray(); 
    byte[] Bt_2=new Des_Encrypter().encrypt(Bt_1,Key); 
    out.writeInt(Bt_2.length); 
    out.write(Bt_2); 
    out.flush(); 
    out.close(); 
    } 
    public static synchronized void Write_Serialized_XML(OutputStream Output_Stream,Object o) throws IOException { writeAsXML(new ObjectOutputStream(Output_Stream),o); } 

在接收端的代碼如下所示:

File_Url="http://"+Site_Url+App_Dir+File_Name; 
    try 
    { 
    Contact_Info_Entry Online_Contact_Entry=(Contact_Info_Entry)Read_Serialized_XML(new URL(File_Url)); 
    } 
    catch (Exception e) 
    { 
    e.printStackTrace(); 
    } 

    private static byte[] readBuf=new byte[16384]; 

    public static synchronized Object readAsXML(ObjectInput in) throws IOException 
    { 
    // Classloader reference must be set since netBeans uses another class loader to load the bean which will fail under some circumstances. 
    ClassLoader oldClassLoader=Thread.currentThread().getContextClassLoader(); 
    Thread.currentThread().setContextClassLoader(Tool_Lib_Simple.class.getClassLoader()); 

    int length=in.readInt(); 
    readBuf=new byte[length]; 

    in.readFully(readBuf,0,length); 
    byte Bt[]=new Des_Encrypter().decrypt(readBuf,Key); 
    XMLDecoder dec=new XMLDecoder(new ByteArrayInputStream(Bt,0,Bt.length)); 
    Object o=dec.readObject(); 
    Thread.currentThread().setContextClassLoader(oldClassLoader); 
    in.close(); 
    return o; 
    } 

    public static synchronized Object Read_Serialized_XML(URL File_Url) throws IOException { return readAsXML(new ObjectInputStream(File_Url.openStream())); } 

但我不能讓從物體在接收端的Java應用程序,爲什麼?該錯誤消息是這樣的:

java.lang.ClassNotFoundException: PayPal_Monitor.Contact_Info_Entry 
Continuing ... 
java.lang.NullPointerException: target should not be null 
Continuing ... 
java.lang.NullPointerException: target should not be null 
Continuing ... 
java.lang.NullPointerException: target should not be null 
Continuing ... 
+2

你將不得不添加一些信息:你有沒有得到任何答覆(連接拒絕,超時,404,內部服務器錯誤...)?該對象是否爲空或格式錯誤?你可以轉儲原始響應? – 2010-04-29 22:21:41

+1

你的命名約定是可恥的 – Bozho 2010-04-29 22:30:31

+1

我還沒有看到代碼如此襤褸幾個小時。 – 2010-04-29 22:35:05

回答

2
java.lang.ClassNotFoundException: PayPal_Monitor.Contact_Info_Entry 

即類應該是也存在於接收側的運行時類路徑。

+0

謝謝,在接收端有一個Contact_Info_Entry,一切都一樣,只是它在默認包中,不在PayPal_Monitor包中,這可能是問題嗎? – Frank 2010-04-30 01:52:37

+0

默認包總是一個問題。 [你之前解釋過](http://stackoverflow.com/questions/2701565/google-app-engine-classnotpersistencecapableexception/2725566#2725566)。 – BalusC 2010-04-30 02:02:02

+0

是的,我記得那個,但對默認包有點不公平,它是爲人們使用而設計的......那麼,從理論上講,讓一個類屬於發送端和同一類的包是理論上的錯誤在接收端的默認包中進行通信? – Frank 2010-04-30 02:14:48

相關問題