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)
Most Recent servlet Faqs
- How to get the client information in a Servlet?
- Can I control Session timeout?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- When do I use HttpSessionListener?
- What is the difference between the request attribute and request parameter?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
- How do you communicate in between applets and servlets?
Most Viewed servlet Faqs
- What is the difference between the request attribute and request parameter?
- When do I use HttpSessionListener?
- How to use ServletContext.getResourceAsStream(java.lang.String path)?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- Can I control Session timeout?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
- How to get the client information in a Servlet?