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?)>
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
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 ...
Joined: Aug 20, 2001
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.
Joined: Jun 13, 2000
Thanks Tim, I got the wrong Object type this morning. It is ServletContext,getInitParameter. Carl
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
Joined: Aug 20, 2001
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.