aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Initialization Parameters in servlet And in context-param 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 "Initialization Parameters in servlet And in context-param" Watch "Initialization Parameters in servlet And in context-param" New topic
Author

Initialization Parameters in servlet And in context-param

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
What is the difference between the initialization parameters in <servlet> and the initialization parameters in <context-param>? If anybody could shed light on it. To be specific, in the Servlet 2.3 Specification, p. 100
<!--
The init-param element contains a name/value pair as an
initialization param of the servlet
Used in: filter, servlet
-->
<!ELEMENT init-param (param-name, param-value, description?)>
and in the Servlet 2.3 Specification, p. 92
<!--
The context-param element contains the declaration of a web
application´┐Żs servlet context initialization parameters.
Used in: web-app
-->
<!ELEMENT context-param (param-name, param-value, description?)>
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Any parameter dfined in the <servlet><init-param> is available to that servlet and that servlet only through the method HttpServlet.getInitParameter
Where parameters defined in <context-param> are available to all servlets (and JSP pages) in that web-app Using ServletConfig.getInitParameter
------------------
I Hope This Helps
Carl Trusiak, SCJP2


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Tim Duncan
Ranch Hand

Joined: Aug 20, 2001
Posts: 150
Originally posted by JiaPei Jen:
What is the difference between the initialization parameters in <servlet> and the initialization parameters in <context-param>?

Well the easy answer is that you use different methods to access them, however the real difference is in intended use.
Context parameters are accessible to any servlet or JSP in the web-app. The usual example is webmaster's address. Another example might be the name of a system or resource that is required by many parts of the system.
The init-params of a servlet are specific to that servlet, eg: a value that the servlet uses.
Hope that helps ...
Tim Duncan
Ranch Hand

Joined: Aug 20, 2001
Posts: 150
Originally posted by Carl Trusiak:
Any parameter dfined in the <servlet><init-param> is available to that servlet and that servlet only through the method HttpServlet.getInitParameter
Where parameters defined in <context-param> are available to all servlets (and JSP pages) in that web-app Using ServletConfig.getInitParameter

A mistake here, that should be ServletContext I think.
context-params are available by calling getServletContext().getInitParameter(String name)
a servlet or filter's init-params are available by calling getInitParameter(String name) on the ServletConfig that was passed to the servlet/filter's init method. Since GenericServlet stores this config object you can simply call the getInitParameter(String name) that it provides.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Thanks Tim,
I got the wrong Object type this morning. It is ServletContext,getInitParameter.
Carl
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Tim and Carl, thanks a lot for your help.
nitin shar
Greenhorn

Joined: Sep 16, 2001
Posts: 12
Hello Tim,
Correct me if I am wrong.Any initialization parameter for a particular sevlet written in
<servlet>
<init-param>
can be retrieved by
ServletConfig.getInitParameter()

and for the parameters to be available to all servlets
written in
<context-param>
can be retrieved by
ServletContext.getInitParameter()
thanks
nitin
Tim Duncan
Ranch Hand

Joined: Aug 20, 2001
Posts: 150

Yes, that's it. ServletConfig and ServletContext both have getInitParameter methods. But just to be crystal clear ... they're not static methods, so you'll need an instance of ServletConfig or ServletContext to call the methods on.
If your servlet extends GenericServlet (or its child HttpServlet) then you can call getInitParameter on the servlet itself,
because the inherited init method saves the ServletConfig object that it receives when the servlet starts, and the servlet can then forward getInitParameter to the stored ServletConfig object. So, if you ever override your servlet's init method you need to remember to call super.init to retain this functionality.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initialization Parameters in servlet And in context-param