我正在尝试从java程序向servlet发送一个字符串,并使用一些填充来检索相同的字符串。
这是我正在处理的代码,问题是java.io.EOFException
在java程序中建立输入流时被抛出。
为什么在我设置它时会出现流的结束。请澄清我的疑问。
Servlet程序
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProgServlet implements Servlet {
public void init(ServletConfig sc){
}
public void service (ServletRequest req,ServletResponse res)
throws ServletException, java.io.IOException
{
}
public void destroy(){
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
//Setting up streams
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
String p = "Server String";
//Receiving data from client and resends by padding
try {
p = (String) ois.readObject();
p = p.concat(" -sever padding");
oos.writeObject(p);
oos.flush();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
}
}
客户端上的java程序是
public static void main(String arg[]) throws IOException{
System.out.println("Enter a string :");
Scanner s = new Scanner(System.in);
String data = s.next();
s.close();
URL serv = null;
try {
serv = new URL("http://10.0.0.9:8080/project/projectservlet");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection servletConnection = null;
System.out.println("establishing communication with server....");
try {
servletConnection = (HttpURLConnection) serv.openConnection();
System.out.println("connection with the server established"+servletConnection.getRequestMethod());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
servletConnection.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(""+servletConnection.getRequestMethod());
servletConnection.setDoOutput(true);
servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
System.out.println("setting up streams to communicate...");
ObjectOutputStream dos = null;
ObjectInputStream dis = null;
try {
dos = new ObjectOutputStream(servletConnection.getOutputStream());
System.out.println("Streams are up and ready to go");
System.out.println("Sending data to the server...");
dos.flush();
dos.writeObject(data);
dos.flush();
System.out.println("Data sent successfullyy \n Retrieving data from server");
dis = new ObjectInputStream(servletConnection.getInputStream());
data = (String) dis.readObject();
System.out.println("Data retrieved from the server is "+data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
dos.close();
dis.close();
servletConnection.disconnect();
}
}
输出和堆栈跟踪是
Enter a string :
jaggu
establishing communication with server....
connection with the server established
GET
POST
setting up streams to communicate...
Streams are up and ready to go
Sending data to the server...
Data sent successfully
Retrieving data from server
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 ServletInvokation.main(ServletInvokation.java:57)
Exception in thread "main" java.lang.NullPointerException
at ServletInvokation.main(ServletInvokation.java:69)
答案 0 :(得分:1)
在java客户端中,输出流表示发送到服务器的请求,输入流表示从服务器检索的响应。当您调用servletConnection.getInputStream()
时,您正在请求来自服务器的响应,因此它会立即将HTTP请求发送到服务器。但是如果你查看你的代码,那时你还没有向输出流写任何内容,所以你实际上是在尝试发送一个空请求,这就是你获得EOFException
的原因。
尝试分两步完成。首先,获取输出流,写入并关闭它。然后获取输入流并从中读取。
另见this answer。
答案 1 :(得分:0)
在servlet工作完成后关闭servlet中的OutPutStream。