aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes what's the corresponding method for init-param? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "what Watch "what New topic
Author

what's the corresponding method for init-param?

Amanda Fu
Ranch Hand

Joined: Jul 17, 2003
Posts: 31
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

Joined: Jul 17, 2003
Posts: 31
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

Joined: Dec 27, 2003
Posts: 65
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>.


A+, Network+, SCJP, SCWCD<br />preparing for SCBCD, SCEA, CompTIA I-Net+
Lou Caudell
Ranch Hand

Joined: Oct 06, 2001
Posts: 32
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

Joined: May 05, 2005
Posts: 162
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

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Lou Caudell
Ranch Hand

Joined: Oct 06, 2001
Posts: 32
Ah, I see. Security..
Thanx
Radhika Gokhale
Ranch Hand

Joined: Dec 12, 2002
Posts: 40
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

Joined: Dec 04, 2004
Posts: 950
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

Joined: Dec 20, 2004
Posts: 41
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:



SCJP 1.4 - 93%
SCWCD 1.4 - 89%
IBM FileNet 3.5 - Developer
IBM FileNet 3.5 - Administrator
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: what's the corresponding method for init-param?
 
Similar Threads
wsdd urgent help
Servlet Initialization Parameters
JSP init parameters.
Unable to get init parameters in servlet
wsdd urgent help Web Services