• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple newbie servlet problem

 
Stanley Beamish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read the body of a HTTP Post request in a servlet, using the servlet request's reader, as follows:

BufferedReader reader = request.getReader();
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}

And have a html page that invokes the servlet as follows:

<form method="post" action="simple_servlet">
Text Field: <input type="text" name="text"> <input type="submit">
</form>

But the body comes back as empty. If I do a getContentLength() on the servlet request, it indicates that there is content. I've also tried using the servlet reader's input stream, but with the same effect.

Can anyone give me some clues as to what stupid thing I'm doing wrong.

Cheers,
SB
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
�'ve never tried working with the Reader directly; can't you use
request.getParameter("text") to access the value of the parameter directly?
 
Stanley Beamish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sadly no. I need to get at the body of a posted http page; the example I gave was of my first steps in doing this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get at the body of a posted http page


If you could explain why, it may help point out an approach.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to recall seeing that if you use the request method getParameter() it causes the stream to be read by the servlet container, so the stream is at the end and no more can be read from it. Do you do anything with the request before trying to get the reader?
Bill
 
Yong Bakos
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. You're outputting to System.out instead of the response object's writer.
 
Stanley Beamish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>I seem to recall seeing that if you use the request method getParameter() >>it causes the stream to be read by the servlet container, so the stream >>is at the end and no more can be read from it. Do you do anything with >>the request before trying to get the reader?
>>Bill

Doh. This was exactly it. The servlet has a debug parameter that I use to control outputting. I'd removed any reference to the request parameters, except this one. Now it works. Thanks for your input.

>> If you could explain why, it may help point out an approach.

I'm working with an application that exchanges data by posting it as the body of an http request. I can't reliably get it back bu querying the parameters.

>>Hi. You're outputting to System.out instead of the response object's writer.

Thanks, but that was intentional. At this stage I was simply trying to print something out to the app server console to check what was coming back.

Anyway, problem solved.

Cheers,
SB
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!