Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write an object in an HttpServletResponse?

497 views
Skip to first unread message

Etienne S.

unread,
Jan 15, 2000, 3:00:00 AM1/15/00
to
Hi guys,
I am writing a servlet which does queries on a database through a JDBC
server.
I want my servlet to send back to the client the ResultSet object result
of my query.
This is the piece of code that i have written... and I thought of using
an ObjectInputStream to read the object on the client side:

System.out.println("callGetItems");
String urlname=request.getParameter("urlname");
java.sql.ResultSet result=server.getItemList(urlname);
try
{
ObjectOutputStream oos=new
ObjectOutputStream(response.getOutputStream()); // This is where the
exception is thrown
oos.writeObject(result);
oos.flush();
}
catch(IOException e)
{
System.out.println("IOException at callGetItems() :
"+e.getMessage());
}

The error message that I get is the following:

java.lang.IllegalStateException: can't mix text and binary input
at
sun.servlet.http.HttpResponse.getOutputStream(HttpResponse.java:436)
at EnominationServlet.callGetItems(EnominationServlet.java:124)
at EnominationServlet.doGet(EnominationServlet.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:499)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)
at
sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.jav
a:165)
at
sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler
.java:121)
at
sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:90)
at java.lang.Thread.run(Thread.java:479)

Thanks for any comments that may help me!!!!


E.T.


Etienne S.

unread,
Jan 15, 2000, 3:00:00 AM1/15/00
to
Hi Nilesh,
Thanks a lot! That's correct! That is what I was doing. I have modified my code.
This is what it looks like now. I am not sure about the content type I have to
specify for the HttpServletResponse.
Right now, the object is written but cannot be read on the client side.
Do you have an example of what I want to do? i.e.: write an object into an
HttpServletResponse and read it back on the client side.
E.T.
***************** Code on the servlet side writing the object

private void callGetItems()
{
response.setContentType("binary");


String urlname=request.getParameter("urlname");
java.sql.ResultSet result=server.getItemList(urlname);
try
{
ObjectOutputStream oos=new ObjectOutputStream(response.getOutputStream());

oos.writeObject(new Vector()); // a Vector cuz ResultSet is not serializable so
will have to write a custom class


oos.flush();
}
catch(IOException e)
{
System.out.println("IOException at callGetItems() : "+e.getMessage());
}
}

************************ Code on the client side reading the object from the socket

InputStream is=socket.getInputStream();

PrintWriter writer=new PrintWriter(out);
writer.print("GET /servlet/EnominationServlet");
writer.print("?type=getitems&urlname=www.bmw.com");
writer.print(" HTTP/1.0\r\n\r\n");
writer.flush(); // This sends the request now!!!

try
{
ObjectInputStream ois=new ObjectInputStream(is); // This is where the exception
is thrown
System.out.println("MainTest.main about to read object");
Vector v=(Vector)ois.readObject();
System.out.println("MainTest.main object read");
ois.close();
}
catch(Exception e)
{
System.out.println("exception at main - "+e.getMessage());
}

The error message is:
exception at main - InputStream does not contain a serialized object

Nilesh Parab wrote:

> Are you calling response.getWriter() somewhere before calling
> response.getOutputStream(). Doing that will give you the IllegalStateException.
>
> - Nilesh

Nilesh Parab

unread,
Jan 16, 2000, 3:00:00 AM1/16/00
to
0 new messages