将自定义对象从applet传递到servlet

时间:2012-12-04 03:11:20

标签: java servlets applet classnotfoundexception urlconnection

我有一个servlet,我想通过UrlConnection一些对象。当我测试它时,它工作正常。当我传递Java本机对象(StringFile ..)时。但是当我尝试传递自己的Object(我自己的类的实例)时,Object通过ObjectOutputStream正确地发送到Servlet,但是当我想在servlet端恢复对象时,它不起作用。它抛出了我的课没有找到的异常。

我在项目中有MyObject类,其中applet加载并发送到servlet,然后我的servlet包中也有MyObject类...所以我必须放置MyObject上班吗?

这是我的applet代码,它通过MyObject

URLConnection实例发送到servlet
  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的代码,从请求对象轻松获取流,然后我想将传入的对象复制到servlet中的MyObject实例。这是一个问题,抛出异常

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)

我想问题在于放置我的课程。我必须做些什么来正确地工作?我需要在servlet中接收MyObject实例,然后在servlet中与他一起工作。它适用于本机Java对象,因为Java无法在MyObject中找到SoundApplet.MyObject类。

在这种情况下,Java如何使用类。例如,当我发送FIle对象时,当我在servlet Java中收到它时,使用哪个类。来自我的Applet中的库的File类或来自projectwith servlet的库中的File类,或者它无关紧要?我真的很困惑这个问题,请尝试解释我和帮助。

这是MyObject类实际放置在web项目中已编译的SoundApplet.jar中的图像。 enter image description here

1 个答案:

答案 0 :(得分:1)

来自The Apache Tomcat 5.5 Servlet/JSP Container: Quick Start

  
      
  • ..
  •   
  • 对于必须在所有Web应用程序中共享的类和资源,请将解压缩的类和资源放在$CATALINA_BASE/shared/classes下,或将包含这些类和资源的JAR文件放在$CATALINA_BASE/shared/lib下。
  •   

因此,如果SoundApplet.MyObject位于Jar(我推荐)中,请将其放在lib目录中。 如果它不在Jar中,请将类文件放在classes/SoundApplet目录中。另外,包名称应为alllowercase,例如soundapplet

另请注意,该类必须至少在 2个单独的位置,以供servlet和applet使用。上面概述了服务器类路径,但是applet(或者更确切地说是JVM)无法访问这些路径。