-1

Using Tomcat. I want to send a String to the server, have the server manipulate the String and then send it back. The app crashes whenever I try to open an InputStream on the client after I have written to the Output Stream.

Server:

public void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws IOException, ServletException{
    try{

        ServletInputStream is = request.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);

        String s = (String)ois.readObject();

        is.close();
        ois.close();

        ServletOutputStream os = response.getOutputStream(); 
        ObjectOutputStream oos = new ObjectOutputStream(os); 

        oos.writeObject("return: "+s);
        oos.flush();

        os.close();
        oos.close();

    }
    catch(Exception e){

    }
}

Client:

URLConnection c = new URL("*****************").openConnection();
c.setDoInput(true);
c.setDoOutput(true);
c.connect();

OutputStream os = c.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);

oos.writeObject("This is the send");
oos.flush();
oos.close();

InputStream is = c.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
System.out.println("ret: "+ois.readObject());

ois.close();
is.close();
os.close();

It returns this error:

java.io.IOException: Server returned HTTP response code: 405 for URL: 
http://mywebpage.com

What is causing this error, what am I doing wrong?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
CptRayMar
  • 133
  • 4
  • 19

1 Answers1

0

You're using HTTP servlets; Tomcat is running as a Web server. You have to speak HTTP from the client, not serialized Java objects. If you are wanting to use Java serialization, you need to use sockets. If you want to use servlets, you need to use some form of putting your information into HTTP; JSON (with Jackson) is a good choice.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152