• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpServletRequest.getReader() returns nothing

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing a HTTP POST on a servlet running on my local box. For this, I am opening a socket on port 80 and sending the data.
When I call getReader() on HttpServletRequest object, I cannot find any data. Instead of Calling getReader(), if I call getParameter() or getParameterMap() on HttpServletRequest object, I can retrieve the data which has been posted

This works well on Linux 4 with apache version 2.0 and tomcat version 4.X i.e. getReader returns data.
The issue is seen on Linux 5 with apache version 2.2.3 and same tomcat version 4.X.

Any clues as to what is going wrong?

Using tcpdump, I have seen the request - It looks as expected.
----------------------------------------------------------------------------------------------
POST /servlet/Help HTTP/1.0
Content-Length: 17
Content-Type: application/x-www-form-urlencoded

Want to send this
----------------------------------------------------------------------------------------------

SERVER CODE:
--------------------
getReader() on HttpServletRequest object returns me BufferedReader object.
If I call read() on buffer, I get -1. If I call readLine() on buffer, I get NULL.

This is how I am calling in my servlet:
java.io.BufferedReader _buffer = req.getReader();
System.out.println("****Is Buffer object ready " + _buffer.ready());
System.out.println("****Is Buffer object char: " + _buffer.read());
System.out.println("****Is Buffer object line: " + _buffer.readLine());

Instead of Calling getReader(), if I call getParameter() or getParameterMap() on HttpServletRequest object, I can retrieve the data which has been posted.



CLIENT CODE:
-----------------
// Construct data
String data = "Sending simple data";

// Create a socket to the host
String hostname = "127.0.0.1";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);

// Send header
String path = "/servlet/Help";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("POST "+path+" HTTP/1.0\r\n");
wr.write("Content-Length: "+data.length()+"\r\n");
wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
wr.write("\r\n");

// Send data
wr.write(data);
wr.flush();

// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
System.out.println(line);
}
wr.close();
rd.close();

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using such outdated version of Tomcat? Be sure to be using Servlet 2.3 library.
 
Swati Bhatiaa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Servlet lib version is 2.3.
I cannot change the tomcat version.
 
Hold that thought. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic