File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getInitParameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getInitParameter" Watch "getInitParameter" New topic


dh chau

Joined: Feb 01, 2003
Posts: 5
What is the difference between ServletConfig().getInitParameter() and ServletContext().getInitParameter()?
I don't know when to use ServletContext().getInitParameter(). From API of Sun, this fn returns a String containing the value of the named context-wide initialization parameter. What is the meaning context-wide initialization parameter?
Many thanks!!
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
hi dh chau,
the getInitParameter of servletConfig is used to get the parameter values that were defined in the <init-param> tag of the <servlet> in web.xml e.g.

This params are available only inside the <servlet-class> specified i.e. com.DailyOvertimeServlet.
So inside this servlet, you can retrieve this initialization parameters using
String yourName = getServletConfig().getInitParameter("yourName");
For the getInitParameter of servletContext, it it used to get the parameter values that were defined in the
<context-param> tag under <web-app> in web.xml

This context parameters are available to all servlets in your web-app i.e. context wide.
Thus, in ANY servlet in your web app, you can retrieve the context parameters using
String companyName = getServletContext().getInitParameter("companyName");
But don't just take my word on this, write some code, run, and see it happen.
Hema Menon
Ranch Hand

Joined: Oct 29, 2000
Posts: 569
Related/Similar discussion here.

I agree. Here's the link:
subject: getInitParameter
It's not a secret anymore!