aspose file tools*
The moose likes Servlets and the fly likes ServletInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletInputStream " Watch "ServletInputStream " New topic
Author

ServletInputStream

Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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 ?


Rahul Bhattacharjee
LinkedIn - Blog
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
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

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
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


Java Resources at www.wbrogden.com
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Aug 12, 2002
Posts: 102
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

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Mar 22, 2000
Posts: 12678
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletInputStream
 
Similar Threads
Java and Perl using xml-rpc
read Content
HTTP request stream created by HttpURLConnection
Problem with Image and .zip file(Uploading)
Base64 encoded file as POST request not working correctly.