Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

what is request.getInputStream() useful for?

 
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 ]
 
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.
 
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
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic