This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to iterate through init params in web.xml?

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic