aspose file tools*
The moose likes Servlets and the fly likes how to iterate through init params in web.xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to iterate through init params in web.xml?" Watch "how to iterate through init params in web.xml?" New topic
Author

how to iterate through init params in web.xml?

Stan Lederer
Ranch Hand

Joined: Sep 24, 2012
Posts: 43
Hi,

I've got a servlet with an init-param in web.xml. I'm able to read it fine in my init method like this:

Now, I want to generalize my servlet by reading several init-params and loading them into a hashmap.

As an example, let's say I have this in my web.xml file:

How do I iterate through the param-name/param-value pairs and load up my hashmap?

Thanks.

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Well, there are parsers available for retrieving the elements stored inside XML files. Read this tutorial. There is a class called DefaultHandler that your class needs to extend. It has callback methods that the parser calls internally. When you override them, you will be able to parse the XML. Study these and try it out. Come back if you are stuck somewhere.


~ Mansukh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

Parsing the XML is nuts! Don't even think of doing that!

Simply use the getInitParameterNames() method of the ServletConfig to get the names of the parameters, then use getInitParameter() on each to fetch the values.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stan Lederer
Ranch Hand

Joined: Sep 24, 2012
Posts: 43
Thanks, Bear. I've dealt with the SAX Parser. It's not fun, especially for a simple task like this. getInitParameterNames() was the missing piece.

Thanks, again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

There's really no need to take apart the engine to see how much oil is in the crankcase; just read the dial.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Bear Bibeault wrote:There's really no need to take apart the engine to see how much oil is in the crankcase; just read the dial.


I liked it. There should be a love button too.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Stan Lederer wrote:Thanks, Bear. I've dealt with the SAX Parser. It's not fun, especially for a simple task like this.


Yes, I agree. Suggesting to use a SAX parser for this task wasn't a good suggestion either. If you were going to parse that XML (which you aren't) then the most convenient possible parser would be the way to go.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to iterate through init params in web.xml?