2

I'm trying to send a string to the servlet from java program and retrieve the same string with some padding. Here's the code i'm working on and the problem is java.io.EOFException is being thrown at the establishment of inputstream in java program.
why does the end of stream is occuring when i'm setting it up. please clarify my doubt.

Servlet program is

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 program on client is

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

the output and stack trace is

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)

jagadeesh
  • 91
  • 1
  • 9

2 Answers2

1

In the java client, the output stream represents the request sent to the server, and the input stream represents the response retrieved from the server. When you call servletConnection.getInputStream(), you are requesting the response from the server, so it immediately sends the HTTP request to the server. But if you look at your code, at that point you have not yet written anything to the output stream, so you are actually trying to send an empty request, and that's why you are getting an EOFException.

Try doing it in two steps instead. First, get the output stream, write to it and close it. Then get the input stream and read from it.

See also this answer.

Community
  • 1
  • 1
David Levesque
  • 22,181
  • 8
  • 67
  • 82
0

Close OutPutStream in servlet after servlet work is completed.

Htaras
  • 849
  • 1
  • 7
  • 17