This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes How does config.getInitParameter( )  know where to get the parameter. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How does config.getInitParameter( )  know where to get the parameter." Watch "How does config.getInitParameter( )  know where to get the parameter." New topic
Author

How does config.getInitParameter( ) know where to get the parameter.

lin xy
Greenhorn

Joined: Oct 29, 2002
Posts: 1
There is a web application.
File -web.xml:
<servlet>
....
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
some servlet file:
...
public void init(){
ServletConfig config=getServletConfig();
param=config.getInitParameter("name");//get value here
}
...

q1:how does config.getInitParameter()know which file to read?
q2:how does config.getInitParameter() know which tag to read in file web.xml?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Moving to Servlets forum...


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by lin xy:
There is a web application.
File -web.xml:
<servlet>
....
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
some servlet file:
...
public void init(){
ServletConfig config=getServletConfig();
param=config.getInitParameter("name");//get value here
}
...

q1:how does config.getInitParameter()know which file to read?
q2:how does config.getInitParameter() know which tag to read in file web.xml?

Ans for both questions:
It reads the web.xml which is under ContextName/WEB-INF directory.
Path upto ContextName is defined, in case of Tomcat, in server.xml.
HTH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
arul murug
Greenhorn

Joined: Aug 08, 2002
Posts: 24
q1:how does config.getInitParameter()know which file to read?
q2:how does config.getInitParameter() know which tag to read in file web.xml?

To answer your questions Web container reads the web.xml file for the application and creates the servletconfig object when it loads your web application.From servletconfig you get these parameters using getInitParameter().


SCJP2<br />SCWCD<br />UML & OOAD - IBM 486
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does config.getInitParameter( ) know where to get the parameter.