File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to iterate through init params in web.xml?

 
Stan Lederer
Ranch Hand
Posts: 57
  • 0
  • 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
  • 0
  • 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: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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: 57
  • 0
  • 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: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 0
  • 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
Pie
Posts: 20171
24
MySQL Database
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic