我有一个 servlet,我想通过UrlConnection
一些对象。当我测试它时,它工作得很好。当我通过 Java 原生对象(String
,File
..)。但是当我尝试传递我自己的Object
(我自己的类的实例)时,对象通过正确发送到 ServletObjectOutputStream
但是当我想在 servlet 端恢复对象时它不起作用。它会引发找不到我的班级的异常。
我MyObject
在带有小程序的项目中有类,它被加载并发送到 servlet,然后MyObject
我的 servlet 包中也有类......那么我必须在哪里放置MyObject
类来工作呢?
这是我的小程序代码,它MyObject
通过以下方式将实例发送到 servletURLConnection
URLConnection con = null;
ObjectOutputStream outputToServlet = null;
ObjectInputStream inputFromServlet = null;
MyObject my = new MyObject("200");
//open Url connection
con = CommunicationAgent.openConnectionWithServlet(CommunicationAgent.setUrlForServletConnection());
con.setDefaultUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
// creating ObjectOutputStream
try {
outputToServlet = new ObjectOutputStream(con.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(SoundAppletSimpleVersion.class.getName()).log(Level.SEVERE, null, ex);
}
//Send MyObject instance to server
try {
outputToServlet.writeObject(my);
} catch (IOException ex) {
Logger.getLogger(SoundAppletSimpleVersion.class.getName()).log(Level.SEVERE, null, ex);
}
这是来自我的 servlet 的代码,很容易从请求对象中获取流,然后我想将传入的对象复制到 MyObject
servlet 中的实例。这是一个问题并抛出异常
ObjectInputStream input = new ObjectInputStream(request.getInputStream());
MyObject my = null;
try {
my = (MyObject) input.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
System.out.print("nepovedlo se precist objekt");
}
Tomcat中抛出异常
INFO: Reloading Context with name [/Rozhlas_jsf] is completed
XII 04, 2012 3:43:22 DOP. Servlets.Servlet processRequest
SEVERE: null
java.lang.ClassNotFoundException: SoundApplet.MyObject
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:622)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at Servlets.Servlet.processRequest(Servlet.java:42)
at Servlets.Servlet.doPost(Servlet.java:120)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
并来自 Javaconsole
network: Connecting http://localhost:8084/Rozhlas_jsf/Servlet with proxy=DIRECT
XII 04, 2012 3:44:26 DOP. SoundApplet.SoundAppletSimpleVersion jButton8ActionPerformed
SEVERE: null
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8084/Rozhlas_jsf/Servlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at SoundApplet.SoundAppletSimpleVersion.jButton8ActionPerformed(SoundAppletSimpleVersion.java:708)
at SoundApplet.SoundAppletSimpleVersion.access$1000(SoundAppletSimpleVersion.java:27)
at SoundApplet.SoundAppletSimpleVersion$12.actionPerformed(SoundAppletSimpleVersion.java:371)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-3" java.lang.NullPointerException
at SoundApplet.SoundAppletSimpleVersion.jButton8ActionPerformed(SoundAppletSimpleVersion.java:717)
at SoundApplet.SoundAppletSimpleVersion.access$1000(SoundAppletSimpleVersion.java:27)
at SoundApplet.SoundAppletSimpleVersion$12.actionPerformed(SoundAppletSimpleVersion.java:371)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我想问题在于安排我的课程。我必须做什么才能正确工作?我需要MyObject
将实例接收到 servlet 中,然后在 servlet 中与他一起工作。它适用于原生 Java 对象,为什么 Java 无法MyObject
在SoundApplet.MyObject
.
在这种情况下,Java 如何处理类。例如,当发送 FIle 对象时,当我在 servlet Java 中收到它时,使用哪个类。File
我的 Applet 中的库中的File
类或带有 servlet 的 project 中的库中的类,或者没关系?我对这个问题真的很困惑,请尝试向我解释并提供帮助。
这是 MyObject 类真正放置在 Web 项目中已编译的 SoundApplet.jar 中的图像。