本文介绍了从applet的传递自定义对象与Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个servlet,我想通过的URLConnection 一些对象。当我测试了它,它工作得很好。当我通过Java本地对象(字符串文件 ..)。但是,当我试图通过我自己的对象(我自己的类的实例),对象通过的ObjectOutputStream 但是当我想挽回的servlet端对象这是行不通的。它会抛出异常,我的类没有被发现。

I have a servlet and i want to pass through UrlConnection some objects. When I tested it, it worked fine. When I passed Java native objects (String, File..). But when I try to pass my own Object (instance of my own Class), Object is sent into Servlet correctly via ObjectOutputStream but when I wanted to restore object in servlet side it doesn't work. it throws exceptions that My class was not found.

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

I have MyObject class in project with applet where is loaded and sent to servlet and then i have MyObject class in my servlet package too... So where I must place MyObject class to work it?

下面是我的小程序code这发为MyObject 实例通过的URLConnection

Here is my applet code which send MyObject instance to servlet via 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);
        }

下面是code从我的servlet,从请求对象都很简单,把流,然后我想进入的对象复制到为MyObject 实例的servlet。这里是一个问题,异常被抛出

Here is code from my servlet, easy take stream from request object and then I want to copy incoming object to MyObject instance in servlet. Here is a problem and exception is thrown

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

Exception thrown in 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

and from 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为什么不能找到 SoundApplet.MyObject 为MyObject 类。

I guess problem is in placing my classes. What I must do to work it correctly? I need Receive MyObject instance into servlet and then work with him in servlet. It works for native Java objects why Java cant find MyObject class in SoundApplet.MyObject.

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

How Java work with classes in this case. When sending FIle object for example, when I receive it in servlet Java use Which class for it. File Class from libraries in my Applet or File class from libraries in projectwith servlet or it doesnt matter? I am really confused in this questions please try to explain me it and help.

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

here is image that MyObject class is really placed in compiled SoundApplet.jar in web project..

推荐答案

从的:


      
  • ..

  •   
  • 类和资源必须在所有的Web应用程序所共享,放置在 $ CATALINA_BASE /共享/班,或含有这些类的地方JAR文件和未包装的类和资源在 $ CATALINA_BASE /共享/ lib目录资源。

  •   

所以,如果 SoundApplet.MyObject 是在JAR(我推荐),将其放置在 LIB 目录。
如果不是在JAR,放置在类/ SoundApplet 目录下的类文件。顺便说一句,包名应该是 alllowercase ,例如 soundapplet

So if SoundApplet.MyObject is in a Jar (which I recommend), place it in the lib directory.If it is not in a Jar, place the class file in the classes/SoundApplet directory. As an aside, package names should be alllowercase, e.g. soundapplet.

另外请注意,这个类必须至少在 2不同的地点以由servlet和小程序都可以使用。服务器类路径是在上面所列的,但小程序(或者更确切地说,JVM)无法访问这些路径的任何。

Also note that the class needs to be in at least 2 separate locations to be used by both the servlet and applet. The server class-path is outlined above, but applets (or rather the JVM) cannot access either of those paths.

这篇关于从applet的传递自定义对象与Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:34
查看更多