Monday, September 17, 2012

Difference between ServletConfig and ServletContext in JSP Servlet J2EE

Difference between ServletConfig and ServletContext
ServletContext and ServletConfig these two are 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 servlet container to initialize various parameter of web application. But they have some difference in terms of scope and availability so first we see what is ServletContext and ServletConfig objects are and then we will see difference between ServletConfig and ServletContext in Java J2EE.

What is ServletConfig in JSP Servlet
Difference between ServletContext and ServletConfig in J2EE Servlt JSPServletConfig  in an interface in Servlet API and ServletConfig object represents or used to initialize single servlet in web application by servlet container. Inside deployment descriptor known as web.xml,  we define Servlet initialization parameter related to that servlet inside <init-param></init-param> tag. its a set of name /value pair. Following is signature of ServletConfig interface :

public interface ServletConfig

By using ServletConfig and combination of init-param you can configure any Servlet in J2EE environment.

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

Signature: public interface ServletContext

By using ServletContext object you can share objects to any Servlet or JSP in 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 represent whole web application running on particular JVM and common for all the servlet
Its like local parameter associated with particular servlet
Its like global parameter associated with 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 a specific to 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 servlet context object.

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

Other Java tutorials from Java tutorials Java67 Blog

No comments:

Post a Comment

Java67 Headline Animator