<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">Servlets are Java classes which service HTTP requests and implement the <i><b>javax.servlet.Servlet</b></i> interface. Web application developers typically write servlets that extend <b><i>javax.servlet.http.HttpServlet</i></b>, an abstract class that implements the Servlet interface and is specially designed to handle HTTP requests.</p>
<h2><b>Servlet Interface-</b></h2>
<p>Servlet interface provides common behavior to all the servlets.</p>
<p>Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet and 2 non-life cycle methods.</p>
<h2><b>Methods of Servlet interface</b></h2>
<p>There are 5 methods in <b>Servlet interface.</b> The<i><b> init, service and destroy</b></i> are the life cycle methods of servlet. These are invoked by the web container.</p>
<h3><b>Method Description</b></h3>
<ol style="text-align: left;">
<li><b>public void init(ServletConfig config)</b> initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once.</li>
<li><b>public void service(ServletRequest request,ServletResponse response)</b> provides response for the incoming request. It is invoked at each request by the web container.</li>
<li><b>public void destroy()</b> is invoked only once and indicates that servlet is being destroyed.</li>
<li><b>public ServletConfig getServletConfig()</b> returns the object of <b><i>ServletConfig</i></b>.</li>
<li><b>public String getServletInfo() </b>returns information about servlet such as writer, copyright, version etc.</li>
</ol>
<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>Hello World Example in Servlet:</b></h2>
<p>Following is the sample source code structure of a servlet example to write Hello World:</p>
<pre class="highlight">// Import required java libraries 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
// Extend HttpServlet class 
public class HelloWorld extends HttpServlet { 
 
 private String message; 
 
 public void init() throws ServletException 
 { 
 // Do required initialization 
 message = "Hello World"; 
 } 
 
 public void doGet(HttpServletRequest request, 
 HttpServletResponse response) 
 throws ServletException, IOException 
 { 
 // Set response content type 
 response.setContentType("text/html"); 
 
 // Actual logic goes here. 
 PrintWriter out = response.getWriter(); 
 out.println("<;h1>;" + message + "<;/h1>;"); 
 } 
 
 public void destroy() 
 { 
 // do nothing. 
 } 
} 
</pre>
<h2><b>Compiling a Servlet:</b></h2>
<p>Let us put above code if<i><b> HelloWorld.java</b></i> file and put this file in <b><i>D:Servlet (Windows)</i></b> or <i><b>/usr/Servlet (Unix)</b></i> then you would need to add these directories as well in <b><i>CLASSPATH</i></b>.</p>
<p>Assuming your environment is setup properly, go in Servlet directory and compile<b><i> HelloWorld.java</i></b> as follows:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/Untitled-4.png" border="0" /></div>
<p>If the servlet depends on any other libraries, you have to include those JAR files on your <i><b>CLASSPATH </b></i>as well. I have included only <b>servlet-api.jar</b> JAR file because I&#8217;m not using any other library in Hello World program.</p>
<p>If everything goes fine, above compilation would produce <b>HelloWorld.class </b>file in the same directory. Next section would explain how a compiled servlet would be deployed in production.</p>
<h2><b>Servlet Deployment:</b></h2>
<p>By default, a servlet application is located at the path <b><i>C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOT</i></b> and the class file would reside in <b><i>C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOTWEB-INFclasses.</i></b></p>
<p>If you have a fully qualified class name of <i><b>com.dineshonjava.MyServle</b></i>t, then this servlet class must be located in <i><b> <b><i>C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOT</i></b>WEB-INF/classes/com/dineshonjava/MyServlet.class</b></i>.</p>
<p>For now, let us copy<b><i> HelloWorld.class</i></b> into <b><i>/webapps/ROOT/WEB-INF/classes</i></b> and create following entries in <i><b>web.xml</b></i> file located in <b><i>C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOT</i></b><i><b>/webapps/ROOT/WEB-INF/</b></i></p>
<pre class="highlight"><;servlet>; 
 <;servlet-name>;HelloWorld<;/servlet-name>; 
 <;servlet-class>;HelloWorld<;/servlet-class>; 
 <;/servlet>; 
 
 <;servlet-mapping>; 
 <;servlet-name>;HelloWorld<;/servlet-name>; 
 <;url-pattern>;/HelloWorld<;/url-pattern>; 
 <;/servlet-mapping>; 
</pre>
<p>Now let us start tomcat server using <i><b>C:Program Files (x86)Apache Software FoundationTomcat 7.0binstartup.bat </b></i>(on windows) or <b>/bin/startup.sh (on Linux/Solaris etc.) </b>and finally type <i><b>http://localhost:8080/HelloWorld</b></i> in browser&#8217;s address box. If everything goes fine, you would get following result:</p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/Untitled-5.png" border="0" /></div>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/servlet-api/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/what-is-web-application-servlet/" target="_blank" rel="noopener">Next</a> >;>;</b></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>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/servlet-api/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/what-is-web-application-servlet/">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: '272'});
});
</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…