• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap in HttpServletResponse

 
Meghana Reddy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is there a way to send a java.util.HashMap in the HttpServletResponse?

Thanks

Meghana
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may send any object that is serializable in the response. The client that initiated the HttpServeltRequest would have to be able to handle serialized objects in the returning stream.

Why do you ask?
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The client that initiated the HttpServeltRequest would have to be able to handle serialized objects in the returning stream


If you are using a browser, then "no". If you are using another client that opened up a HttpURLConnection, then you can return an ObjectOutputStream.

Are we limiting the discussion to browser initiated request/response only?
 
Meghana Reddy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael

Thanks for the reply

We have two clients

one which uses Jakarta Commons HttpClient and the other one is the web browser. So we wanted to have the same servlet serve both the clients.

So was just trying to find if there is a way to send a hashmap through the HttpServletResponse.

thanks anyway

Meghana
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A browser wouldn't know what to do with a serialized Java Object.

What are you doing with it in the httpclient client?
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The browser would not know what to do with a serialized object, so in your case, you could not so this.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meghana Reddy:
Is there a way to send a java.util.HashMap in the HttpServletResponse?


There is no method that would set an object in response , like we have in case of HTTPServletRequest's setAttribute , nor it makes sense to set object in response.

What you can do is serialize the Map object to an XML or some other format and then sent it to the client in the response body.

What exactly you want to do ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:


There is no method that would set an object in response , like we have in case of HTTPServletRequest's setAttribute


You can call getOutputStream to get a reference to the ServletOutputStream.
Using that, you can stream any kind of data you like, including serialized Java objects.


nor it makes sense to set object in response.

In this case, (where the original poster is using a browser as a client) you are correct but, if the client is a Java application (Applet or Java Web Start App) passing serialized Java objects over HTTP is common way of tunneling through firewalls.
[ August 15, 2007: Message edited by: Ben Souther ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:

In this case, (where the original poster is using a browser as a client) you are correct but, if the client is a Java application (Applet or Java Web Start App) passing serialized Java objects over HTTP is common way of tunneling through firewalls.


Yes, thats true Ben.

Meghana:one which uses Jakarta Commons HttpClient and the other one is the web browser


AS Meghana says that one of the clients in a web browser , so I said that serialized bytes of HashMap in HTTP response body would not make sense to the browser.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:


AS Meghana says that one of the clients in a web browser , so I said that serialized bytes of HashMap in HTTP response body would not make sense to the browser.



Yep,
Which is why I said:
In this case, (where the original poster is using a browser as a client) you are correct ....



It was aleady established that this would not work for the original poster but I wanted it to be clear that the reason it wouldn't work wasn't due to servlets inability to stream binary data.
Rather it had to do with the clients being used. I also wanted to point out that there are cases where it makes sense to send an object in response.
So I gave an example
[ August 16, 2007: Message edited by: Ben Souther ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ! Thanks Ben.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:

It was aleady established that this would not work for the original poster but I wanted it to be clear that the reason it wouldn't work wasn't due to servlets inability to stream binary data.
Rather it had to do with the clients being used. I also wanted to point out that there are cases where it makes sense to send an object in response.
So I gave an example



Thats really great Ben. As you said, if we are clear with what makes the consequences, we can be pretty sure on what to do in further requirements.

Thank you Ben.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!