从 servlet 写入和读取时出现 java.io.EOFException

发布于 2024-08-29 17:34:12 字数 4293 浏览 4 评论 0原文

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

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)

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

I have the following code on the applet side:

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

In the servlet side I recieve the code as follows:

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

When I click on the button that calls the code in the applet side, I get the following error:

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)

Why am I getting this error? I have flushed when I output, I have closed the connections also, yet I get the error. Any reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-09-05 17:34:13

最后关闭 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

Close the ObjectOutputStream at the end.

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
时光清浅 2024-09-05 17:34:13

首先 request.getParameter()
那么 request.getInputStream()

这种情况在 tomcat 上肯定会出现异常,但在 weblogic 上不会出现异常

first request.getParameter()
then request.getInputStream()

this case must course exception on tomcat ,but not course exception on weblogic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文