• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarrification about HttpServletRequest

 
Thennam Pandian
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

1. what is the reason to add InputStream as one of the property to HttpServletRequest? give me a practical example?

2. what is the use of that InputStream Object?

thanks in advance...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand.
 
Thennam Pandian
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

we can get the InputStream Object from HttpServletRequest using

getInputStream() method. that means HttpServletRequest interface

implementation class should have one data memeber of type InputStream.


Now My Question is : " what is need to add the InputStream member

to the HttpServletRequest interface implementing class"?

or

what caused the sun to add add the InputStream member

to the HttpServletRequest interface implementing class?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the InputStream is simply used for the purpose described in the API doc.

Marco
 
Thennam Pandian
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The request body will have only the request parameter.

We can get the all the request parameter using getParameter and

getParameterValues as a String Object.

what is the advantage of reading the request body as a byte over String Object.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Retrieves the body of the request as binary data...

As the documentation says the method retrieves all the request data at once as "binary data" in contrast to one string per parameter. This may or may not be an advantage depending on what you want to do. In any case it's an advantage if you have for example image data in a POST request uploaded by a user and want to read this definitely binary data.

And don't forget that this method is inherited from ServletRequest. So even though almost all servlets in practice will be based on the HTTP protocol it's absolutely possible to create servlets which are using a complete different communication mechanism where it would be more useful to get any binary request data from such an input stream.

Marco
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I have caught up now.

As Marco says, the method comes from ServletRequest and not HttpServletRequest and so may be used by protocols other than HTTP. Having said that, there are times when the data sent in the HttpServletRequest may not be character data, and would need to be read as a binary stream.

The default HTTP encoding is able to parse the request and inflate the data into parameters, but if the request uses an alternate encoding, like a multipart form submission (like a file upload), your servlet will get nothing useful unless you read the InputStream and parse the data yourself, or plug in another API to do it for you.
 
Thennam Pandian
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco and David , i got it now. thanks a lot...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic