写入和读取 servlet 时出现 java.io.EOFException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2666040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.EOFException while writing and reading froma servlet
提问by mithun1538
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:
在 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();
}
}
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.accessObjectOutputStream 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
0(RequestRoomForm.java:7)
at com.org.RequestRoomForm.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?
为什么我收到这个错误?我在输出时已刷新,我也关闭了连接,但出现错误。这有什么原因吗?
回答by Arun VC
Close the ObjectOutputStream at the end.
最后关闭 ObjectOutputStream。
##代码##回答by user6582737
first request.getParameter()
then request.getInputStream()
先request.getParameter()
然后request.getInputStream()
this case must course exception on tomcat ,but not course exception on weblogic
这种情况必须在 tomcat 上当然例外,但在 weblogic 上当然不是例外