Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletInputStream

 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sending a request like the following to a servlet.

POST /cool/test HTTP/1.1
User-Agent: Java/1.4.2_04
Host: 127.0.0.1:8089
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 28

bXkgbmFtZSBpcyByYWh1bCAxMjM

What does request.getInputStream() return ?
Can I not get a inputStream of the body of the request using this?
I tried but got a inputstream with in.available() == 0
why is this so ?
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, available() method of java.io.InputStream always returns zero. Not sure, whether implementing subclass of this class now overrides that and returns valid data.

Long back (probably in Tomcat 3 or 4) we tried that and failed because of the same reason. As far as I remember, available() method never returned outside 0.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So can by any chance I not get a inputstream of the body of the request ?
By reading the javadoc for getInputStream method in the HTTPServeltRequest I think that this should give me the inputstream of the body of the request.But not happening in this way.I suspect a bug or am i doing some mistake.

But if I pass the same date as parameter , then i am able to get that using request.getParameter()..why is that so ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not call both getInputStream and getParameter from the same request.

The container will parse the request once.
As soon as you call getParameter, the input stream gets parsed and it's no longer available. Likewise, once you call getInputStream, it is no longer possible to get parameters with getParameter.




[KLUNK]
[ September 20, 2006: Message edited by: Ben Souther ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I not get a inputStream of the body of the request using this?

People do this all the time. How exactly are you trying to do it?

NOTE: If you do any getParameter calls at all, anywhere in the request process before you try to get the stream, the request stream will have been read by the servlet engine and will not be usable.
Bill
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not trying to use getParameter and getInputStream together.When my code contained only getInputStream then I get a stream whith no data/bytes in that.(Using tcpmon I have verified that client is sending the data in the body of the request properly.)

But when I send the date from client as a POST request and data as parameter ,I am able to get that in servlet using getParameter.
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,
As per Servlet API (do not remember starting from when), getParameter of HttpServletRequest works for both query string parameters (for GET) and request body data (for POST).
But, getInputStream() works only for request body data (i.e. for POST).

Look at those methods at the Sun J2EE 1.4 API.

-Susanta
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using POST.Moreover this method of for getting the inputstream of the body of the request and GET doesnot have a body.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your sevlet get the POST request directly or is there some filter or handler that sees it first?
Perhaps at this point you should post the actual code - please use the UBB code formatting.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic