Categories: Servlet

Difference between GenericServlet and HttpServlet

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">GenericServlet class implements Servlet&comma; ServletConfig and Serializable interfaces&period; It provides the implementaion of all the methods of these interfaces except the service method&period; GenericServlet may be directly extended by a servlet&comma; although it&&num;8217&semi;s more common to extend a protocol-specific subclass such as HttpServlet&period;GenericServlet makes writing servlets easier&period; It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface&period; GenericServlet also implements the log method&comma; declared in the ServletContext interface&period;<&sol;p>&NewLine;<p>GenericServlet class can handle any type of request so it is protocol-independent&period;<&sol;p>&NewLine;<p>You may create a generic servlet by inheriting the GenericServlet class and providing the implementation of the service method&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<h2>Methods of <i><b>GenericServlet <&sol;b><&sol;i>class<&sol;h2>&NewLine;<p>There are many methods in <i><b>GenericServlet <&sol;b><&sol;i>class&period; They are as follows&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>public void init&lpar;ServletConfig config&rpar;<&sol;b> is used to initialize the servlet&period;<&sol;li>&NewLine;<li><b>public abstract void service&lpar;ServletRequest request&comma; ServletResponse response&rpar; <&sol;b>provides service for the incoming request&period; It is invoked at each time when user requests for a servlet&period;<&sol;li>&NewLine;<li><b>public void destroy&lpar;&rpar;<&sol;b> is invoked only once throughout the life cycle and indicates that servlet is being destroyed&period;<&sol;li>&NewLine;<li><b>public ServletConfig getServletConfig&lpar;&rpar;<&sol;b> returns the object of ServletConfig&period;<&sol;li>&NewLine;<li><b>public String getServletInfo&lpar;&rpar;<&sol;b> returns information about servlet such as writer&comma; copyright&comma; version etc&period;<&sol;li>&NewLine;<li><b>public void init&lpar;&rpar;<&sol;b> it is a convenient method for the servlet programmers&comma; now there is no need to call super&period;init&lpar;config&rpar;<&sol;li>&NewLine;<li><b>public ServletContext getServletContext&lpar;&rpar; <&sol;b>returns the object of ServletContext&period;<&sol;li>&NewLine;<li><b>public String getInitParameter&lpar;String name&rpar;<&sol;b> returns the parameter value for the given parameter name&period;<&sol;li>&NewLine;<li><b>public Enumeration getInitParameterNames&lpar;&rpar;<&sol;b> returns all the parameters defined in the web&period;xml file&period;<&sol;li>&NewLine;<li><b>public String getServletName&lpar;&rpar;<&sol;b> returns the name of the servlet object&period;<&sol;li>&NewLine;<li><b>public void log&lpar;String msg&rpar; <&sol;b>writes the given message in the servlet log file&period;<&sol;li>&NewLine;<li><b>public void log&lpar;String msg&comma;Throwable t&rpar;<&sol;b> writes the explanatory message in the servlet log file and a stack trace&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2>Difference between <i><b>HttpServlet <&sol;b><&sol;i>and <i><b>GenericServlet<&sol;b><&sol;i>&&num;8211&semi;<&sol;h2>&NewLine;<h3><b>javax&period;servlet&period;GenericServlet<&sol;b><&sol;h3>&NewLine;<p>Signature&colon; <i><b>public abstract class GenericServlet extends java&period;lang&period;Object implements Servlet&comma; ServletConfig&comma; java&period;io&period;Serializable<&sol;b><&sol;i><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><i>GenericServlet <&sol;i><&sol;b>defines a generic&comma; protocol-independent servlet&period;<&sol;li>&NewLine;<li><i><b>GenericServlet <&sol;b><&sol;i>gives a blueprint and makes writing servlet easier&period;<&sol;li>&NewLine;<li><i><b>GenericServlet <&sol;b><&sol;i>provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface&period;<&sol;li>&NewLine;<li><b><i>GenericServlet <&sol;i><&sol;b>implements the log method&comma; declared in the ServletContext interface&period;<&sol;li>&NewLine;<li>To write a generic servlet&comma; it is sufficient to override the abstract service method&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><b>javax&period;servlet&period;http&period;HttpServlet<&sol;b><&sol;h3>&NewLine;<p>Signature&colon; <i><b>public abstract class HttpServlet extends GenericServlet implements java&period;io&period;Serializable<&sol;b><&sol;i><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><i>HttpServlet <&sol;i><&sol;b>defines a HTTP protocol specific servlet&period;<&sol;li>&NewLine;<li><i><b>HttpServlet <&sol;b><&sol;i>gives a blueprint for Http servlet and makes writing them easier&period;<&sol;li>&NewLine;<li><b><i>HttpServlet <&sol;i><&sol;b>extends the <b><i>GenericServlet <&sol;i><&sol;b>and hence inherits the properties <i><b>GenericServlet<&sol;b><&sol;i>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2>Servlet Example by inheriting the <i><b>GenericServlet <&sol;b><&sol;i>class-<&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the simple example of servlet by inheriting the GenericServlet class&period;<&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;io&period;&ast;&semi; &NewLine; import javax&period;servlet&period;&ast;&semi; &NewLine; &NewLine; public class HelloServlet extends GenericServlet&lbrace; &NewLine; public void service&lpar;ServletRequest req&comma;ServletResponse res&rpar; &NewLine; throws IOException&comma;ServletException&lbrace; &NewLine; &NewLine; res&period;setContentType&lpar;"text&sol;html"&rpar;&semi; &NewLine; &NewLine; PrintWriter out&equals;res&period;getWriter&lpar;&rpar;&semi; &NewLine; out&period;print&lpar;"&lt&semi;html&gt&semi;&lt&semi;body&gt&semi;"&rpar;&semi; &NewLine; out&period;print&lpar;"&lt&semi;b&gt&semi;hello generic servlet&lt&semi;&sol;b&gt&semi;"&rpar;&semi; &NewLine; out&period;print&lpar;"&lt&semi;&sol;body&gt&semi;&lt&semi;&sol;html&gt&semi;"&rpar;&semi; &NewLine; &NewLine; &rcub; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-form-data-basics-of-web&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-servlets-overview&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;class-httpservlet&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><b>Servlet Related Posts<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-servlets-overview&sol;">Java Servlets Overview<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlet-life-cycle&sol;">Servlet Life Cycle<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hello-world-example-in-servlet-interface&sol;">Servlet Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-servletconfig-and-cervletcontext-in-java-servlet&sol; "> Difference between ServletConfig and ServletContext<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-genericservlet-and&sol;"> Difference between GenericServlet and HttpServlet<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;what-is-web-application-servlet&sol;">What is web application&quest;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;advantages-of-servlets-over-cgi&sol;">Advantages of Servlets over CGI<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-genericservlet-and&sol;">GenericServlet Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestdispatcher-interface&sol;">RequestDispatcher Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servletconfig-interface&sol;">ServletConfig<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servletcontext-interface&sol;">ServletContext <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlet-filters&sol;">Servlet Filter Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-database-access&sol;">Database Access Example using Sevlet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlet-upload-file-example&sol;">File Uploading Example using Servlet<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-form-data-basics-of-web&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;class-httpservlet&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '268'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago