서블릿이 있는데 어떤 객체를 UrlConnection
통해 전달하려고합니다. 내가 그것을 테스트했을 때, 그것은 잘 동작했다. Java 원시 객체 (String
, File
..)를 전달했습니다. 그러나 내 자신의 Object
(내 자신의 클래스의 인스턴스)을 전달하려고하면 객체는 ObjectOutputStream
을 통해 Servlet으로 올바르게 전송되지만 서블릿 측에서 객체를 복원하려고 할 때 작동하지 않습니다. My 클래스가 발견되지 않는 예외를 throw합니다.애플릿에서 서블릿으로 사용자 정의 객체 전달
내가 서블릿으로로드 된 다음 애플릿과 함께 프로젝트에있는 MyObject
클래스를 가지고 있고 나서도 서블릿 패키지에 MyObject
클래스가 있습니다 ... 그래서 내가 배치해야하는 곳은 MyObject
클래스입니다. 여기
MyObject
인스턴스를 보내 내 애플릿 코드가 나는 서블릿에서
MyObject
인스턴스에 들어오는 개체를 복사하려면 다음 여기에
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);
}
요청 객체에서 내 서블릿 코드, 쉽게 걸릴 스트림이를 통해 서블릿하는 것입니다 . 여기에 문제가 및 예외가 나는 문제가 내 수업을 배치에 추측
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)
Javaconsole
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)
톰캣
과에서 던져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");
}
예외를 슬로우됩니다. 올바르게 작동하려면 어떻게해야합니까? 서블릿에 MyObject
인스턴스를 받아야하고 서블릿에서 그와 함께 작업하십시오. 네이티브 Java 객체에서 Java가 왜클래스를 찾았는지 SoundApplet.MyObject
에서 알 수 있습니다.
이 경우 Java가 클래스와 함께 작동하는 방식. 예를 들어, FILE 객체를 보낼 때, 서블릿에서 Java 객체를받을 때 어떤 클래스를 사용할 것인가. File
내 애플릿에있는 라이브러리의 클래스 또는 File
클래스의 projectwith 서블릿에있는 라이브러리에서 클래스가 문제가되지 않습니까? 나는이 질문에 정말로 혼란 스럽다. 나에게 그것을 설명하고 도와 주려고 노력해라. 여기
내 SoundApplet.jar을 웹 응용 프로그램의 /WEB-INF/lib/SoundApplet.jar에 추가했는데 Tomcat에서이 예외가 발생했습니다. [/ servlet] 경로가있는 서블릿 [Servlet]의 SEVERE : Servlet.service() Rozhlas_jsf]가 예외를 던졌습니다 java.lang.ClassCastException : SoundApplet.MyObject를 Servlets.MyObject로 캐스팅 할 수 없음 –
더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/) (서블릿 및 애플릿 코드) . –
좋아, 나는 여기서 뭘하고 싶은지 간단한 일을 시도했다. 두 프로젝트가있다. 하나는 애플릿 프로젝트이고 다음은 내 웹 프로젝트입니다. 여기 링크가 있습니다. [link] (http://www.sendspace.com/file/8bngey) 실행할 수 있습니까? @AndrewThompson –