Difference between ServletConfig and ServletContext in JSP Servlet? Answer

Difference between ServletConfig and ServletContext
ServletContext and ServletConfig these two are the important interface of Servlet API which is used by Java J2EE programmer during web application development. Correct understanding of What is ServletContext and ServletConfig is very important for any J2EE application developer. Apart from that Difference between ServletContext and ServletConfig are a popular Servlet JSP interview questions and mostly asked on both fresher and experienced Java programmer during J2EE interviews. 

Both ServletContext and ServletConfig are basically configuration objects which are used by the servlet container to initialize various parameters of web applications. 

But they have some the difference in terms of scope and availability so first, we see what is ServletContext and ServletConfig objects are and then we will see the difference between ServletConfig and ServletContext in Java J2EE.



What is ServletConfig in JSP Servlet

ServletConfig  in an interface in Servlet API and ServletConfig object represents or used to initialize a single servlet in web application by the servlet container. Inside deployment descriptor known as web.xml,  we define Servlet initialization parameter related to that servlet inside <init-param></init-param> tag. it's a set of name /value pairs. 

Following is the signature of ServletConfig interface :

public interface ServletConfig

By using ServletConfig and a combination of init-param you can configure any Servlet in the J2EE environment. You can further refer to these Servlet and JSP courses to learn more about ServletContext and Servlet config in the Java JEE environment. 




What is ServletContext in Servlet JSP? Answer

ServletContext is even more important than ServletConfig and its one per web application, also known as Context. This object is common for all the servlets and they use this object to communicate with the servlet container to get the detail of the whole web application or execution environment important thing to remember is, it represents a web application in a single JVM.

Signature: 

public interface ServletContext

By using the ServletContext object you can share objects to any Servlet or JSP in the whole web application. See how to get ServletContext in Servlet JSP and Struts to find out different ways of getting ServletContext in J2EE application.


Difference between ServletContext vs ServletConfig

Now let’s see difference between ServletContext and ServletConfig in Servlets JSP in tabular format

Servlet Config
Servlet Context
Servlet config object represent single servlet
It represents the whole web application running on a particular JVM and common for all the servlet
It's like a local parameter associated with a particular servlet
It's like a global parameter associated with the whole application
It’s a name-value pair defined inside the servlet section of web.xml file so it has servlet wide scope
ServletContext has application-wide scope so define outside of servlet tag in web.xml file.
getServletConfig() method is used to get the config object
getServletContext() method is  used to get the context object.
for example shopping cart of a user is specific to the particular user so here we can use servlet config
To get the MIME type of a file or application session related information is stored using a servlet context object.



That’s all on What is ServletContext, What is ServletConfig, and what are the differences between ServletContext and ServletConfig in the J2EE environment. The most important difference to remember is that ServletContext is per web application while ServletConfig is per servlet basis. ServletContext is also referred to as application scope in JSP


Other Java tutorials from Java tutorials Java67 Blog

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.