ServletConfig and ServletContext, both are objects created at the time of servlet initialization and used to provide some initial parameters or configuration information to the servlet. But, the difference lies in the fact that information shared by ServletConfig is for a specific servlet, while information shared by ServletContext is available for all servlets in the web application.
- ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization.
- ServletConfig is for a particular servlet, that means one should store servlet specific information in web.xml and retrieve them using this object.
Suppose, one is building a job portal and desires to share different email ids (which may get change over time) to recruiter and job applicant.
So, he decides to write two servlets one for handling recruiter’s request and another one for the job applicant.
Where to store email-ids?
Put email-id as a name-value pair for different servlet inside web.xml which can further be retrieved using getServletConfig().getInitParameter(“name”) in the servlet.
- ServletContext is the object created by Servlet Container to share initial parameters or configuration information to the whole application.
Suppose, the name of one’s job portal is “NewWebsite.tg”. Showing the website name at the top of webpages delivered by different servlets, one needs to store the website name in every servlet inviting redundancy. Since the information shared by ServletContext can be accessed by every Servlet, it is better to go with ServletContext and retrieve the website name using getServletContext.getInitParameter(“Name”) whenever required.
Implementation of examples of ServletConfig and ServletContext is shown below.
Servlet for recruiter
Servlet for applicant
- Output: Deploy the app and open urls on localhost:
As shown above, different servlets get different email-id with same parameter name “Email” because of different values attached to it in the web.xml.
Each servlet has got their own <init-param> inside <servlet> containing email-id.
- Both servlets got the access of ServletContext parameter “Website-name”.Since, <context-param> is outside of <servlet> in web.xml, hence it is independent of servlet and accessible from whole app.
Below is the table of comparison between the two:
|ServletConfig is servlet specific||ServletContext is for whole application|
|Parameters of servletConfig are present as name-value pair in <init-param> inside <servlet>.||Parameters of servletContext are present as name-value pair in <context-param> which is outside of <servlet> and inside <web-app>|
|ServletConfig object is obtained by getServletConfig() method.||ServletContext object is obtained by getServletContext() method.|
|Each servlet has got its own ServletConfig object.||ServletContext object is only one and used by different servlets of the application.|
|Use ServletConfig when only one servlet needs information shared by it.||Use ServletContext when whole application needs information shared by it|
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Difference between Java Servlet and CGI
- Difference between Servlet and JSP
- Servlet Collaboration In Java Using RequestDispatcher and HttpServletResponse
- Java Servlet and JDBC Example | Insert data in MySQL
- Javax.servlet.http.Cookie class in Java
- URL Rewriting using Java Servlet
- Hidden Form Field using Annotation | Java Servlet
- How to add Image to MySql database using Servlet and JDBC
- Starting with first Servlet Application
- The HttpSession Interface in Servlet
- Life Cycle of a Servlet
- Difference between Core Java and Advanced Java
- Difference between a Java Application and a Java Applet
- Difference between Java IO and Java NIO
- Difference between Java and Core Java
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Similarities and Difference between Java and C++
- Difference between Scanner and BufferReader Class in Java
- Difference between Abstract Class and Interface in Java
- Difference between == and .equals() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.