• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

what's the corresponding method for init-param?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I see. Security..
Thanx
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic