<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;"><i><b>ServletConfig </b></i>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.An object of ServletConfig is created by the web container for each servlet. This object can be used to get configuration information from web.xml file.</p>
<p>If the configuration information is modified from the web.xml file, we don&#8217;t need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time.</p>
<h2><b>Advantage of <i>ServletConfig</i></b></h2>
<p>The core advantage of ServletConfig is that you don&#8217;t need to edit the servlet file if information is modified from the web.xml file.</p>
<h2><b>Methods of <i>ServletConfig </i>interface</b></h2>
<ul style="text-align: left;">
<li><b><i>public String getInitParameter(String name):</i></b>Returns the parameter value for the specified parameter name.</li>
<li><b><i>public Enumeration getInitParameterNames():</i></b>Returns an enumeration of all the initialization parameter names.</li>
<li><i><b>public String getServletName():</b></i>Returns the name of the servlet.</li>
<li><b><i>public ServletContext getServletContext():</i></b>Returns an object of ServletContext.</li>
</ul>
<p> ;</p>
<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>
<h2><b>How to initialize a servlet inside <i>web.xml</i></b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111-1.png" border="0" /></div>
<h3><b>Inside servlet class:</b></h3>
<pre class="highlight">ServletConfig config=getServletConfig(); 
String email=config.getInitParameter("email"); 
</pre>
<h2><b>Example of <i>ServletConfig </i>to get initialization parameter</b></h2>
<p>In this example, we are getting the one initialization parameter from the web.xml file and printing this information in the servlet.</p>
<p><b><i>HelloServlet.java</i></b></p>
<pre class="highlight">import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class HelloServlet extends HttpServlet { 
public void doGet(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 
 response.setContentType("text/html"); 
 PrintWriter out = response.getWriter(); 
 
 ServletConfig config=getServletConfig(); 
 String email=config.getInitParameter("email"); 
 out.print("Email is: "+eamil); 
 
 out.close(); 
 } 
 
} 
</pre>
<p><b><br />
</b> <i><b>web.xml</b></i></p>
<pre class="highlight"><;web-app>; 
 
<;servlet>; 
<;servlet-name>;HelloServlet<;/servlet-name>; 
<;servlet-class>;HelloServlet<;/servlet-class>; 
 
<;init-param>; 
<;param-name>;email<;/param-name>; 
<;param-value>;admin@dineshonjava.com<;/param-value>; 
<;/init-param>; 
 
<;/servlet>; 
 
<;servlet-mapping>; 
<;servlet-name>;HelloServlet<;/servlet-name>; 
<;url-pattern>;/servlet1<;/url-pattern>; 
<;/servlet-mapping>; 
 
<;/web-app>; 
</pre>
<h2><b>Example of <i>ServletConfig </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 and printing this information in the servlet.</p>
<p><i><b>HelloServlet.java</b></i></p>
<pre class="highlight">import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Enumeration; 
 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
 
public class HelloServlet extends HttpServlet { 
public void doGet(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 
 response.setContentType("text/html"); 
 PrintWriter out = response.getWriter(); 
 
 ServletConfig config=getServletConfig(); 
 Enumeration<;String>; e=config.getInitParameterNames(); 
 
 String str=""; 
 while(e.hasMoreElements()){ 
 str=e.nextElement(); 
 out.print("<;br>;Name: "+str); 
 out.print(" value: "+config.getInitParameter(str)); 
 } 
 
 out.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>; 
 
<;init-param>; 
<;param-name>;name<;/param-name>; 
<;param-value>;dinesh<;/param-value>; 
<;/init-param>; 
 
<;init-param>; 
<;param-name>;email<;/param-name>; 
<;param-value>;admin@dineshonjava.com<;/param-value>; 
<;/init-param>; 
 
<;/servlet>; 
 
<;servlet-mapping>; 
<;servlet-name>;HelloServlet<;/servlet-name>; 
<;url-pattern>;/servlet1<;/url-pattern>; 
<;/servlet-mapping>; 
 
<;/web-app>; 
</pre>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/httpsession-interface/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/servletcontext-interface/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<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>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/send-redirect-in-servlet/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/servletcontext-interface/">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: '261'});
});
</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…