Vous êtes sur la page 1sur 2

Signature: public interface ServletConfig ServletConfig is implemented by the servlet container to initialize a single servlet using init().

That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class.

Example code: <servlet> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>com.javapapers.ServletConfigTest</servlet-class> <init-param> <param-name>topic</param-name> <param-value>Difference between ServletConfig and ServletContext</param-value> </init-param> </servlet>

Signature: public interface ServletContext ServletContext is implemented by the servlet container for all servlet to communicate with its servlet container, for example, to get the MIME type of a file, to get dispatch requests, or to write to a log file. That is to get detail about its execution environment. It is applicable only within a single Java Virtual Machine. If a web applicationa is distributed between multiple JVM this will not work. For understanding, this is like a application global variable mechanism for a single web application deployed in only one JVM.

The ServletContext object is contained within the ServletConfig object. That is, the ServletContext can be accessed using the ServletConfig object within a servlet. You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file. Example code: <context-param> <param-name>globalVariable</param-name> <param-value>javapapers.com</param-value> </context-param>

Difference between ServletContext vs ServletConfig Now lets see difference between ServletContext and ServletConfig in Servlets JSP in tabular format

Servlet Config Servlet config object represent single servlet

Servlet Context It represent whole web application running on particular JVM and common for all the servlet Its like global parameter associated with whole application ServletContext has application wide scope so define outside of servlet tag in web.xml file.

Its like local parameter associated with particular servlet Its a name value pair defined inside the servlet section of web.xml file so it has servlet wide scope getServletConfig() method is used to get the config object for example shopping cart of a user is a specific to particular user so here we can use servlet config

getServletContext() method is used to get the context object. To get the MIME type of a file or application session related information is stored using servlet context object.

Vous aimerez peut-être aussi