FAQ

Java

JSP

Servlet


Advertisement



What is the difference between the initialization parameters in and ?

In the Servlet 2.3 Specification:

 

<!--
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?)>
...

<!--
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?)>

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. These init parameters are within the scrope of the entire application. The parameters defined in <context-param> are available by calling getServletContext().getInitParameter(String name).

The init-params of a servlet are specific to that servlet, eg: a value that the servlet uses. These init parameters are within the scope of the servlet. The parameters defined in <servlet> are available by calling getInitParameter(String name) on the ServletConfig that was passed to the servlet/filter's init method. Since GenericServlet stores this ServletConfig object you can simply call the getInitParameter(String name) that it provides.

For example:

getServletContext().getInitParameter("foo") (returns context init params), 
getServletConfig().getInitParameters("foo") (returns servlet init params).
getInitParameters("foo") (uses method inherited directly from GenericServlet and returns servlet init params)


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |