aspose file tools*
The moose likes Servlets and the fly likes deviation in the return type of getParameterValues() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "deviation in the return type of getParameterValues() method" Watch "deviation in the return type of getParameterValues() method" New topic
Author

deviation in the return type of getParameterValues() method

John Mallavalli
Ranch Hand

Joined: Apr 24, 2002
Posts: 46
Why did the authors have a String array as the return type for the getParameterValues() method of ServletRequest interface but for all the other methods which return multiple values such as getHeaderNames() of HttpServletRequest interface, getAttributeNames() method of ServletContext interface, getInitParameterNames() method of ServletConfig interface etc., have java.util.Enumeration as the return type ?


Do not let what you cannot do interfere with what you can do !!<br /> <br />SCJP & SCWCD 1.4
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
I think that getParameterValues() returns only values, not the pairs like in the other methods.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
I think that getParameterValues() returns only values, not the pairs like in the other methods.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 635
What pair ???
All of them return return 0 or more values.


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
John,

Welcome back to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We request that display names be of the pattern FIRST_NAME+SPACE+LAST_NAME.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Pairs [key;value] so they are stored in some kind of collection, and maybe they are returned via Hashtable.elements . And you can see there is a method getParameterNames() which return Enumeration and the method getParameterValues(String name) which return an array of String. Parameter names are unique and could be iterated but one parameter could have more than one value. [key;value[]]
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 635
Hi David,
Following is the output of request.getHeaderNames() on my meachine

accept
accept-language
accept-encoding
user-agent
host
connection
cookie

it returns only names (value) of header elements,i.e all these method returns 0 or move value and not [key;value] to get value of this name we have a method call getHeader(String name).

This can be achived by returning 1D String array,instead of Enumeration,so question is why the difference in return type?

cheers
Praful
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Header names are something constant :-). But parameters passed to the servlet may vary. Sometimes could one parameter take more values (array)
and the function must return this array.
It could look like this.

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 635
David,
I agree to your statement,Header names are something constant :-). But parameters passed to the servlet may vary. Sometimes could one parameter take more values (array)

But same thing is achivable by returning enumeration from method getParameterValues, as follows.
If I have to develop such methods then I would preffer to keep the return type of all methods same either by applying this logic to getParameterValues
or your logic to methods like getHeaderNames,getParameterNames etc.

(anyone kinldy correct me if I'm going wrong)

So the question still remains,why Sun choose to have different Return type for getParameterValues







Cheers
-P

[ August 26, 2004: Message edited by: Praful Thakare ]
[ August 26, 2004: Message edited by: Praful Thakare ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12786
    
    5
Why? - How about because it is simpler and faster! Particularly in the very common case of only a single value associated with a parameter name.

I'll never understand why people start arguments like this.
Bill
John Mallavalli
Ranch Hand

Joined: Apr 24, 2002
Posts: 46
I have asked this question since I haven't found any other method of similar kind returning String array. And if the reason is being simpler, why didnt the authors use it for other similar methods instead of using Enumeration.

For example, the getHeaders(String name) method of the HttpServletRequest is somewhat similar to the getParameterValues(String name) method. So why didnt the authors use String[] as the return type for the getHeaders() method if it was simple.

I had not asked this question for argument sake but I really would like to know the reason behind making that decision.
Sorry if I sound crazy(sometimes I do though!!)

Thanks,
John
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 635
Why? - How about because it is simpler and faster! Particularly in the very common case of only a single value associated with a parameter name

In that case,getHeaderNames(),getHeaderNames(),getInitParameterNames() must return String array,as They come under comman case of Only a single value associated with parameterand they return values which can be embraced by 1D String array.

I'll never understand why people start arguments like this.


May be cause people are trying to find the answer of somthing which they find either strange,or weard,or they think there is some reason which is out of there thinking reach

Cheers
-P
[ August 26, 2004: Message edited by: Praful Thakare ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12786
    
    5
Look up the source code for the implementation of HttpRequest in Tomcat.
org.apache.catalina.core.ApplicationHttpRequest.java
You will see there that the request parameters have been parsed by the servlet engine into a HashMap having the parameter names as keys and String[] as the stored value.
Therefore the String[] can be returned with no further manipulation in the getParameterValues( String name ) method.
Thats one of the great things about using Tomcat - you can get the source.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: deviation in the return type of getParameterValues() method