【问题标题】:Why can't Java servlet sent out an object?为什么 Java servlet 不能发出对象?
【发布时间】:2023-03-09 21:35:01
【问题描述】:

我使用以下方法从 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 ...

【问题讨论】:

  • 您必须添加更多信息:您是否收到任何响应(连接拒绝、超时、404、内部服务器错误...)?对象是 null 还是格式错误?你能转储原始响应吗?
  • 你的命名习惯是可耻的
  • 我已经好几个小时没见过这么邋遢的代码了。
  • 看起来像是混合的 C#/PHP 风格。其他 Java 人无法快速解释该代码。请仔细阅读:java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html,尤其是第 9 章。

标签: java servlets


【解决方案1】:
java.lang.ClassNotFoundException: PayPal_Monitor.Contact_Info_Entry

该类也应该存在于接收方的运行时类路径中。

【讨论】:

  • 谢谢,在接收端有一个Contact_Info_Entry,一切都一样,只是在默认包里,不在PayPal_Monitor包里,会不会是问题?
  • 默认包总是一个问题。 You was explained that before.
  • 是的,我记得,但这对默认包有点不公平,它是为人们设计的......除此之外,让一个类属于一个包在理论上是错误的吗发送端和接收端默认包中的同一个类进行通信?
  • 这也是错误的。您可以拥有具有相同名称但位于不同包中的类。以现实世界为例:java.util.Datejava.sql.Date。默认包仅适用于小型和简单的一类或可能两类大小的应用程序,就像您在基础教程中所强调的那样。但是在现实世界的企业应用程序中呢?不,忘了它。永远。所有其他类都不会看到无包类。
  • 好点,谢谢。为了解决这个问题,我可以做些什么来在双方之间进行通信,具有相同结构的对象:相同的字符串类型、int 类型、向量,但它们只是属于每一侧的不同包?来回传递它们的实际解决方案是什么?
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 2014-08-13
  • 2019-01-15
  • 2013-01-25
相关资源
最近更新 更多