Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 10 January 2017

What is difference between ServletConfig and ServletContext ?


Here we will learn all the differences between ServletConfig and ServletContext in Servlet.

Servlet Config
Servlet Context
Servlet Config object is used to set some parameters for each & every servlet.
Servlet Context object is used to set some parameters for whole application.
It sets some local parameters for each servlet.
It sets global parameters for whole application.
It is defined under the <servlet> tag in web.xml.
It is defined outside the servlet tag in web.xml.
To get parameter we use getServletConfig() method .
To get parameter we use getServletContext() method .
Object of servlet config is created during initialization process of servlet.
Object of servlet context is created during deployment of web application.
Servlet config object destroyed as servlet execution is completed.
Servlet context object destroyed when application is removed from server.
Web.xml Declaration Example :

<servlet>
<servlet-name>
    HomeSevlet
</servlet-name>
<servlet-class>
    org.servlet.HomeSevlet
</servlet-class>
<init-param>
   <param-name>servletId</param-name>
   <param-value>101</param-value>
</init-param>
</servlet>
<servlet-mapping>
  <servlet-name>
     HomeSevlet
 </servlet-name>
 <url-pattern>/homeServlet</url-pattern>
</servlet-mapping>
Web.xml Declaration Example :

<context-param>
   <param-name>
       Driver
   </param-name>
   <param-value>
       sun.jdbc.odbc.JdbcOdbcDriver
   </param-value>
</context-param>
Servlet Access Example :

String configObject = getServletConfig().getInitParameter("servletId");
Servlet Access Example :

String contextObject = getServletContext().getInitParameter("driver");


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment