Categories: Servlet

Servlet Tutorial – Java Servlets Overview

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In this servlet tutorial we will see examples of servlet&comma; Java Servlets is a web technology for Java&period; It was the first web technology for Java and many new web technologies have arrived since&period; Still&comma; Java Servlets are very useful&comma; both to know&comma; and for certain use cases&period;<&sol;p>&NewLine;<p>Java Servlets are part of the Java Enterprise Edition &lpar;Java EE&rpar;&period; You will need to run your Java Servlets inside a Servlet compatible &&num;8220&semi;Servlet Container&&num;8221&semi; &lpar;e&period;g&period; web server&rpar; in order for them to work&period;<&sol;p>&NewLine;<p>Java Servlets it Java technology for creating the dynamic web applications&period; Java Servlets are server side components in Java that runs on Servlet enabled web server such as Tomcat&comma; Jetty&comma; WebSphere etc&period;&period; Java Servlet is much faster then CGI and Perl since it runs in the same JVM&period; In case of CGI and Perl separate memory space is allocated for execution of the program which reduces it&&num;8217&semi;s performance&period;<&sol;p>&NewLine;<p>Servlet technology is robust and scalable as it uses the java language&period; Before Servlet&comma; CGI &lpar;Common Gateway Interface&rpar; scripting language was used as a server-side programming language&period; But there were many disadvantages of this technology&period; We have discussed these disadvantages below&period;<&sol;p>&NewLine;<p>There are many interfaces and classes in the servlet API such as <b>Servlet<&sol;b>&comma; <b>GenericServlet<&sol;b>&comma; <b>HttpServlet<&sol;b>&comma; <b>ServletRequest<&sol;b>&comma; <b>ServletResponse <&sol;b>etc&period;<&sol;p>&NewLine;<h2><i><b>What is a Servlet&quest;<&sol;b><&sol;i><&sol;h2>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Servlet can be described in many ways&comma; depending on the context&period;<&sol;li>&NewLine;<li>Servlet is a technology i&period;e&period; used to create web application&period;<&sol;li>&NewLine;<li>Servlet is an API that provides many interfaces and classes including documentations&period;<&sol;li>&NewLine;<li>Servlet is an interface that must be implemented for creating any servlet&period;<&sol;li>&NewLine;<li>Servlet is a class that extend the capabilities of the servers and respond to the incoming request&period; It can respond to any type of requests&period;<&sol;li>&NewLine;<li>Servlet is a web component that is deployed on the server to create dynamic web page&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img class&equals;"alignnone" title&equals;"Servlets Tutorial - Java Servlets Overview" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;12&sol;response&period;jpg" width&equals;"286" height&equals;"22" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Servlets Architecture&colon;<&sol;b><&sol;h2>&NewLine;<p>Following diagram shows the position of Servelts in a Web Application&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img title&equals;"Java Servlets Overview" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;12&sol;web-requestHandling-1&period;gif" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Servlets Tasks&colon;<&sol;b><&sol;h2>&NewLine;<p>Servlets perform the following major tasks&colon;<&sol;p>&NewLine;<p>Read the explicit data sent by the clients &lpar;browsers&rpar;&period; This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program&period;<&sol;p>&NewLine;<p>Read the implicit HTTP request data sent by the clients &lpar;browsers&rpar;&period; This includes cookies&comma; media types and compression schemes the browser understands&comma; and so forth&period;<&sol;p>&NewLine;<p>Process the data and generate the results&period; This process may require talking to a database&comma; executing an RMI or CORBA call&comma; invoking a Web service&comma; or computing the response directly&period;<&sol;p>&NewLine;<p>Send the explicit data &lpar;i&period;e&period;&comma; the document&rpar; to the clients &lpar;browsers&rpar;&period; This document can be sent in a variety of formats&comma; including text &lpar;HTML or XML&rpar;&comma; binary &lpar;GIF images&rpar;&comma; Excel&comma; etc&period;<&sol;p>&NewLine;<p>Send the implicit HTTP response to the clients &lpar;browsers&rpar;&period; This includes telling the browsers or other clients what type of document is being returned &lpar;e&period;g&period;&comma; HTML&rpar;&comma; setting cookies and caching parameters&comma; and other such tasks&period;<&sol;p>&NewLine;<h2><b>Servlets Packages&colon;<&sol;b><&sol;h2>&NewLine;<p>Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification&period;<&sol;p>&NewLine;<p>Servlets can be created using the javax&period;servlet and javax&period;servlet&period;http packages&comma; which are a standard part of the Java&&num;8217&semi;s enterprise edition&comma; an expanded version of the Java class library that supports large-scale development projects&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img class&equals;"alignnone" title&equals;"Servlets Tutorial" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;12&sol;hire&period;gif" width&equals;"113" height&equals;"22" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Servlet Tutorial Table of Contents<&sol;b><&sol;h2>&NewLine;<p>This Servlet Tutorial has a list of the topics covered in this Java Servlet trail&period; This list &lpar;menu&rpar; is also present at the top right of every page in the trail&period;<&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;" rel&equals;"noopener">Java Servlets Overview<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-environment-setup&sol;" rel&equals;"noopener">Servlets &&num;8211&semi; Environment Setup<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlet-life-cycle&sol;" rel&equals;"noopener">Servlet Life Cycle<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a class&equals;"GHUY-LPPB" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlet-api&sol;" rel&equals;"noopener">Servlet AP<&sol;a>I<&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;&num;&period;UqL4LeLpyxg" rel&equals;"noopener">Servlet Example<&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;" rel&equals;"noopener">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;" rel&equals;"noopener">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;servlets-form-data-basics-of-web&sol;" rel&equals;"noopener">Servlets &&num;8211&semi; Form Data<&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;" rel&equals;"noopener">GenericServlet <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;class-httpservlet&sol;" rel&equals;"noopener">HttpServlet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;httprequest&sol;" rel&equals;"noopener">HttpRequest<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;httpresponse-servlet&sol;" rel&equals;"noopener">HttpResponse<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestdispatcher-interface&sol;" rel&equals;"noopener">RequestDispatcher<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a class&equals;"GHUY-LPPB" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;send-redirect-in-servlet&sol;" rel&equals;"noopener">Send Redirect in Servlet<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;httpsession-interface&sol;" rel&equals;"noopener">HttpSession<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servletconfig-interface&sol;" rel&equals;"noopener">ServletConfig<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servletcontext-interface&sol;" rel&equals;"noopener">ServletContext<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;the-servlet-cookie-api&sol;" rel&equals;"noopener">Cookies Handling<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;cookies-in-session-tracking&sol;" rel&equals;"noopener">Session Tracking<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlet-filters&sol;" rel&equals;"noopener">Servlet Filter<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-database-access&sol;" rel&equals;"noopener">Database Access<&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;" rel&equals;"noopener">File Uploading<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-hits-counter&sol;" rel&equals;"noopener">Hits Counter<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;servlets-auto-page-refresh&sol;" rel&equals;"noopener">Auto Refresh<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sending-email-through-javamail-api-in&sol;" rel&equals;"noopener">Sending Email<&sol;a><&sol;b><&sol;li>&NewLine;<li>Packaging<&sol;li>&NewLine;<li>Debugging<&sol;li>&NewLine;<li>Servlet Internationalization<&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&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;jstl-fntouppercase-function&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;servlets-environment-setup&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&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; &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;servlets-environment-setup&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; '276'&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