This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes calling getInputStream after getParameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling getInputStream after getParameter" Watch "calling getInputStream after getParameter" New topic
Author

calling getInputStream after getParameter

Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Hi All,

I have a requirment where I need to do something like this



The problem is that the inputstream get reset to null after calling the getParameter

the getParameter values are used at 3 places, only at one place I am required to build the value from the inputstream

Any pointers on how to solve it arre welcome. So far I can think of writing getParameter like funtinality myself but any other non complicated ways will be very helpfull

PS: Can we assume that the & symbol will always mean the start of a new parameter

Thanks in advance,
Raj
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Since getParameter triggers the reading of the input stream to the end, there is no way you can do both. You may have to get the input stream and do the parsing yourself. The information missing from your post:
1. Is the request a GET or a POST?
2. Where does the request come from? An HTML form? Javascript? whatever?

Bill


Java Resources at www.wbrogden.com
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Thanks for the reply

Its a POST request

The request comes from multiple locations

1. Java Batch programs
2. JSP application (Both HTML forms and javascript)
3. 3rd part applications

Basically I can make no assumptions about the front end neither can I change them.

Any pointers on how to parse the inputstream would be very helpfull

is it just key1=val1&key2=val2&key3=val3 and so on or are there some pittfalls I should avoid

Thanks again,
Raj
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
looks like HttpUtils class will do what I was looking for. But its deprecated any idea why and alternatives if any will be helpfull

Thanks in advance,
Raj
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
How about using

public java.util.Map getParameterMap()

to store your parameters in an immutable HashMap and subsequently build it from the map as opposed to InputStream.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
the thing is there might be a scenario where there are no parameters but I still want to read the inputstream

do I make sense or am I complicating a simple solution
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Why do you need the input stream in the first place?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
actually the inputstream contains a xml document. The document some times comes as one of the parameters "xmlDoc" or the inputstream contains only the xml document.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

So this is a file upload?
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
I think no, The xml may be generated by a batch program, other servlets or even javascript. Actually I am not confident on how when where the request is generated and have no control over it. For all you know it could be a file upload.

ps: some times the xmlDoc is just one of the request parameters, other times it is the post body
[ March 02, 2006: Message edited by: Rajagopal Manohar ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Since you can't change the way requests are created, it looks to me like you will have to grab the contents of the input stream to a byte[], grab all the header information to some sort of Map, and then decide what to do with it.
I say byte[] because that leaves you the flexibility to handle data as binary or as characters.
If there was something in the request headers that could let you decide how to treat the body of the POST you could use a Filter to look at the headers and direct the request to the appropriate servlet.
Bill
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Originally posted by Rajagopal Manohar:
looks like HttpUtils class will do what I was looking for. But its deprecated any idea why and alternatives if any will be helpfull

Thanks in advance,
Raj


Thanks for the replys,

I am using this class as it seems to do what I want read a string and return a hashtable of parameters or throw a exception. Is there any thing that can go wrong with this approach?

-Raj
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by William Brogden:

If there was something in the request headers that could let you decide how to treat the body of the POST you could use a Filter to look at the headers and direct the request to the appropriate servlet.
Bill


Yes, if the clients are at all well written then they will adhere to the HTTP spec and send you a "Content-Type" request header.
If it's application/x-www-form-urlencoded then you should look for the data in a request parameter.
If it's text/xml then you would know to parse the body of the message as XML.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling getInputStream after getParameter
 
Similar Threads
Where can I find 'POST' Request String?
How can i get all the existing processes in a system?
Java Runtime command syntax help
InputStream -> OutputStream
what is request.getInputStream() useful for?