我正在尝试从 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)