2011-08-08 58 views
1

我試圖在Applet中獲取簡單的用戶詳細信息(名稱,電話號碼,性別(選項框)),並在JSP中顯示詳細信息。我將所有三個詳細信息一個HashMap並將其發送到輸出流中。 Applet代碼如下。從Applet發送數據到servlet時的EOFException

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"); 

雖然上一個servlet我剛剛拿到的HashMap並將其轉發給一個JSP頁面來顯示。

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(); 
    } 

作爲問評價在問題here所述的SYSOUT(「在Servlet的」)被打印。但拋出異常

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) 

我在做什麼錯。請幫忙。

回答

0

如果我理解正確的話,你是:

  1. 發送到servlet
  2. 從呼叫
  3. 發送JSP爲響應這一號召
  4. 轉向電子輸入流中讀取的請求用戶從小程序到相同的servlet
  5. 這會導致第二個請求,其中沒有任何序列化的內容
  6. 該serv讓這個第二個請求失敗

這不幸的是無法工作。您應該使用兩個servlet(或具有可選參數的同一個Servlet)來處理這兩個請求,一個會從輸入流中讀取數據,然後寫入會話,而另一個會從會話中恢復並顯示在jsp中。

+0

謝謝SimoneGianni。是的,這是接縫問題。但我不清楚如何使用兩個servlet(或使用一個servlet和可選的參數)。如果你能詳細說明,這將是非常有用的。 – anthoon

0

我懷疑小程序正在拋出一個異常,但您尚未在Java控制檯中檢測到它。

+0

我也檢查了Java控制檯,並且沒有Applet不會拋出任何異常。 – anthoon

相關問題