IntelliJ Java IDE
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getInitParameter() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "getInitParameter()" Watch "getInitParameter()" New topic
Author

getInitParameter()

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
what is the difference between ServletConfig.getInitParameter() and ServletContext.getInitParameter() ?
Thank you in advance.
Stephen Tallamy
Greenhorn

Joined: Jan 08, 2002
Posts: 16
ServletConfig contains information specific to that servlet. ServletContext contains information about the web application the servlet is a member of.
Generally you use ServletContext to get global set-up parameters such as the webmaster's email addres or a central database. ServletConfig is used more for specific initalisation parameters for the servlet.
Here is an example web application descriptor:

In this example, param1 is available through the ServletContext of both MyServlet and MyOtherServlet. param2 is available through the ServletConfig of MyServlet only. param3 is available through the ServletConfig of MyOtherServlet only.
Hope this helps.
Rohit Poddar
Ranch Hand

Joined: Aug 18, 2001
Posts: 36
1) ServletContext is same for all the servlets in that Web application i.e. there is only one ServletContext per Web App. per JVM. And hence ServletContext.getInitParameter() will give same parameter values to each Servlet in thar web app.
ServletConfig is the object passed to Servlet by container to give initialization information (which is different for different Servlets) for that servlet as oppose to Servlet Context (which is one per Web APp per JVM) and hence ServletConfig.getInitParameter() might give different values in each servlet.
2) Servlet COntext init parameters are specified in web.xml using <context-param></context-param> tag whereas Servlet init params are specified by <servlet><init-param></init-param></servlet>


SCJP, SCWCD
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Well explained Rohit....


Ashik Uzzaman
Senior Member of Technical Staff,
Salesforce.com, San Francisco, CA, USA.
 
IntelliJ Java IDE
 
subject: getInitParameter()
 
developer file tools