<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: justify;">An object of <b><i>ServletContext </i></b>is created by the web container at time of deploying the project. This object can be used to get configuration information from web.xml file. There is only one <i><b>ServletContext </b></i>object per web application.If any information is shared to many servlet, it is better to provide it from the web.xml file using the element.</p>
<h2><b>Advantage of <i>ServletContext</i></b></h2>
<p>Easy to maintain if any information is shared to all the servlet, it is better to make it available for all the servlet. We provide this information from the web.xml file, so if the information is changed, we don&#8217;t need to modify the servlet. Thus it removes maintenance problem.</p>
<h2><b>Usage of <i>ServletContext </i>Interface</b></h2>
<p>There can be a lot of usage of <i><b>ServletContext </b></i>object. Some of them are as follows:</p>
<ul style="text-align: left;">
<li>The object of <i><b>ServletContext </b></i>provides an interface between the container and servlet.</li>
<li>The <i><b>ServletContext </b></i>object can be used to get configuration information from the web.xml file.</li>
<li>The <i><b>ServletContext </b></i>object can be used to set, get or remove attribute from the web.xml file.</li>
<li>The <i><b>ServletContext </b></i>object can be used to provide inter-application communication.</li>
</ul>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th>Methods</th>
<th>Description</th>
</tr>
<tr>
<td>Object getAttribute(String name)</td>
<td>returns the container attribute with the given name, or null if there is no attribute by that name.</td>
</tr>
<tr>
<td>String getInitParameter(String name)</td>
<td>returns parameter value for the specified parameter name, or null if the parameter does not exist</td>
</tr>
<tr>
<td>Enumeration getInitParameterNames()</td>
<td>returns the names of the context&#8217;s initialization parameters as an Enumeration of String objects</td>
</tr>
<tr>
<td>void setAttribute(String name,Object obj)</td>
<td>set an object with the given attribute name in the application scope</td>
</tr>
<tr>
<td>void removeAttribute(String name)</td>
<td>removes the attribute with the specified name from the application context</td>
</tr>
</tbody>
</table>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111.png" border="0" /></div>
<h2><b>How to get the object of <i>ServletContext</i></b></h2>
<pre class="highlight">ServletContext app = getServletContext(); 
ServletContext app = getServletConfig().getServletContext(); 
</pre>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/servletcontext.jpg" border="0" /></div>
<h2><b>How to get the object of ServletContext interface</b></h2>
<pre class="highlight">getServletContext() method of ServletConfig interface returns the object of ServletContext. 
 getServletContext() method of GenericServlet class returns the object of ServletContext. 
</pre>
<p><i><b>Syntax of getServletContext() method</b></i></p>
<pre class="highlight">public ServletContext getServletContext() 
</pre>
<p><i><b>Example of getServletContext() method</b></i></p>
<pre class="highlight">//We can get the ServletContext object from ServletConfig object 
 ServletContext application=getServletConfig().getServletContext(); 
 
 //Another convenient way to get the ServletContext object 
 ServletContext application=getServletContext(); 
</pre>
<h2><b>Example of <i>ServletContext </i>to get the initialization parameter</b></h2>
<p>In this example, we are getting the initialization parameter from the web.xml file and printing the value of the initialization parameter. Notice that the object of ServletContext represents the application scope. So if we change the value of the parameter from the web.xml file, all the servlet classes will get the changed value. So we don&#8217;t need to modify the servlet. So it is better to have the common information for most of the servlets in the web.xml file by context-param element. Let&#8217;s see the simple example:<br />
<i><b>HelloServlet.java</b></i></p>
<pre class="highlight">import java.io.*; 
 import javax.servlet.*; 
 import javax.servlet.http.*; 
 
 
 public class HelloServlet extends HttpServlet{ 
 public void doGet(HttpServletRequest req,HttpServletResponse res) 
 throws ServletException,IOException 
 { 
 res.setContentType("text/html"); 
 PrintWriter pw=res.getWriter(); 
 
 //creating ServletContext object 
 ServletContext context=getServletContext(); 
 
 //Getting the value of the initialization parameter and printing it 
 String driverName=context.getInitParameter("driverName"); 
 pw.println("driver name is="+driverName); 
 
 pw.close(); 
 
 } 
} 
</pre>
<p><i><b>web.xml</b></i></p>
<pre class="highlight"><;web-app>; 
 
 <;servlet>; 
 <;servlet-name>;HelloServlet<;/servlet-name>; 
 <;servlet-class>;HelloServlet<;/servlet-class>; 
 <;/servlet>; 
 
 <;context-param>; 
 <;param-name>;driverName<;/param-name>; 
 <;param-value>;sun.jdbc.odbc.JdbcOdbcDriver<;/param-value>; 
 <;/context-param>; 
 
 <;servlet-mapping>; 
 <;servlet-name>;HelloServlet<;/servlet-name>; 
 <;url-pattern>;/context<;/url-pattern>; 
 <;/servlet-mapping>; 
 
 <;/web-app>; 
</pre>
<h2><b>Example of <i>ServletContext </i>to get all the initialization parameters</b></h2>
<p>In this example, we are getting all the initialization parameter from the web.xml file. For getting all the parameters, we have used the getInitParameterNames() method in the servlet class.</p>
<p><i><b>HelloServlet.java</b></i></p>
<pre class="highlight">import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
 
public class DemoServlet extends HttpServlet{ 
public void doGet(HttpServletRequest req,HttpServletResponse res) 
throws ServletException,IOException 
{ 
res.setContentType("text/html"); 
PrintWriter out=res.getWriter(); 
 
ServletContext context=getServletContext(); 
Enumeration<;string>; e=context.getInitParameterNames(); 
 
String str=""; 
while(e.hasMoreElements()){ 
 str=e.nextElement(); 
 out.print("<;br>; "+context.getInitParameter(str)); 
} 
} 
} 
</pre>
<p><i><b>web.xml</b></i></p>
<pre class="highlight"><;web-app>; 
 
 <;servlet>; 
 <;servlet-name>;HelloServlet<;/servlet-name>; 
 <;servlet-class>;HelloServlet<;/servlet-class>; 
 <;/servlet>; 
 
 <;context-param>; 
 <;param-name>;driverName<;/param-name>; 
 <;param-value>;sun.jdbc.odbc.JdbcOdbcDriver<;/param-value>; 
 <;/context-param>; 
 
 <;context-param>; 
 <;param-name>;name<;/param-name>; 
 <;param-value>;dinesh<;/param-value>; 
 <;/context-param>; 
 
 <;context-param>; 
 <;param-name>;email<;/param-name>; 
 <;param-value>;admin@dineshonjava.com<;/param-value>; 
 <;/context-param>; 
 
 <;servlet-mapping>; 
 <;servlet-name>;HelloServlet<;/servlet-name>; 
 <;url-pattern>;/context<;/url-pattern>; 
 <;/servlet-mapping>; 
 
 <;/web-app>; 
</pre>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlets-database-access/">Database Access Example using Sevlet</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/servletconfig-interface/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/the-servlet-cookie-api/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/servletconfig-interface/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/the-servlet-cookie-api/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '260'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…