<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">GenericServlet class implements Servlet, ServletConfig and Serializable interfaces. It provides the implementaion of all the methods of these interfaces except the service method. GenericServlet may be directly extended by a servlet, although it&#8217;s more common to extend a protocol-specific subclass such as HttpServlet.GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet also implements the log method, declared in the ServletContext interface.</p>
<p>GenericServlet class can handle any type of request so it is protocol-independent.</p>
<p>You may create a generic servlet by inheriting the GenericServlet class and providing the implementation of the service method.</p>
<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>Methods of <i><b>GenericServlet </b></i>class</h2>
<p>There are many methods in <i><b>GenericServlet </b></i>class. They are as follows:</p>
<ul style="text-align: left;">
<li><b>public void init(ServletConfig config)</b> is used to initialize the servlet.</li>
<li><b>public abstract void service(ServletRequest request, ServletResponse response) </b>provides service for the incoming request. It is invoked at each time when user requests for a servlet.</li>
<li><b>public void destroy()</b> is invoked only once throughout the life cycle and indicates that servlet is being destroyed.</li>
<li><b>public ServletConfig getServletConfig()</b> returns the object of ServletConfig.</li>
<li><b>public String getServletInfo()</b> returns information about servlet such as writer, copyright, version etc.</li>
<li><b>public void init()</b> it is a convenient method for the servlet programmers, now there is no need to call super.init(config)</li>
<li><b>public ServletContext getServletContext() </b>returns the object of ServletContext.</li>
<li><b>public String getInitParameter(String name)</b> returns the parameter value for the given parameter name.</li>
<li><b>public Enumeration getInitParameterNames()</b> returns all the parameters defined in the web.xml file.</li>
<li><b>public String getServletName()</b> returns the name of the servlet object.</li>
<li><b>public void log(String msg) </b>writes the given message in the servlet log file.</li>
<li><b>public void log(String msg,Throwable t)</b> writes the explanatory message in the servlet log file and a stack trace.</li>
</ul>
<h2>Difference between <i><b>HttpServlet </b></i>and <i><b>GenericServlet</b></i>&#8211;</h2>
<h3><b>javax.servlet.GenericServlet</b></h3>
<p>Signature: <i><b>public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable</b></i></p>
<ul style="text-align: left;">
<li><b><i>GenericServlet </i></b>defines a generic, protocol-independent servlet.</li>
<li><i><b>GenericServlet </b></i>gives a blueprint and makes writing servlet easier.</li>
<li><i><b>GenericServlet </b></i>provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.</li>
<li><b><i>GenericServlet </i></b>implements the log method, declared in the ServletContext interface.</li>
<li>To write a generic servlet, it is sufficient to override the abstract service method.</li>
</ul>
<h3><b>javax.servlet.http.HttpServlet</b></h3>
<p>Signature: <i><b>public abstract class HttpServlet extends GenericServlet implements java.io.Serializable</b></i></p>
<ul style="text-align: left;">
<li><b><i>HttpServlet </i></b>defines a HTTP protocol specific servlet.</li>
<li><i><b>HttpServlet </b></i>gives a blueprint for Http servlet and makes writing them easier.</li>
<li><b><i>HttpServlet </i></b>extends the <b><i>GenericServlet </i></b>and hence inherits the properties <i><b>GenericServlet</b></i>.</li>
</ul>
<h2>Servlet Example by inheriting the <i><b>GenericServlet </b></i>class-</h2>
<p>Let&#8217;s see the simple example of servlet by inheriting the GenericServlet class.</p>
<pre class="highlight">import java.io.*; 
 import javax.servlet.*; 
 
 public class HelloServlet extends GenericServlet{ 
 public void service(ServletRequest req,ServletResponse res) 
 throws IOException,ServletException{ 
 
 res.setContentType("text/html"); 
 
 PrintWriter out=res.getWriter(); 
 out.print("<;html>;<;body>;"); 
 out.print("<;b>;hello generic servlet<;/b>;"); 
 out.print("<;/body>;<;/html>;"); 
 
 } 
 } 
</pre>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/servlets-form-data-basics-of-web/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/class-httpservlet/" 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/servlets-form-data-basics-of-web/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/class-httpservlet/">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: '268'});
});
</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…