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 ]
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.
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
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>();
InputStream is = request.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buf = new byte;
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, "&");
token = st.nextToken();
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().
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