Difference between servlet init and servlet context parameter

2 Flares Twitter 0 Facebook 2 Google+ 0 LinkedIn 0 Filament.io 2 Flares ×

Often programmer’s wonder what is the difference between the servlet init and context parameters. This is a quick look at the difference between these two method of parameter initialization’s.

Servlet init parameter

You can see that init-param is defined inside a servlet element. This means it is only available to the servlet under declaration and not to other parts of the web application. If you want this parameter to be available to other parts of the application say a JSP this needs to be explicitly passed to the JSP. For instance passed as request.setAttribute(). This is highly inefficient and difficult to code. So if you want to get access to global values from anywhere within the application without explicitly passing those values, you need to use Context Init parameters.

Context Parameter

This context param is available to all parts of the web application and it can be retrieved from the Context object.  For instance,  getServletContext().getInitParameter(“dbname”);

From a JSP  you can access the context parameter using the application implicit object.  For example, application.getAttribute(“dbname”);