Forums Register Login

getInitParameter

+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Related/Similar discussion here.
Hema
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1081 times.
Similar Threads
is this a bug???
get init parameter in filter
ServletContext Vs ServletConfig
getInitParameter() method
Getting file not found exception.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:59:48.