{"id":255,"date":"2018-07-27T06:12:11","date_gmt":"2018-07-27T06:12:11","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=255"},"modified":"2018-08-02T08:31:00","modified_gmt":"2018-08-02T08:31:00","slug":"java-servlets-introduction","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/java-servlets-introduction\/","title":{"rendered":"Java Servlets &#8211; Introduction"},"content":{"raw":"<div>\r\n\r\n<strong>\u00a0 \u00a0 Server-side Programming<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The programs that are written and executed in the server side are called Server-side programs. The Server-side program receives the data from the Web Server, processes the data and returns the output back to the web Server which will be given to the Web Browser. The Web Browser receives the data and presents the data on the document. The Server- side programs can be written in any language and the current technologies include ASP, JSP and soon.<\/p>\r\n&nbsp;\r\n\r\n<strong>What is Servlet?<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Servlet is like an Applet, but executes on the server side. Servlets are platform independent server-side software components. It is written in Java, which dynamically extends the functionality of a server. A servlet executes on a Java-enabled server.<\/p>\r\n&nbsp;\r\n\r\n<strong>How does it work?<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe steps involved in the working of a Servlet are:\r\n\r\n&nbsp;\r\n\r\nStep 1: Client sends a request to server.\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Step 2: Server starts a Servlet.<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Step 3: Servlet computes a result for server and does not quit.<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Step 4: Server returns response to client.<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Step 5: Another client sends a request.<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Step 6: Server calls the Servlet again.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Servlets vs. CGI scripts<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Advantages:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">CGI means every time there's client request, HTTP server creates new instance of process to serve this request. Running a Servlet does not require creating a separate process each time. A servlet stays in memory, so it doesn\u2019t have to be reloaded each time. There is only one instance handling multiple requests, not a separate instance for every request. If untrusted servlets, they can run in a \u201csandbox\u201d.<\/p>\r\n&nbsp;\r\n\r\n<strong>Disadvantage:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There is less choice of languages, when compared to CGI scripts. CGI scripts can be in any language.<\/p>\r\n&nbsp;\r\n\r\n<strong>Tomcat<\/strong>\r\n\r\n&nbsp;\r\n\r\nTomcat is the Servlet Engine than handles Servlet requests for Apache. Tomcat is a \u201chelper application\u201d for Apache. It acts as a \u201cservlet container\u201d. Apache can handle many types of web services. Apache can be installed without Tomcat. Tomcat can be installed without Apache. It is easier to install Tomcat standalone than as part of Apache. By itself, Tomcat can handle web pages, servlets, and JSP. Apache and Tomcat are open source (and therefore free) software .\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>The Servlet API<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">To build Servlets, we need two packages in Java that contain the classes and interfaces.<\/p>\r\n<p style=\"text-align: justify\"><strong>javax.servlet<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong>javax.servlet.http<\/strong><\/p>\r\n<p style=\"text-align: justify\">These packages are not part of Java core packages, so they are not included in the Java Software Development Kit.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Servlet API consists of packages such as <strong>javax.servlet<\/strong> and <strong>javax.servlet.http<\/strong>.<\/p>\r\n<p style=\"text-align: justify\">1.\u00a0\u00a0<strong>javax.servlet <\/strong>package contains interfaces like:<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\"><strong>Servlet<\/strong> Interface \u2013 Declares life cycle methods for a servlet.<\/li>\r\n \t<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletRequest <\/strong><span style=\"font-size: 1em\">Interface \u2013 Used to read data from a client request.<\/span><\/li>\r\n \t<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletResponse <\/strong><span style=\"font-size: 1em\">Interface \u2013 Used to write data to a client response.<\/span><\/li>\r\n \t<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletContext <\/strong><span style=\"font-size: 1em\">Interface \u2013 Enables servlets to log events and access information about their environment.<\/span><\/li>\r\n \t<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletConfig <\/strong><span style=\"font-size: 1em\">Interface \u2013 Allows servlets to get initialization parameters.<\/span><\/li>\r\n<\/ul>\r\n<\/div>\r\n<ol style=\"text-align: justify\" start=\"2\">\r\n \t<li><strong>javax.servlet.http <\/strong>package contains interfaces like:<\/li>\r\n<\/ol>\r\n<ul style=\"text-align: justify\">\r\n \t<li><strong>HttpServletRequest <\/strong>Interface \u2013 Enables servlets to read data from an HTTP request<\/li>\r\n \t<li><strong>HttpServletResponse <\/strong>Interface \u2013 Enables servlets to write data to an HTTP response.<\/li>\r\n \t<li><strong>HttpSession <\/strong>Interface \u2013 Allows session data to be read and written<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><strong>\u00a0 \u00a0 \u00a0The Servlet Interface<\/strong><\/p>\r\n&nbsp;\r\n\r\nA servlet is any class that implements the <strong>javax.servlet.Servlet<\/strong> interface. The API provides two abstract classes that implement the Servlet interface called <strong>GenericServlet<\/strong> and <strong>HttpServlet<\/strong>.\r\n\r\n&nbsp;\r\n\r\nIn practice, most servlets extend the javax.servlet.http.HttpServlet class. Some servlets extend javax.servlet.GenericServlet instead.\r\n\r\n&nbsp;\r\n\r\nThe Servlet Interface consists of the following methods such as:\r\n<ul>\r\n \t<li><strong>public void init(ServletConfig)<\/strong><\/li>\r\n \t<li><strong>public void service(request, response)<\/strong><\/li>\r\n \t<li><strong>public void destroy()<\/strong><\/li>\r\n \t<li><strong>public ServletConfig getServletConfig()<\/strong><\/li>\r\n \t<li><strong>public String getServletInfo()<\/strong><\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><strong><em>\u00a0 \u00a0 Note:<\/em><\/strong> Servlets, like Applets, usually lack a main method, but must implement or override certain other methods.<\/p>\r\n&nbsp;\r\n\r\n<strong>The ServletRequest Interface<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The various methods defined in the ServletRequest Interface are shown in Table 28.1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-256 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150.png\" alt=\"\" width=\"635\" height=\"354\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>The ServletResponse Interface<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe various methods defined in the ServletResponse interface are shown in Table 28.2.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-257 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151.png\" alt=\"\" width=\"605\" height=\"260\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>The ServletContext Interface<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe various methods defined in the ServletContext interface are shown in Table 28.3.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-258 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152.png\" alt=\"\" width=\"638\" height=\"381\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>The HttpServletRequest Interface<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe various methods defined in the HttpServletRequest interface are shown in Table 28.4.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-259 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153.png\" alt=\"\" width=\"630\" height=\"288\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>The HttpServletResponse Interface<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe various methods defined in the HttpServletResponseinterface are shown in Table 28.5.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-260 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154.png\" alt=\"\" width=\"451\" height=\"505\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>The HttpSession Interface<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe various methods defined in the <strong>HttpSession<\/strong> interface are shown in Table 28.6.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-261 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155.png\" alt=\"\" width=\"601\" height=\"401\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 GenericServlet<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A GenericServlet defines a generic, protocol-independent Servlet. It gives a blueprint and makes writing Servlet easier. GenericServlet provides simple versions of the lifecycle methods called init() and destroy() and of the methods in the ServletConfig interface. To write a generic servlet, it is sufficient to override the abstract service method.public abstract void <a href=\"https:\/\/docs.oracle.com\/cd\/E17802_01\/products\/products\/servlet\/2.1\/api\/javax.servlet.ServletRequest.html\">service(ServletRequest <\/a>req, <a href=\"https:\/\/docs.oracle.com\/cd\/E17802_01\/products\/products\/servlet\/2.1\/api\/javax.servlet.ServletResponse.html\">ServletResponse <\/a>res) throws <a href=\"https:\/\/docs.oracle.com\/cd\/E17802_01\/products\/products\/servlet\/2.1\/api\/javax.servlet.ServletException.html\">ServletException, IOException<\/a>.Here ServletRequest and ServletResponse provides access to generic server requests and response.<\/p>\r\n&nbsp;\r\n\r\n<strong>HttpServlet<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A HttpServlet class is extended to add HTTP-specific functionality to Servlets. It is an abstract class that extends GenericServlet class. Web-based servlets typically extend the class HttpServlet.<span style=\"text-align: initial;font-size: 1em\">HttpServlet overrides the service()method and defines other methods like doGet() and doPost(). It provides additional methods that are called by the service() method automatically. Both methods throw ServletException and IOException.<\/span><span style=\"text-align: initial;font-size: 1em\">Methods of most concern in this class are:<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<ul>\r\n \t<li>protected void doGet(HttpServletRequest, HttpServletResponse)<\/li>\r\n \t<li>protected void doPost(HttpServletRequest, HttpServletResponse)<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 Life Cycle of Servlet<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Servlet lifecycle starts with the initiation of the Servlet where an instance of a Servlet is created. Then the Servlet is initialized with the control parameters in the init() method. When the servlet has a request the service() method is called where the client request is processed. When the Web container is closed, the servlets are destroyed by calling the destroy() method and the Servlet is marked for garbage collection.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0<strong>Instantiation <\/strong>\u2013 Web container creates an instance.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0 <strong>Initialization <\/strong>\u2013 container calls the instance\u2019s<strong> init()<\/strong>. It initializes a Servlet.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0<strong>Service <\/strong>\u2013 If container has request, then it calls the<strong> service() <\/strong>method. This method processes a client\u2019s request.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0<strong>Destroy <\/strong>\u2013 before reclaiming the memory (destroying the servlet), the container calls the<strong> destroy() <\/strong>method. This method terminates a Servlet.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0<strong>Unavailable <\/strong>\u2013 instance is destroyed and marked for GC.<\/p>\r\n<p style=\"text-align: justify\"><strong>Description of the Methods<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong>init() Method<\/strong><\/p>\r\n<p style=\"text-align: justify\">When a servlet is first started up, its <strong>init(ServletConfig config)<\/strong> method is called.<\/p>\r\n<p style=\"text-align: justify\"><strong>public void init(ServletConfig config) throws ServletException<\/strong><\/p>\r\n<p style=\"text-align: justify\">This method is called once when the Servlet is loaded into the Servlet engine, before the Servlet is asked to process its first request and it should perform any necessary initializations.<\/p>\r\n<p style=\"text-align: justify\"><strong>service() Method<\/strong><\/p>\r\n<p style=\"text-align: justify\">Every servlet request results in a call to <strong>service(ServletRequest request, ServletResponse response).<\/strong><\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><strong>\u00a0 \u00a0public void service(ServletRequest request, ServletResponse response)<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong>throws ServletException, IOException<\/strong><\/p>\r\n<p style=\"text-align: justify\">This method is called to process a request. It can be called zero, one or many times until the servlet is unloaded. Multiple threads (one per request) can execute this method in parallel so it must be thread safe.<\/p>\r\n<p style=\"text-align: justify\"><strong>destroy() Method<\/strong><\/p>\r\n<p style=\"text-align: justify\">When the servlet is shut down, <strong>destroy()<\/strong> is called.<\/p>\r\n<p style=\"text-align: justify\"><strong>public void destroy()<\/strong><\/p>\r\n<p style=\"text-align: justify\">This method is called once just before the Servlet is unloaded and taken out of service.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Architecture Diagram<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">First the HTTP requests coming to the server are delegated to the Servlet container. The Servlet container loads the Servlet before invoking the <strong>service()<\/strong> method. Then the Servlet container handles multiple requests by spawning multiple threads, each thread executing the <strong>service()<\/strong> method of a single instance of the Servlet. This is shown in Figure 28.1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-262 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156.png\" alt=\"\" width=\"424\" height=\"415\" \/><\/p>\r\n<strong>HTTP Requests<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When a request is submitted from a Web page, it is almost always a <strong>GET<\/strong> or a <strong>POST<\/strong> request.<\/p>\r\n<p style=\"text-align: justify\">The HTTP &lt;<strong>form<\/strong>&gt; tag has an attribute <strong>action<\/strong>, whose value can be <strong>\"get\"<\/strong> or <strong>\"post\".<\/strong> The <strong>\"get\"<\/strong> action results in the form information being put after a '?' in the URL.<\/p>\r\n&nbsp;\r\n\r\n<strong>HTTP Requests<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When a request is submitted from a Web page, it is almost always a <strong>GET<\/strong> or a <strong>POST<\/strong> request.<\/p>\r\n<p style=\"text-align: justify\">The HTTP &lt;<strong>form<\/strong>&gt; tag has an attribute <strong>action<\/strong>, whose value can be <strong>\"get\"<\/strong> or <strong>\"post\".<\/strong> The <strong>\"get\"<\/strong> action results in the form information being put after a '?' in the URL.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nhttp:\/\/www.google.com\/search?hl=en&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=servlets\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The &amp; separates the various parameters. Only a limited amount of information can be sent this way whereas <strong>\"put\"<\/strong> can send large amounts of information.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The service method dispatches the following kinds of requests such as DELETE, GET, HEAD, OPTIONS, POST, PUT, and TRACE.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A GET request is dispatched to the doGet(HttpServletRequest request, HttpServletResponse response) method.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A POST request is dispatched to the doPost(HttpServletRequest request, HttpServletResponse response) method.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">These are the two methods we will usually override. Methods doGet and doPost typically do the same thing, so usually you do the real work in one, and have the other just call it.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">public void doGet(HttpServletRequest request, HttpServletResponse response) {<\/p>\r\n<p style=\"text-align: justify\">doPost(request, response);<\/p>\r\n<p style=\"text-align: justify\">}<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Summary<\/strong><\/p>\r\n<p style=\"text-align: justify\">In this module, we have discussed about the purpose of Servlets used to create web applications. This section also explains about the architecture and lifecycle of Servlets. Further, it gives an insight about the Servlet API, that provides many interfaces and classes, which is deployed on the server to create dynamic web pages.<\/p>\r\n<p style=\"text-align: justify\">http:\/\/www.google.com\/search?hl=en&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=servlets<\/p>\r\n<p style=\"text-align: justify\">The &amp; separates the various parameters. Only a limited amount of information can be sent this way whereas <strong>\"put\"<\/strong> can send large amounts of information.<\/p>\r\n<p style=\"text-align: justify\">The service method dispatches the following kinds of requests such as DELETE, GET, HEAD, OPTIONS, POST, PUT, and TRACE.<\/p>\r\n<p style=\"text-align: justify\">A GET request is dispatched to the doGet(HttpServletRequest request, HttpServletResponse response) method.<\/p>\r\n<p style=\"text-align: justify\">A POST request is dispatched to the doPost(HttpServletRequest request, HttpServletResponse response) method.<\/p>\r\n<p style=\"text-align: justify\">These are the two methods we will usually override. Methods doGet and doPost typically do the same thing, so usually you do the real work in one, and have the other just call it.<\/p>\r\n<p style=\"text-align: justify\">public void doGet(HttpServletRequest request, HttpServletResponse response) {<\/p>\r\n<p style=\"text-align: justify\">doPost(request, response);<\/p>\r\n<p style=\"text-align: justify\">}<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Summary<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module, we have discussed about the purpose of Servlets used to create web applications. This section also explains about the architecture and lifecycle of Servlets. Further, it gives an insight about the Servlet API, that provides many interfaces and classes, which is deployed on the server to create dynamic web pages.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Web Links<\/strong><\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li><a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/\"><em>http:\/\/java.sun.com<\/em><\/a><\/li>\r\n \t<li><em>https:\/\/pawangkp.files.wordpress.com\/2009\/10\/deawsj-6_<strong>ppt<\/strong>_1b.<strong>ppt<\/strong><\/em><\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><\/p>","rendered":"<div>\n<p><strong>\u00a0 \u00a0 Server-side Programming<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The programs that are written and executed in the server side are called Server-side programs. The Server-side program receives the data from the Web Server, processes the data and returns the output back to the web Server which will be given to the Web Browser. The Web Browser receives the data and presents the data on the document. The Server- side programs can be written in any language and the current technologies include ASP, JSP and soon.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>What is Servlet?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Servlet is like an Applet, but executes on the server side. Servlets are platform independent server-side software components. It is written in Java, which dynamically extends the functionality of a server. A servlet executes on a Java-enabled server.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How does it work?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The steps involved in the working of a Servlet are:<\/p>\n<p>&nbsp;<\/p>\n<p>Step 1: Client sends a request to server.<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Step 2: Server starts a Servlet.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Step 3: Servlet computes a result for server and does not quit.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Step 4: Server returns response to client.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Step 5: Another client sends a request.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Step 6: Server calls the Servlet again.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 Servlets vs. CGI scripts<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Advantages:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">CGI means every time there&#8217;s client request, HTTP server creates new instance of process to serve this request. Running a Servlet does not require creating a separate process each time. A servlet stays in memory, so it doesn\u2019t have to be reloaded each time. There is only one instance handling multiple requests, not a separate instance for every request. If untrusted servlets, they can run in a \u201csandbox\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Disadvantage:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There is less choice of languages, when compared to CGI scripts. CGI scripts can be in any language.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Tomcat<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Tomcat is the Servlet Engine than handles Servlet requests for Apache. Tomcat is a \u201chelper application\u201d for Apache. It acts as a \u201cservlet container\u201d. Apache can handle many types of web services. Apache can be installed without Tomcat. Tomcat can be installed without Apache. It is easier to install Tomcat standalone than as part of Apache. By itself, Tomcat can handle web pages, servlets, and JSP. Apache and Tomcat are open source (and therefore free) software .<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>The Servlet API<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To build Servlets, we need two packages in Java that contain the classes and interfaces.<\/p>\n<p style=\"text-align: justify\"><strong>javax.servlet<\/strong><\/p>\n<p style=\"text-align: justify\"><strong>javax.servlet.http<\/strong><\/p>\n<p style=\"text-align: justify\">These packages are not part of Java core packages, so they are not included in the Java Software Development Kit.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Servlet API consists of packages such as <strong>javax.servlet<\/strong> and <strong>javax.servlet.http<\/strong>.<\/p>\n<p style=\"text-align: justify\">1.\u00a0\u00a0<strong>javax.servlet <\/strong>package contains interfaces like:<\/p>\n<ul>\n<li style=\"text-align: justify\"><strong>Servlet<\/strong> Interface \u2013 Declares life cycle methods for a servlet.<\/li>\n<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletRequest <\/strong><span style=\"font-size: 1em\">Interface \u2013 Used to read data from a client request.<\/span><\/li>\n<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletResponse <\/strong><span style=\"font-size: 1em\">Interface \u2013 Used to write data to a client response.<\/span><\/li>\n<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletContext <\/strong><span style=\"font-size: 1em\">Interface \u2013 Enables servlets to log events and access information about their environment.<\/span><\/li>\n<li style=\"text-align: justify\"><strong style=\"font-size: 1em\">ServletConfig <\/strong><span style=\"font-size: 1em\">Interface \u2013 Allows servlets to get initialization parameters.<\/span><\/li>\n<\/ul>\n<\/div>\n<ol style=\"text-align: justify\" start=\"2\">\n<li><strong>javax.servlet.http <\/strong>package contains interfaces like:<\/li>\n<\/ol>\n<ul style=\"text-align: justify\">\n<li><strong>HttpServletRequest <\/strong>Interface \u2013 Enables servlets to read data from an HTTP request<\/li>\n<li><strong>HttpServletResponse <\/strong>Interface \u2013 Enables servlets to write data to an HTTP response.<\/li>\n<li><strong>HttpSession <\/strong>Interface \u2013 Allows session data to be read and written<\/li>\n<\/ul>\n<p style=\"text-align: justify\"><strong>\u00a0 \u00a0 \u00a0The Servlet Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A servlet is any class that implements the <strong>javax.servlet.Servlet<\/strong> interface. The API provides two abstract classes that implement the Servlet interface called <strong>GenericServlet<\/strong> and <strong>HttpServlet<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>In practice, most servlets extend the javax.servlet.http.HttpServlet class. Some servlets extend javax.servlet.GenericServlet instead.<\/p>\n<p>&nbsp;<\/p>\n<p>The Servlet Interface consists of the following methods such as:<\/p>\n<ul>\n<li><strong>public void init(ServletConfig)<\/strong><\/li>\n<li><strong>public void service(request, response)<\/strong><\/li>\n<li><strong>public void destroy()<\/strong><\/li>\n<li><strong>public ServletConfig getServletConfig()<\/strong><\/li>\n<li><strong>public String getServletInfo()<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify\"><strong><em>\u00a0 \u00a0 Note:<\/em><\/strong> Servlets, like Applets, usually lack a main method, but must implement or override certain other methods.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The ServletRequest Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The various methods defined in the ServletRequest Interface are shown in Table 28.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-256 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150.png\" alt=\"\" width=\"635\" height=\"354\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150.png 635w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150-300x167.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150-225x125.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-150-350x195.png 350w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The ServletResponse Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The various methods defined in the ServletResponse interface are shown in Table 28.2.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-257 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151.png\" alt=\"\" width=\"605\" height=\"260\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151.png 605w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151-300x129.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151-225x97.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-151-350x150.png 350w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The ServletContext Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The various methods defined in the ServletContext interface are shown in Table 28.3.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-258 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152.png\" alt=\"\" width=\"638\" height=\"381\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152.png 638w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-152-350x209.png 350w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The HttpServletRequest Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The various methods defined in the HttpServletRequest interface are shown in Table 28.4.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-259 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153.png\" alt=\"\" width=\"630\" height=\"288\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153.png 630w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153-300x137.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153-65x30.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153-225x103.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-153-350x160.png 350w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The HttpServletResponse Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The various methods defined in the HttpServletResponseinterface are shown in Table 28.5.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-260 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154.png\" alt=\"\" width=\"451\" height=\"505\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154.png 451w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154-268x300.png 268w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154-65x73.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154-225x252.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-154-350x392.png 350w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The HttpSession Interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The various methods defined in the <strong>HttpSession<\/strong> interface are shown in Table 28.6.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-261 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155.png\" alt=\"\" width=\"601\" height=\"401\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155.png 601w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155-300x200.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155-225x150.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-155-350x234.png 350w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 GenericServlet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A GenericServlet defines a generic, protocol-independent Servlet. It gives a blueprint and makes writing Servlet easier. GenericServlet provides simple versions of the lifecycle methods called init() and destroy() and of the methods in the ServletConfig interface. To write a generic servlet, it is sufficient to override the abstract service method.public abstract void <a href=\"https:\/\/docs.oracle.com\/cd\/E17802_01\/products\/products\/servlet\/2.1\/api\/javax.servlet.ServletRequest.html\">service(ServletRequest <\/a>req, <a href=\"https:\/\/docs.oracle.com\/cd\/E17802_01\/products\/products\/servlet\/2.1\/api\/javax.servlet.ServletResponse.html\">ServletResponse <\/a>res) throws <a href=\"https:\/\/docs.oracle.com\/cd\/E17802_01\/products\/products\/servlet\/2.1\/api\/javax.servlet.ServletException.html\">ServletException, IOException<\/a>.Here ServletRequest and ServletResponse provides access to generic server requests and response.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>HttpServlet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A HttpServlet class is extended to add HTTP-specific functionality to Servlets. It is an abstract class that extends GenericServlet class. Web-based servlets typically extend the class HttpServlet.<span style=\"text-align: initial;font-size: 1em\">HttpServlet overrides the service()method and defines other methods like doGet() and doPost(). It provides additional methods that are called by the service() method automatically. Both methods throw ServletException and IOException.<\/span><span style=\"text-align: initial;font-size: 1em\">Methods of most concern in this class are:<\/span><\/p>\n<\/div>\n<div>\n<ul>\n<li>protected void doGet(HttpServletRequest, HttpServletResponse)<\/li>\n<li>protected void doPost(HttpServletRequest, HttpServletResponse)<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 Life Cycle of Servlet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Servlet lifecycle starts with the initiation of the Servlet where an instance of a Servlet is created. Then the Servlet is initialized with the control parameters in the init() method. When the servlet has a request the service() method is called where the client request is processed. When the Web container is closed, the servlets are destroyed by calling the destroy() method and the Servlet is marked for garbage collection.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0<strong>Instantiation <\/strong>\u2013 Web container creates an instance.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0 <strong>Initialization <\/strong>\u2013 container calls the instance\u2019s<strong> init()<\/strong>. It initializes a Servlet.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0<strong>Service <\/strong>\u2013 If container has request, then it calls the<strong> service() <\/strong>method. This method processes a client\u2019s request.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0<strong>Destroy <\/strong>\u2013 before reclaiming the memory (destroying the servlet), the container calls the<strong> destroy() <\/strong>method. This method terminates a Servlet.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0<strong>Unavailable <\/strong>\u2013 instance is destroyed and marked for GC.<\/p>\n<p style=\"text-align: justify\"><strong>Description of the Methods<\/strong><\/p>\n<p style=\"text-align: justify\"><strong>init() Method<\/strong><\/p>\n<p style=\"text-align: justify\">When a servlet is first started up, its <strong>init(ServletConfig config)<\/strong> method is called.<\/p>\n<p style=\"text-align: justify\"><strong>public void init(ServletConfig config) throws ServletException<\/strong><\/p>\n<p style=\"text-align: justify\">This method is called once when the Servlet is loaded into the Servlet engine, before the Servlet is asked to process its first request and it should perform any necessary initializations.<\/p>\n<p style=\"text-align: justify\"><strong>service() Method<\/strong><\/p>\n<p style=\"text-align: justify\">Every servlet request results in a call to <strong>service(ServletRequest request, ServletResponse response).<\/strong><\/p>\n<\/div>\n<p style=\"text-align: justify\"><strong>\u00a0 \u00a0public void service(ServletRequest request, ServletResponse response)<\/strong><\/p>\n<p style=\"text-align: justify\"><strong>throws ServletException, IOException<\/strong><\/p>\n<p style=\"text-align: justify\">This method is called to process a request. It can be called zero, one or many times until the servlet is unloaded. Multiple threads (one per request) can execute this method in parallel so it must be thread safe.<\/p>\n<p style=\"text-align: justify\"><strong>destroy() Method<\/strong><\/p>\n<p style=\"text-align: justify\">When the servlet is shut down, <strong>destroy()<\/strong> is called.<\/p>\n<p style=\"text-align: justify\"><strong>public void destroy()<\/strong><\/p>\n<p style=\"text-align: justify\">This method is called once just before the Servlet is unloaded and taken out of service.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Architecture Diagram<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">First the HTTP requests coming to the server are delegated to the Servlet container. The Servlet container loads the Servlet before invoking the <strong>service()<\/strong> method. Then the Servlet container handles multiple requests by spawning multiple threads, each thread executing the <strong>service()<\/strong> method of a single instance of the Servlet. This is shown in Figure 28.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-262 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156.png\" alt=\"\" width=\"424\" height=\"415\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156.png 424w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156-300x294.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156-65x64.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156-225x220.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-156-350x343.png 350w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/p>\n<p><strong>HTTP Requests<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When a request is submitted from a Web page, it is almost always a <strong>GET<\/strong> or a <strong>POST<\/strong> request.<\/p>\n<p style=\"text-align: justify\">The HTTP &lt;<strong>form<\/strong>&gt; tag has an attribute <strong>action<\/strong>, whose value can be <strong>&#8220;get&#8221;<\/strong> or <strong>&#8220;post&#8221;.<\/strong> The <strong>&#8220;get&#8221;<\/strong> action results in the form information being put after a &#8216;?&#8217; in the URL.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>HTTP Requests<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When a request is submitted from a Web page, it is almost always a <strong>GET<\/strong> or a <strong>POST<\/strong> request.<\/p>\n<p style=\"text-align: justify\">The HTTP &lt;<strong>form<\/strong>&gt; tag has an attribute <strong>action<\/strong>, whose value can be <strong>&#8220;get&#8221;<\/strong> or <strong>&#8220;post&#8221;.<\/strong> The <strong>&#8220;get&#8221;<\/strong> action results in the form information being put after a &#8216;?&#8217; in the URL.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>http:\/\/www.google.com\/search?hl=en&#38;ie=UTF-8&#38;oe=UTF-8&#38;q=servlets<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The &amp; separates the various parameters. Only a limited amount of information can be sent this way whereas <strong>&#8220;put&#8221;<\/strong> can send large amounts of information.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The service method dispatches the following kinds of requests such as DELETE, GET, HEAD, OPTIONS, POST, PUT, and TRACE.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A GET request is dispatched to the doGet(HttpServletRequest request, HttpServletResponse response) method.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A POST request is dispatched to the doPost(HttpServletRequest request, HttpServletResponse response) method.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">These are the two methods we will usually override. Methods doGet and doPost typically do the same thing, so usually you do the real work in one, and have the other just call it.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">public void doGet(HttpServletRequest request, HttpServletResponse response) {<\/p>\n<p style=\"text-align: justify\">doPost(request, response);<\/p>\n<p style=\"text-align: justify\">}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Summary<\/strong><\/p>\n<p style=\"text-align: justify\">In this module, we have discussed about the purpose of Servlets used to create web applications. This section also explains about the architecture and lifecycle of Servlets. Further, it gives an insight about the Servlet API, that provides many interfaces and classes, which is deployed on the server to create dynamic web pages.<\/p>\n<p style=\"text-align: justify\">http:\/\/www.google.com\/search?hl=en&#38;ie=UTF-8&#38;oe=UTF-8&#38;q=servlets<\/p>\n<p style=\"text-align: justify\">The &amp; separates the various parameters. Only a limited amount of information can be sent this way whereas <strong>&#8220;put&#8221;<\/strong> can send large amounts of information.<\/p>\n<p style=\"text-align: justify\">The service method dispatches the following kinds of requests such as DELETE, GET, HEAD, OPTIONS, POST, PUT, and TRACE.<\/p>\n<p style=\"text-align: justify\">A GET request is dispatched to the doGet(HttpServletRequest request, HttpServletResponse response) method.<\/p>\n<p style=\"text-align: justify\">A POST request is dispatched to the doPost(HttpServletRequest request, HttpServletResponse response) method.<\/p>\n<p style=\"text-align: justify\">These are the two methods we will usually override. Methods doGet and doPost typically do the same thing, so usually you do the real work in one, and have the other just call it.<\/p>\n<p style=\"text-align: justify\">public void doGet(HttpServletRequest request, HttpServletResponse response) {<\/p>\n<p style=\"text-align: justify\">doPost(request, response);<\/p>\n<p style=\"text-align: justify\">}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module, we have discussed about the purpose of Servlets used to create web applications. This section also explains about the architecture and lifecycle of Servlets. Further, it gives an insight about the Servlet API, that provides many interfaces and classes, which is deployed on the server to create dynamic web pages.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Web Links<\/strong><\/p>\n<ul style=\"text-align: justify\">\n<li><a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/\"><em>http:\/\/java.sun.com<\/em><\/a><\/li>\n<li><em>https:\/\/pawangkp.files.wordpress.com\/2009\/10\/deawsj-6_<strong>ppt<\/strong>_1b.<strong>ppt<\/strong><\/em><\/li>\n<\/ul>\n<p style=\"text-align: justify\">\n","protected":false},"author":4,"menu_order":26,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-255","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/255\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/255\/revisions\/589"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/255\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=255"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=255"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}