Categories: Servlet

Hello World Example in Servlet Interface

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