简体   繁体   English

Servlet和Applet之间的通信在工作时失败

[英]Communication between Servlet & Applet fails when it was working

I have an applet (where the user logins) & on pressing login button I send the login details to my servlet. 我有一个小程序(用户登录)和按下登录按钮我将登录详细信息发送到我的servlet。 In my applet, the function responsible for sending the login details to the servelt & receiving the servlet response object FAILS . 在我的applet中,负责将登录详细信息发送到servelt并接收servlet响应对象FAILS的函数

My problem is: I dont know why my function sendObject( Object o ) fails & I dont know how to fix it. 我的问题是:我不知道为什么我的函数sendObject(对象o)失败,我不知道如何解决它。 Could you help me understand what is wrong & why? 你能帮我理解什么是错的吗?为什么?

I am using Apache 7.0. 我正在使用Apache 7.0。 I think that receiving the response (getting the input stream) from the servlet fails but I dont know why & logging in was working fine the other day so I know my WEB-INF folder is set up fine & the web.xml is fine. 我认为从servlet接收响应(获取输入流)失败但我不知道为什么&登录在前几天工作正常所以我知道我的WEB-INF文件夹设置正常并且web.xml很好。

My function (I document where the function fails): 我的函数(我记录函数失败的地方):

public ArrayList <Object> sendObject( Object o )
{
    try
    {
        URL servletUrl = new URL( this.getCodeBase(), "Updater" ); // "http://localhost:8080/" );
        URLConnection conn = servletUrl.openConnection();

        conn.setDoInput( true );
        conn.setDoOutput( true );
        conn.setUseCaches( false );
        conn.setRequestProperty( "Content-Type", "application/x-java-serialized-object" );

        OutputStream out = conn.getOutputStream();
        ObjectOutputStream objOut = new ObjectOutputStream( out );
        objOut.writeObject( o ); 
        objOut.flush(); 
        objOut.close();

        // receive result from servlet
        System.out.println( "Result from applet.getCodeBase()=" + this.getCodeBase() );
     // The following line is where I get the exception thrown
        InputStream instr = conn.getInputStream();              
        ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
        ArrayList <Object> result = (ArrayList <Object>) inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();

        System.out.println( "Contents of output is " );
        for (int i=0; i<result.size(); i++)
        {
            System.out.println( "" + result.get(i) );
        }

        return result;
    }
    catch ( IOException e )
    {
        System.out.println( "In sendObject(): " + e );
    }
    catch ( Exception e )
    {
        System.out.println( "In sendObject(): " + e );
    }

    return null;
}    

Java Console Output: Java控制台输出:

Result from applet.getCodeBase() = http://localhost:8080/Updater/ 来自applet.getCodeBase()= http:// localhost:8080 / Updater /的结果
In sendObject(): java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/Updater/Updater 在sendObject()中:java.io.IOException:服务器返回HTTP响应代码:500为URL: http:// localhost:8080 / Updater / Updater

From the Apache log: 从Apache日志:

20/02/2011 11:15:08 AM org.apache.catalina.core.ApplicationContext log 20/02/2011 11:15:08 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized() 信息:ContextListener:contextInitialized()
20/02/2011 11:15:08 AM org.apache.catalina.core.ApplicationContext log 20/02/2011 11:15:08 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized() 信息:SessionListener:contextInitialized()
20/02/2011 11:15:23 AM org.apache.catalina.core.ApplicationContext log 20/02/2011 11:15:23 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Updater as unavailable 信息:将servlet更新程序标记为不可用
20/02/2011 11:15:23 AM org.apache.catalina.core.StandardWrapperValve invoke 20/02/2011 11:15:23 AM org.apache.catalina.core.StandardWrapperValve调用
SEVERE: Allocate exception for servlet Updater 严重:为servlet更新程序分配异常
java.lang.NullPointerException 显示java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125) 在javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)
at UpdaterServlet.(UpdaterServlet.java:32) // REFER BELOW TO SEE LINE 32 OF THIS FILE 在UpdaterServlet。(UpdaterServlet.java:32)//请参阅以下内容,查看此文件的第32行
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source) 在java.lang.Class.newInstance0(未知来源)
at java.lang.Class.newInstance(Unknown Source) 在java.lang.Class.newInstance(未知来源)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048) 在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:799) 在org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:799)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source) 在java.lang.Thread.run(未知来源)

localhost_access_log...: localhost_access_log ...:

127.0.0.1 - - [20/Feb/2011:11:15:12 +1100] "GET /Updater/index.html HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:12 +1100]“GET /Updater/index.html HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:15 +1100] "GET /Updater/UpdaterApplet.class HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:15 +1100]“GET /Updater/UpdaterApplet.class HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:15 +1100] "GET /Updater/ContentTree.class HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:15 +1100]“GET /Updater/ContentTree.class HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:15 +1100] "GET /Updater/ContentMap.class HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:15 +1100]“GET /Updater/ContentMap.class HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:15 +1100] "GET /Updater/UpdaterApplet$1.class HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:15 +1100]“GET /Updater/UpdaterApplet$1.class HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:15 +1100] "GET /Updater/SQLInterface.class HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:15 +1100]“GET /Updater/SQLInterface.class HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:23 +1100] "GET /Updater/ObjSerializationInterface.class HTTP/1.1" 304 - 127.0.0.1 - - [20 / Feb / 2011:11:15:23 +1100]“GET /Updater/ObjSerializationInterface.class HTTP / 1.1”304 -
127.0.0.1 - - [20/Feb/2011:11:15:23 +1100] "POST /Updater/Updater HTTP/1.1" 500 3117 127.0.0.1 - - [20 / Feb / 2011:11:15:23 +1100]“POST / Updater / Updater HTTP / 1.1”500 3117

This is the UpdaterServlet.java file (if you see above theres an error at line 32 of this file): 这是UpdaterServlet.java文件(如果您在上面看到此文件第32行的错误):

public class UpdaterServlet extends HttpServlet
{
// Class Variables:
private static final long serialVersionUID = 1L;
private ArrayList <File> localDatabases;
private ArrayList <ArrayList <Object>> input;
private ArrayList <ArrayList <Object>> output;

// Class Methods:

public UpdaterServlet()
{
    getServletContext().log( "File open/created" ); // LINE 32
    localDatabases = new ArrayList <File>();

It looks like getServletContext() returns null ; 看起来getServletContext()返回null ; are you also overriding the init(ServletConfig) method in the UpdaterServlet ? 你是否也重写了UpdaterServletinit(ServletConfig)方法? If so, make sure to call super.init(config) . 如果是这样,请务必调用super.init(config)

This NPE means that the ServletConfig is not present (from where the ServletContext is to be returned). 这个NPE意味着ServletConfig不存在(从返回ServletContext位置)。 This is indeed not available in the constructor of the servlet. 这确实在servlet的构造函数中不可用。 The servletcontainer sets it after construction of the servlet. servletcontainer 构造servlet 之后设置它。 You need to do the desired job in init() method instead. 您需要在init()方法中执行所需的工作。

@Override
public void init() {
    getServletContext().log( "File open/created" ); // LINE 32
    localDatabases = new ArrayList <File>();
    // ...
}

Unrelated to the concrete problem, the stacktrace also hints that your servlet is in the default package. 与具体问题无关,堆栈跟踪还暗示您的servlet在默认包中。 This will only work in very specific circumstances (certain Apache Tomcat versions in combination with certain JDK versions). 这仅适用于非常特定的情况(某些Apache Tomcat版本与某些JDK版本结合使用)。 I strongly recommend to put the servlet class in a package and alter the web.xml accordingly so that you aren't dependent on that anymore. 我强烈建议将servlet类放在一个包中并相应地更改web.xml ,这样你就不再依赖它了。

So, does your webserver know what to do on /Updater/Updater ? 那么,您的网络服务器是否知道在/Updater/Updater上要做什么? It looks like not. 看起来不像。 Take a look at the error log, there should be more information. 看一下错误日志,应该有更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM