• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to iterate through init params in web.xml?

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic