从applet的传递自定义对象与Servlet(Passing custom Objects fro

2019-10-17 11:08发布

我有一个servlet,我想通过传递UrlConnection一些对象。 当我测试了它,它工作得很好。 当我通过Java本地对象( StringFile ..)。 但是,当我试图通过我自己的Object (我自己的类的实例),目的是通过正确发送到Servlet的ObjectOutputStream ,但是当我想挽回的servlet端对象这是行不通的。 它抛出我的类没有被发现异常。

MyObject与小程序项目类被加载并发送至servlet的地方,然后我有MyObject类在我的servlet包太...那么,我必须把MyObject类的工作呢?

这是送我的applet代码MyObject例如通过与Servlet URLConnection

  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为什么不能找到MyObjectSoundApplet.MyObject

如何与Java在这种情况下类工作。 当发送文件对象,例如,当我收到它的servlet的Java使用哪个类的。 File从我的小程序或库类File类从库中的项目里servlet或它并不重要? 我真的很困惑这个问题,请试着解释我来说帮助。

这里是形象,为MyObject类是真正摆在编译SoundApplet.jar在Web项目..

Answer 1:

从使用Apache Tomcat 5.5的Servlet / JSP容器:快速启动 :

  • ..
  • 对于类和资源,必须在所有Web应用程序共享,将未包装的类和资源在$CATALINA_BASE/shared/classes ,或含有根据这些类和资源的地方JAR文件$CATALINA_BASE/shared/lib

所以,如果SoundApplet.MyObject是在一个罐子里(我推荐),将其放置在lib目录。 如果不是在罐中放置在类文件classes/SoundApplet目录。 顺便说一句,包名应该alllowercase ,如soundapplet

还要注意的是类需要在到由servlet和小应用程序都可以使用至少2个单独的位置 。 服务器类路径是在上面概述的,但小应用程序(或者更确切地说,JVM)不能访问任何这些路径的。



文章来源: Passing custom Objects from applet to servlet