This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is request.getInputStream() useful for?  RSS feed

 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I have a lot of code to show for what is probably a simple question but... the displayHeaderWithIO method in the code below results in no output (or just the opening and closing html tags that are hard coded actually) while the displayHeader method at the end correctly displays most of the header (i am still unhappy because it does not display "GET / HTTP/1.1" stuff that is supposed to start every get request.




my question is why doesn't the first approach work and is there a way to read in the full http request properly with a servlet/jsp? also, what is request.getInputStream() useful for anyway?
[ July 06, 2007: Message edited by: ra carter ]
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getInputStream() is used to get the body of the request. Thus, you will only get the body of the request, not the whole thing.

the request.getHeaders() is used to get the HTTP headers only. If you want to get the HTTP method, you can use request.getMethod(). If you want to get the protocol, you can use request.getProtocol(). The HttpServletRequest does all of these for you.

I don't think you can get a raw HTTP header using Servlet. Probably you need to use Socket.
 
Jay Rajani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya,

I dont know whether you got the answer or not. But i faced the same problem and was trying many things to make it work.

I succeeded in doing it. Dont know whether it is the RIGHT way, but i can share it anyway

I had to read the raw request so as to maintain the sequence of parameters. I wanted the same sequence in which browser sends to JBoss instead of HashMap generated order (which is basically the hash order)

I wrote following code and resolved my problem (after trying so many options )

LinkedHashMap<String, String> paramLinkedMap = new LinkedHashMap<String, String>();

log.debug("b4");
try{
String data=null;

InputStream is = request.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte buf[] = new byte[1024];
int letti;

while ((letti = is.read(buf)) > 0)
baos.write(buf, 0, letti);

data = new String(baos.toByteArray());

log.debug("Data : " + data);

StringTokenizer st = new StringTokenizer(data, "&");
String token;
while(st.hasMoreTokens()){
token = st.nextToken();
paramLinkedMap.put(URLDecoder.decode(token.substring(0, token.indexOf("="))),
URLDecoder.decode(token.substring(token.indexOf("=")+1, token.length())));
}

}catch(Exception e){

}
log.debug("after");

Believe me i tried BufferRead, readline, char array but all in vain. Dont have enough reasons or logics to explain but this works fine. Tested

Moreover for those who are novice in using InputStream, if you use getParameter first, you will not get inputstream by calling getInputStream or a buffer reader by getReader().

Cheers,
Jay Rajani
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!