问题
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.
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?
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);
}
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");
}
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)
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)
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
.
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.
here is image that MyObject class is really placed in compiled SoundApplet.jar in web project..
回答1:
From The Apache Tomcat 5.5 Servlet/JSP Container: Quick Start:
- ..
- For classes and resources that must be shared across all web applications, place unpacked classes and resources under
$CATALINA_BASE/shared/classes
, or place JAR files containing those classes and resources under$CATALINA_BASE/shared/lib
.
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
.
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.
来源:https://stackoverflow.com/questions/13695487/passing-custom-objects-from-applet-to-servlet