This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpServletRequest.getReader() returns nothing  RSS feed

 
Swati Bhatiaa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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();

 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes, Servlet lib version is 2.3.
I cannot change the tomcat version.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!