GeeCON Prague 2014*
The moose likes Servlets and the fly likes Servlet CANNOT Receive Serialized Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet CANNOT Receive Serialized Object" Watch "Servlet CANNOT Receive Serialized Object" New topic
Author

Servlet CANNOT Receive Serialized Object

kit chan
Greenhorn

Joined: Mar 13, 2004
Posts: 1
Hi all experts,
I encounter a problem of servlet receiving a serialized object. It throws a EOFException exception. I dunno why and can You tell me how to solve it.
Code
=======================
Servlet
=======================
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
/*
* Only Do Send and Receive -- connect to client
*/
public class CPServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException
{
System.out.println("*******************INIT");
super.init(config);
}
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
CPManager mgr = new CPManager();
System.out.println("doPost method");
System.out.println(request.getMethod());
response.setContentType("application/octet-stream");

ObjectInputStream oin = new ObjectInputStream(request.getInputStream());
ObjectOutputStream oout = new ObjectOutputStream(response.getOutputStream());

System.out.println("**** Start to send content **** ");

// Message is a serialized object
Message msg = mgr.encryptNWrapKey(sth);
sendContent(msg,oout);
oout.close();
System.out.println("**** Finish sending First Message");
System.out.println("**** Start to receive content **** ");
Message msg2 = receiveContent(oin);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("doGetMethod");
doGet(request, response);
}
public void sendContent(Message msg, ObjectOutputStream oout)
{
MyUtil.sendObjectThrSocket(oout,msg);
}

public Message receiveContent(ObjectInputStream oin)
{
Message msg = (Message)MyUtil.getObjectThrSocket(oin);
return msg;
}
}

==============
Client
==============
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
/*
* Only Do Send and Receive -- connect to server
*/
public class CustomerClient
{
ObjectInputStream oin = null;
ObjectOutputStream oout = null;
URLConnection conn = null;
// connect to server -- Using URL and URLConnection
public void connect(String servletName)
{
try
{
URL url = new URL("http://localhost:8080/cart/servlet/"+servletName);
conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

// don't use a cached version of URL Connection
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty
("Content_Type", "application/octet-stream");
conn.connect();
oout = new ObjectOutputStream(conn.getOutputStream());
oin = new ObjectInputStream(conn.getInputStream());

System.out.println("connected to "+servletName);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
public Message receiveContent()
{
Message msg = (Message)MyUtil.getObjectThrSocket(oin);
return msg;
}
public void close()
{
try
{
oout.close();
oin.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}

public void sendContent(Message msg)
{
MyUtil.sendObjectThrSocket(oout, msg);
}
public static void main(String[] args)
{
CustomerManager mgr = new CustomerManager();
CustomerClient client = new CustomerClient();

client.connect("CPServlet");

Message CPEncrypted = client.receiveContent();
Message customerEncrypted = mgr.firstStep(CPEncrypted);
client.sendContent(customerEncrypted);
client.close();

}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet CANNOT Receive Serialized Object