Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Retrieving all Parameters to a Java Servlet (See related posts)

//
// Ref: http://forum.java.sun.com/thread.jspa?threadID=405328&messageID=2184836
//
// The getParameterMap() returns a Map that has Strings as keys and String[] as
// values. That's so you can have ?x=1&x=2&x=3 in your URL query string. The
// keys in the Map must be unique but there can be multiple values for each
// key.
//
// So.. when you pull a value out of a Map created by the getParameterMap()
// method you must cast it to a String[] or else you'll get the value's
// location in memory instead of it's actual value. If you're writing the
// code and know for a fact that you'll always have only one value for each
// param then you can just use yourVar[0] but if there may be multiple
// values for each key then you'll need to loop over each value array.
//

   1  
   2      Map params = request.getParameterMap();
   3      Iterator i = params.keySet().iterator();
   4      
   5      while ( i.hasNext() )
   6        {
   7          String key = (String) i.next();
   8          String value = ((String[]) params.get( key ))[ 0 ];
   9        }

You need to create an account or log in to post comments to this site.


Click here to browse all 5355 code snippets

Related Posts