Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what's the corresponding method for init-param?

 
Amanda Fu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean how to get the params defined in DD by method?
like,
<servlet>
<init-param>
<parm-name>id</parm-name>
<parm-value>foo</parm-value>
 
Amanda Fu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where i am confused is that <context-param> and <init-param>.
context.getInitParamter() will retrieve from <context-param> or <init-param>?
 
Phil Rhodes
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext.getInitParameter() will get the context-wide init parameter defined by <context-param>, yes.
And ServletConfig.getInitParameter() will retrieve the servlet specific init parameter defined by <init-param>.
 
Lou Caudell
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What may add additional confusion here is the issue of how would one address a redundantly named parameter.
// web.xml
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.servlet1</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>com.servlet2</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

How would one address the non-unique <init-param> "debug" with getinitParmeter() and retreive the desired value from the correct <servlet-name>. As you can see each servlet has a debug parameter.

I have accessed an implemented subclass
class Servlet1_XML_Param {
public static final String DEBUG = "debug";
}
and passed the static value.

String dbg = context.getinitParameter(Servlet1_XML_Param.DEBUG);

to obtain the value, but how do you do it by just passing a string?
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be two <init-param>'s with the same name in two different Servlets since they belong only to the ServletConfig of each Servlet and are unique. But the same cannot be true for <context-param> since they are for the whole application.

One Servlet cannot access the <init-param> of other Servlets whereas <context-param> can be accessed by any Servlet and could be changed. We use getServletConfig.getInitParameter("") to retrieve <init-param>'s and getServletContext.getInitParameter("") to retrieve <context-param>'s.

A discussion on it is in Pg 158 of HFJS.

Thanks
Chandu
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Additionaly.

String dbg = context.getinitParameter(Servlet1_XML_Param.DEBUG);

return null according to your web.xml, assuming the context is of type ServltContext.

Thanks
 
Lou Caudell
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. Security..
Thanx
 
Radhika Gokhale
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi narender,

I didn't get it. Why will
String dbg = context.getinitParameter(Servlet1_XML_Param.DEBUG);

return null? Please clarify.

Regards,
Radhika
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Radhika,

The web.xml is definded as

<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.servlet1</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>com.servlet2</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


So, there is no Context parameters defined in the web.xml. Therefore when you try to get the Init parameters through ServletContext getInitiParameter method. It always return null.

If you relly want to access the application level paramenter "debug", try the follwing web.xml.



Hope it help you.

Thanks
 
Salvador Cecilio
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amanda Fu:
where i am confused is that <context-param> and <init-param>.

context.getInitParamter() will retrieve from <context-param> or <init-param>?


I was just looking at this and was confused until I figured it out.

To get the init-param value:



To get the context-param value:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic