Search code examples
javaservletsappleteofexception

EOFException when sending data from Applet to a servlet


I am trying to get simple user Details (Name,Phone No, Gender(Option Box)) in an Applet and display the details in an JSP.I put all the three details in an HashMap and send it in a output Stream. The Applet Code is as Follows.

    try 
    {
        userUrl = "http://localhost:8080/AppletTest/display.jsp";

       /* In the web.xml file I have mapped display.jsp to the Servlet */

        testServlet = new URL(userUrl.toString());
        servletConnection = testServlet.openConnection();

        servletConnection.setDoOutput(true);
        servletConnection.setRequestProperty("Content-Type","application/octet-stream");

        ObjectOutputStream oos1 = new ObjectOutputStream(servletConnection.getOutputStream());

        /* DataMap is the HashMap Containing values */

        oos1.writeObject(dataMap);
        oos1.flush();
        oos1.close();

        //  Thread.currentThread().sleep(5000);

    }
    catch(Exception ie)
    {
        ie.printStackTrace();
    }

    // Finally call servlet by going to that page.

    getAppletContext().showDocument(userUrl, "_self");

While on a servlet i just get the HashMap and forward it to a jsp page to display.

    try
    {
        System.out.println("In Servlet");

        ObjectInputStream inputFromApplet = new ObjectInputStream(request.getInputStream());
        HashMap<String,String> receievedData = (HashMap<String,String>) inputFromApplet.readObject();

        request.setAttribute("dataMap",receievedData);
        request.getRequestDispatcher("display1.jsp").forward(request, response);

        inputFromApplet.close();
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }

As asked in the comments in The Question here ,The Sysout("In Servlet") is printed. But an Exception is thrown

In Servlet        
SEVERE: Servlet.service() for servlet jsp threw exception java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream. java:2749)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at org.apache.jsp.display1_jsp._jspService(display1_jsp.java:71)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

What am I doing wrong. Please Help.


Solution

  • If I understand correctly you are :

    1. sending a request to the servlet
    2. reading from e input stream of that call
    3. sending a jsp as the response to THAT call
    4. redirecting the user from the applet to that same servlet
    5. this causes a SECOND request, without anything serialized in it
    6. the servlet fails on this second request

    This unfortunately canno work. You should use two servlets (or the same one with an optional parameter) to handle the two requests, one will read from the input stream, and write to the session, while the second will retrive from the session and display in a jsp.