Using getParameterMap with Tomcat

S

Scott Harper

I have a servlet with a doPost method that grabs the set of posted parameters
with the HttpServletRequest.getParameterMap() method. I want to go through
the list of keys looking for some specific (required) parameters. Once I
process these parameters, I'd like to remove them from the map, leaving a set
of "optional" or unrecognized parameters. Ultimately I will grab them and log
them off as extra data.

However, it appears that because I happen to be running under Tomcat (4.1, if
that really matters), the getParameterMap method actually returns a
org.apache.catalina.utils.ParameterMap object. They have subclassed HashMap
and added some locking capabilities. So I can't use the Map.remove() method
unless I call their setLocked(true) method first.

I'd rather not have my implementation tied specifically to the servlet
container. I've tried copying the ParameterMap object to a more general
purpose map type (HashMap) like:

Map parameterMap = request.getParameterMap();
HashMap h = (HashMap) parameterMap;

But since ParameterMap inherits from HashMap, I suspect I am running into some
"you can't cast up" issues, and I still see the same behavior if I try to call
h.remove().

Does anyone have any suggestions on how to work around this issue?


thanks
scott
 
S

Scott Harper

I've tried copying the ParameterMap object to a more general
purpose map type (HashMap) like:

Map parameterMap = request.getParameterMap();
HashMap h = (HashMap) parameterMap;

Update... I revisited the copying idea, and it seems to work. The first
thing I tried was using Map.putAll() like:

Map parameterMap = request.getParameterMap();
HashMap h = new HashMap();
parameterMap.putAll(h);

but apparently the Apache ParameterMap class needs the locking business
handled for putAll too.

So I basically just iterated through all the keys and copied the values one at
a time. Wonder if anyone would have any comments on the relative efficiency
of the following two methods:

HashMap h = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements())
{
String key = (String) names.nextElement();
String[] value = req.getParameterValues(key);
h.put(key, value);
}

vs.

HashMap h = new HashMap();
Map parameterMap = request.getParameterMap();
Set keySet = parameterMap.keySet();
Iterator i = keySet.iterator();
while (i.hasNext())
{
String key = (String) i.next();
Object value = parameterMap.get(key);
h.put(key, value);
}

They are basically the same, but I wonder if calling getParameterMap() and
then keySet() might result in double the processing of just calling
getParameterNames().


scott
 
C

Chris Uppal

Scott said:
Update... I revisited the copying idea, and it seems to work. The first
thing I tried was using Map.putAll() like:

Map parameterMap = request.getParameterMap();
HashMap h = new HashMap();
parameterMap.putAll(h);

That should be
h.putAll(parameterMap);
if you want to copy the parameter map into h.

Another way to get an unlocked copy would be (untested):
ParameterMap copy = parameterMap.clone();
copy.setLocked(false);

a time. Wonder if anyone would have any comments on the relative
efficiency of the following two methods: [...]
They are basically the same, but I wonder if calling getParameterMap() and
then keySet() might result in double the processing of just calling
getParameterNames().

I doubt if it makes any difference. I expect that getParameterNames()
ultimately reduces to a call to keySet() on the same underlying collection
object as is returned by getParameterMap().

-- chris
 
S

Scott Harper

That should be
h.putAll(parameterMap);
if you want to copy the parameter map into h.

Ah, I see... so would you expect this to be any more efficient than copying
all the key/values individually as below?
Another way to get an unlocked copy would be (untested):
ParameterMap copy = parameterMap.clone();
copy.setLocked(false);

Well, the problem with this is that the Apache object is not in the classpath
(by default). So to use ParameterMap directly would require some
customization on the Tomcat setup (I presume). Otherwise I would just use
the original copy returned by getParameterMap.

And besides, I'd prefer not to have my implementation tied specifically to
Tomcat.
a time. Wonder if anyone would have any comments on the relative
efficiency of the following two methods: [...]
They are basically the same, but I wonder if calling getParameterMap() and
then keySet() might result in double the processing of just calling
getParameterNames().

I doubt if it makes any difference. I expect that getParameterNames()
ultimately reduces to a call to keySet() on the same underlying collection
object as is returned by getParameterMap().

That's what I figured, but I wasn't sure if was missing something obvious.


scott
 
C

Chris Uppal

Scott said:
Ah, I see... so would you expect this to be any more efficient than
copying all the key/values individually as below?

Take a look at the source -- it's almost exactly the same code ;-)

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top