Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Using getParameterMap with Tomcat

Thread view: 
Scott Harper - 01 Mar 2007 23:18 GMT
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
Scott Harper - 02 Mar 2007 17:00 GMT
>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
Chris Uppal - 02 Mar 2007 18:07 GMT
> 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
Scott Harper - 02 Mar 2007 19:03 GMT
>> Update...  I revisited the copying idea, and it seems to work.  The first
>> thing I tried was using Map.putAll() like:
[quoted text clipped - 6 lines]
>    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: [...]
[quoted text clipped - 5 lines]
>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
Chris Uppal - 02 Mar 2007 21:56 GMT
> > >    Map parameterMap = request.getParameterMap();
> > >    HashMap h = new HashMap();
[quoted text clipped - 6 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.