【问题标题】:java.io.EOFException while writing and reading froma servletjava.io.EOFException 写入和读取 servlet
【发布时间】:2010-04-19 08:12:39
【问题描述】:

我在小程序端有以下代码:

URL servlet = new URL(appletCodeBase, "FormsServlet?form=requestRoom");
URLConnection con = servlet.openConnection();

con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/octet-stream");

ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
out.writeObject(user);//user is an object of a serializable class
out.flush();
out.close();

ObjectInputStream in = new ObjectInputStream(con.getInputStream());
status = (String)in.readObject();
in.close();
if("success".equals("status")) {
    JOptionPane.showMessageDialog(rootPane, "Request submitted successfully.");
} else {
    JOptionPane.showMessageDialog(rootPane, "ERROR! Request cannot be made at this 
    time");
}

在 servlet 端,我收到如下代码:

    form = request.getParameter("form");
    if("requestRoom".equals(form)) {
        String fullName, eID, reason;
        UserRequestingRoom user;

        try {
            in = new ObjectInputStream(request.getInputStream());
            user = (UserRequestingRoom)in.readObject();
            fullName = user.getFullName();
            eID = user.getEID();
            reason = user.getReason();

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chat_applet","root","");
            PreparedStatement statement = con.prepareStatement("INSERT INTO REQCONFROOM VALUES(\"" + fullName + "\",\"" + eID + "\",\"" + reason + "\")");
            statement.execute();

            out = new ObjectOutputStream(response.getOutputStream());
            out.writeObject("success");
            out.flush();

        } catch (Exception e)  {
            e.printStackTrace();
            out = new ObjectOutputStream(response.getOutputStream());
            out.writeObject("fail");
            out.flush();
        }
    }

当我点击小程序端调用代码的按钮时,出现以下错误:

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at com.org.RequestRoomForm.requestActionPerformed(RequestRoomForm.java:151)

    **//Line 151 is "ObjectInputStream in..." line in the applet code**

    at com.org.RequestRoomForm.access$000(RequestRoomForm.java:7)
    at com.org.RequestRoomForm$1.actionPerformed(RequestRoomForm.java:62)
    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.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(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)

为什么会出现此错误?输出时我已经刷新,我也关闭了连接,但我得到了错误。这有什么原因吗?

【问题讨论】:

  • 您是否调试过您的 servlet(或至少进行了一些日志输出)以确保您确实到达了 servlet 并输入了 if 块?从客户端异常来看,似乎 servlet(或其他一些服务器组件)没有向响应流写入任何内容,因此客户端由于响应为空而立即失败。
  • 是的。它没有显示执行到达 servlet 的任何迹象。无论如何,我不得不拆分我的可序列化对象并通过 URl 本身(通过 &)发送它。结果太粗略了——一个很长的 URL。但它最终完成了这项工作。

标签: java objectoutputstream objectinputstream eofexception


【解决方案1】:

最后关闭 ObjectOutputStream。

ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
out.writeObject(user);//user is an object of a serializable class
out.flush();
out.close(); //Don,t close your out here

ObjectInputStream in = new ObjectInputStream(con.getInputStream());
status = (String)in.readObject();
in.close();

out.close(); //Close your out after reading the input

【讨论】:

    【解决方案2】:

    第一个request.getParameter()
    然后request.getInputStream()

    这种情况必须在tomcat上当然例外,但在weblogic上不是当然例外

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      相关资源
      最近更新 更多