{"id":34048,"date":"2014-12-12T16:00:25","date_gmt":"2014-12-12T14:00:25","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=34048"},"modified":"2023-12-05T16:28:55","modified_gmt":"2023-12-05T14:28:55","slug":"java-servlet-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html","title":{"rendered":"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)"},"content":{"rendered":"<p>Java Servlets is a Java based web technology. Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems.<\/p>\n<p>A servlet can almost be thought of as an applet that runs on the server side&#8211;without a face. Java servlets make many Web applications possible.<\/p>\n<p>Java Servlets comprise a fundamental part of the Java Enterprise Edition (Java EE). Please note that Java Servlets have to be executed inside a Servlet compatible &#8220;Servlet Container (e.g. web server) in order to work.<\/p>\n<p>This tutorial works as a comprehensive, kick-start guide for your Java Servlet based code.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Servlet Process\">1.1 Servlet Process<\/a><\/dt>\n<dt><a href=\"#Merits\">1.2 Merits<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Life Cycle\">2. Lifecycle<\/a><\/dt>\n<dt><a href=\"#Container\">3. Container<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Services\">3.1 Services<\/a><\/dt>\n<dt><a href=\"#Servlet Container Configurations\">3.2 Servlet Container Configurations<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Demo\">4. Demo: To start with<\/a><\/dt>\n<dt><a href=\"#Filter\">5. Filter<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Interface\">5.1 Interface<\/a><\/dt>\n<dt><a href=\"#Example\">5.2 Example<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Session\">6. Session<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Session Handling\">6.1 Session Handling<\/a><\/dt>\n<dt><a href=\"#Mechanisms of Session Handling\">6.2 Mechanism of Session Handling<\/a><\/dt>\n<dt><a href=\"#Example\">6.3 Example<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Exception Handling\">7. Exception Handling<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Error Code Configuration\">7.1 Error Code Configuration<\/a><\/dt>\n<dt><a href=\"#Exception-Type Configuration\">7.2 Exception- Type Configuration<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Debugging\">8. Debugging<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Message Logging\">8.1 Message Logging<\/a><\/dt>\n<dt><a href=\"#Java Debugger\">8.2 Java Debugger<\/a><\/dt>\n<dt><a href=\"#Headers\">8.3 Headers<\/a><\/dt>\n<dt><a href=\"#Refresh\">8.4 Refresh<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Internationalization\">9. Internationalization<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Methods\">9.1 Methods<\/a><\/dt>\n<dt><a href=\"#ExampleI\">9.2 Example<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#References\">10. References<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#Website\">10.1 Website<\/a><\/dt>\n<dt><a href=\"#Book\">10.2 Book<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#Conclusion\">11. Conclusion<\/a><\/dt>\n<dt><a href=\"#Download\">12. Download<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>Servlet is a Java programming language class, part of Java Enterprise Edition (Java EE). Sun Microsystems developed its first version 1.0 in the year 1997. Its current Version is Servlet 3.1.<\/p>\n<p>Servlets are used for creating dynamic web applications in java by extending the capability of a server. It can run on any web server integrated with a Servlet container.<\/p>\n<h3><a name=\"Servlet Process\"><\/a>1.1 Java Servlet Process<\/h3>\n<p>The process of a servlet is shown below:<\/p>\n<p><figure id=\"attachment_34050\" aria-describedby=\"caption-attachment-34050\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/servlet-processing-of-user-requests.jpg\"><img decoding=\"async\" class=\"wp-image-34050 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/servlet-processing-of-user-requests.jpg\" alt=\"Java Servlet Figure 1: servlet processing of user requests\" width=\"598\" height=\"208\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/servlet-processing-of-user-requests.jpg 598w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/servlet-processing-of-user-requests-300x104.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-34050\" class=\"wp-caption-text\">Figure 1: servlet processing of user requests<\/figcaption><\/figure><\/p>\n<ul>\n<li>A Request is sent by a client to a servlet container. The container acts as a Web server.<\/li>\n<li>The Web server searches for the servlet and initiates it.<\/li>\n<li>The client request is processed by the servlet and it sends the response back to the server.<\/li>\n<li>The Server response is then forwarded to the client.<\/li>\n<\/ul>\n<h3><a name=\"Merits\"><\/a>1.2 Merits<\/h3>\n<ul>\n<li>Servlets are platform independent as they can run on any platform.<\/li>\n<li>The Servlet API inherits all the features of the Java platform.<\/li>\n<li>It builds and modifies the security logic for server-side extensions.<\/li>\n<li>Servlets inherit the security provided by the Web Server.<\/li>\n<li>In Servlet, only a single instance of the requests runs concurrently. It does not run in a separate process. So, it saves the memory by removing the overhead of creating a new process for each request.<\/li>\n<\/ul>\n<h2><a name=\"Life Cycle\"><\/a>2. Life Cycle<\/h2>\n<p>Servlet lifecycle describes how the servlet container manages the servlet object.<\/p>\n<ul>\n<li>Load Servlet Class<\/li>\n<li>Servlet Instance is created by the web container when the servlet class is loaded<\/li>\n<li><code>init()<\/code>:This is called only once when the servlet is created. There is no need to call it again and again for multiple requests.<\/li>\n<\/ul>\n<pre class=\"brush:java\">          public void init() throws ServletException {\n\n           }<\/pre>\n<ul>\n<li><code>service()<\/code>: It is called by the web container to handle request from clients. Here the actual functioning of the code is done. The web container calls this method each time when request for the servlet is received.<\/li>\n<\/ul>\n<p>It calls <code>doGet()<\/code>, <code>doPost()<\/code>, <code>doTrace()<\/code>, <code>doPut()<\/code>, <code>doDelete()<\/code> and other methods<\/p>\n<ul>\n<li><code>doGet()<\/code>:<\/li>\n<\/ul>\n<pre class=\"brush:java\">public void doGet(HttpServletRequest request,HttpServletResponse response)\n    throws ServletException, IOException {\n\/\/ code\n}<\/pre>\n<ul>\n<li><code>doPost()<\/code>:<\/li>\n<\/ul>\n<pre class=\"brush:java\">public void doPost(HttpServletRequest request, HttpServletResponse response)\n    throws ServletException, IOException {\n    \/\/ code\n}<\/pre>\n<ul>\n<li><code>destroy()<\/code>: It is used to clean resources and called before removing the servlet instance.<\/li>\n<\/ul>\n<pre class=\"brush:java\">public void destroy()<\/pre>\n<p><figure id=\"attachment_34055\" aria-describedby=\"caption-attachment-34055\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Servlet-Life-Cycle.jpg\"><img decoding=\"async\" class=\"wp-image-34055 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Servlet-Life-Cycle.jpg\" alt=\"Java Servlet Figure 2: Servlet Life Cycle\" width=\"598\" height=\"491\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Servlet-Life-Cycle.jpg 598w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Servlet-Life-Cycle-300x246.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-34055\" class=\"wp-caption-text\">Figure 2: Servlet Life Cycle<\/figcaption><\/figure><\/p>\n<h2><a name=\"Container\"><\/a>3. Container<\/h2>\n<p>It is known as servlet engine which manages Java Servlet components on top of a web server to the request send by the client.<\/p>\n<h3><a name=\"Services\"><\/a>3.1 Services<\/h3>\n<p>Servlet Container provides the following services:<\/p>\n<ul>\n<li>It manages the servlet life cycle.<\/li>\n<li>The resources like servlets, JSP pages and HTML files are managed by servlet container.<\/li>\n<li>It appends session ID to the URL path to maintain session.<\/li>\n<li>Provides security service.<\/li>\n<li>It loads a servlet class from network services, file systems like remote file system and local file system.<\/li>\n<\/ul>\n<h3><a name=\"Servlet Container Configurations\"><\/a>3.2 Servlet Container Configurations<\/h3>\n<p>The servlet container can be configured with the web server to manage servlets in three ways listed below:<\/p>\n<ul>\n<li>Standalone container<\/li>\n<li>In-process container<\/li>\n<li>Out-process container<\/li>\n<\/ul>\n<p><strong>Standalone container<\/strong>: In this type the Web Server functionality is taken by the Servlet container. Here, the container is strongly coupled with the Web server.<\/p>\n<p><strong>In-Process container<\/strong>: In this the container runs within the Web server process.<\/p>\n<p><strong>Out-Process container<\/strong>: In this type there is a need to configure the servlet container to run outside the Web server process. It is used in some cases like if there is a need to run Servlets and Servlet container in different process\/systems.<\/p>\n<h2><a name=\"Demo\"><\/a>4. Demo: To start with<\/h2>\n<p>Here is an example showing Demo Servlet. Follow these steps to start with your first Servlet Application in NetBeansIDE.<\/p>\n<p><strong>Step 1:<\/strong> Open <em>NetBeansIDE -&gt; File -&gt; New Project-&gt;WebApplication -&gt;<\/em> Set Project name as <em>WebApplicationServletDemo<\/em><\/p>\n<p><figure id=\"attachment_34058\" aria-describedby=\"caption-attachment-34058\" style=\"width: 623px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure3.jpg\"><img decoding=\"async\" class=\"wp-image-34058 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure3.jpg\" alt=\"Java Servlet Figure 3: Create new WebApplication project in NetBeansIDE: WebApplicationServletDemo\" width=\"623\" height=\"390\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure3.jpg 623w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure3-300x187.jpg 300w\" sizes=\"(max-width: 623px) 100vw, 623px\" \/><\/a><figcaption id=\"caption-attachment-34058\" class=\"wp-caption-text\">Figure 3: Create new WebApplication project in NetBeansIDE: WebApplicationServletDemo<\/figcaption><\/figure><\/p>\n<p><strong>Step 2:<\/strong> Now click on <em>Next &gt;<\/em>as shown above. This will create new project with the following directory structure.<\/p>\n<p><figure id=\"attachment_34059\" aria-describedby=\"caption-attachment-34059\" style=\"width: 196px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure4.jpg\"><img decoding=\"async\" class=\"wp-image-34059 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure4.jpg\" alt=\"Java Servlet Figure 4: Project Directory after creating new project\" width=\"196\" height=\"206\" \/><\/a><figcaption id=\"caption-attachment-34059\" class=\"wp-caption-text\">Figure 4: Project Directory after creating new project<\/figcaption><\/figure><\/p>\n<p><strong>Step 3:<\/strong> Create new servlet application by Right Clicking on <em>Project Directory-&gt; New -&gt; Servlet<\/em><\/p>\n<p><figure id=\"attachment_34060\" aria-describedby=\"caption-attachment-34060\" style=\"width: 337px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure5.jpg\"><img decoding=\"async\" class=\"wp-image-34060 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure5.jpg\" alt=\"Java Servlet Figure 5: Adding Servlet file\" width=\"337\" height=\"329\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure5.jpg 337w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure5-300x292.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure5-42x42.jpg 42w\" sizes=\"(max-width: 337px) 100vw, 337px\" \/><\/a><figcaption id=\"caption-attachment-34060\" class=\"wp-caption-text\">Figure 5: Adding Servlet file<\/figcaption><\/figure><\/p>\n<p><strong>Step 4:<\/strong> Add the Servlet Class Name as \u201c<em>ServletDemo<\/em>\u201d and click on <em>Next<\/em>.<\/p>\n<p><figure id=\"attachment_34061\" aria-describedby=\"caption-attachment-34061\" style=\"width: 615px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure6.jpg\"><img decoding=\"async\" class=\"wp-image-34061 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure6.jpg\" alt=\"Java Servlet Figure 6: Adding Servlet Class Name\" width=\"615\" height=\"433\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure6.jpg 615w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure6-300x211.jpg 300w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><\/a><figcaption id=\"caption-attachment-34061\" class=\"wp-caption-text\">Figure 6: Adding Servlet Class Name<\/figcaption><\/figure><\/p>\n<p><strong>Step 5: <\/strong>Now, Configure Servlet Deployment by checking \u201c<em>Add information to deployment descriptor (web.xml)<\/em>&#8221; and adding URL Pattern (the link visible) as <em>ServletDemo<\/em>. This step will generate <em>web.xml<\/em> file in <em>WEB-INF<\/em> folder.<\/p>\n<p><figure id=\"attachment_34062\" aria-describedby=\"caption-attachment-34062\" style=\"width: 622px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure7.jpg\"><img decoding=\"async\" class=\"wp-image-34062 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure7.jpg\" alt=\"Java Servlet Figure 7: Configuring Servlet Deployment\" width=\"622\" height=\"275\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure7.jpg 622w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure7-300x132.jpg 300w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/><\/a><figcaption id=\"caption-attachment-34062\" class=\"wp-caption-text\">Figure 7: Configuring Servlet Deployment<\/figcaption><\/figure><\/p>\n<p><strong>Step 6:<\/strong> Click on Finish as shown above, this will add ServletDemo.java servlet under project directory. Check the changes under Directory Structure:<\/p>\n<p><figure id=\"attachment_34063\" aria-describedby=\"caption-attachment-34063\" style=\"width: 255px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure8.jpg\"><img decoding=\"async\" class=\"wp-image-34063 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure8.jpg\" alt=\"Java Servlet Figure 8: Changes under project directory after configuring\" width=\"255\" height=\"250\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure8.jpg 255w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure8-42x42.jpg 42w\" sizes=\"(max-width: 255px) 100vw, 255px\" \/><\/a><figcaption id=\"caption-attachment-34063\" class=\"wp-caption-text\">Figure 8: Changes under project directory after configuring<\/figcaption><\/figure><\/p>\n<p>Here is the code for deployment descriptor (web.xml) with URL-patter as \/<em>ServletDemo<\/em>:<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 1: web.xml<\/span><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;web-app version=\"3.1\" xmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_3_1.xsd\"&gt;\n&lt;servlet&gt;\n&lt;servlet-name&gt;ServletDemo&lt;\/servlet-name&gt;\n&lt;servlet-class&gt;ServletDemo&lt;\/servlet-class&gt;\n&lt;\/servlet&gt;\n&lt;servlet-mapping&gt;\n    &lt;servlet-name&gt;ServletDemo&lt;\/servlet-name&gt;\n    &lt;url-pattern&gt;\/ServletDemo&lt;\/url-pattern&gt;\n&lt;\/servlet-mapping&gt;\n&lt;session-config&gt;\n&lt;session-timeout&gt;\n            30\n&lt;\/session-timeout&gt;\n&lt;\/session-config&gt;\n&lt;\/web-app&gt;<\/pre>\n<p>Here,<\/p>\n<pre class=\"brush:xml\">&lt;servlet-name&gt;: name given to Servlet\n&lt;servlet-class&gt;: servlet class\n&lt;servlet-mapping&gt;: maps internal name to URL\n&lt;url-pattern&gt;: link displays when Servlet runs<\/pre>\n<p>The hyperlink <em>Next<\/em> is mentioned as <em>ServletDemo<\/em>. So, when the user will click on it, the page will redirect to <em>ServletDemo<\/em> servlet whose url-pattern is mentioned as <em>ServetDemo<\/em>:<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 2: index.html<\/span><\/p>\n<pre class=\"brush:html\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Welcome&lt;\/title&gt;\n&lt;meta charset=\"UTF-8\"&gt;\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div&gt;&lt;h2&gt;Welcome&lt;\/h2&gt;&lt;\/div&gt;\n&lt;p&gt;We're still under development stage. Stay Tuned for our website's new design and learning content.&lt;\/p&gt;\n&lt;a href=\"ServletDemo\"&gt;&lt;b&gt;Next&lt;\/b&gt;&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\">Listing 3: ServletDemo.java<\/span><\/p>\n<pre class=\"brush:java\">import java.io.IOException;\nimport java.io.PrintWriter;\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\npublic class ServletDemo extends HttpServlet {\n\n\tprotected void processRequest(HttpServletRequest request, HttpServletResponse response)\n\t\t\tthrows ServletException, IOException {\n\t\tresponse.setContentType(\"text\/html;charset=UTF-8\");\n\t\ttry (PrintWriter out = response.getWriter()) {\n\t\t\tout.println(\"&lt;!DOCTYPE html&gt;\");\n\t\t\tout.println(\"&lt;html&gt;\");\n\t\t\tout.println(\"&lt;head&gt;\");\n\t\t\tout.println(\"&lt;title&gt;Servlet ServletDemo&lt;\/title&gt;\");            \n\t\t\tout.println(\"&lt;\/head&gt;\");\n\t\t\tout.println(\"&lt;body&gt;\");\n\t\t\tout.println(\"&lt;h1&gt;Servlet ServletDemo at \" + request.getContextPath() + \"&lt;\/h1&gt;\");\n\t\t\tout.println(\"&lt;\/body&gt;\");\n\t\t\tout.println(\"&lt;\/html&gt;\");\n\t\t}\n\t}\n\n\t@Override\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response)\n\t\t\tthrows ServletException, IOException {\n\t\tresponse.setContentType(\"text\/html;charset=UTF-8\");\n\t\tPrintWriter out = response.getWriter(); \n\t\ttry {\n\t\t\t\/* TODO output your page here. You may use following sample code. *\/ \n\t\t\tout.println(\"&lt;!DOCTYPE html&gt;\"); \n\t\t\tout.println(\"&lt;html&gt;\");\n\t\t\tout.println(\"&lt;head&gt;\");\n\t\t\tout.println(\"&lt;title&gt;Servlets&lt;\/title&gt;\");\n\t\t\tout.println(\"&lt;\/head&gt;\");\n\t\t\tout.println(\"&lt;body&gt;\");\n\t\t\tout.println(\"&lt;br \/&gt;&lt;p&gt;&lt;h2&gt;First Demo Servlet application&lt;\/h2&gt;&lt;br \/&gt;Here, the URL-pattern is ServletDemo in web.xml. So, the address is &lt;i&gt;WebApplicationServletDemo\/ServletDemo&lt;\/i&gt;.&lt;\/p&gt;\");\n\t\t\tout.println(\"&lt;br \/&gt;&lt;br \/&gt;&lt;a href=\\\"index.html\\\"&gt;Previous Page&lt;\/a&gt;\");\n\t\t\tout.println(\"&lt;\/body&gt;\");\n\t\t\tout.println(\"&lt;\/html&gt;\");\n\t\t} \n\t\tfinally \n\t\t{ \n\t\tout.close(); \n\t\t}\n\t}\n}\n<\/pre>\n<p><figure id=\"attachment_34066\" aria-describedby=\"caption-attachment-34066\" style=\"width: 642px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure9.jpg\"><img decoding=\"async\" class=\"wp-image-34066 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure9.jpg\" alt=\"Java Servlet Figure 9: Output showing index.html welcome page\" width=\"642\" height=\"198\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure9.jpg 642w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure9-300x92.jpg 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/a><figcaption id=\"caption-attachment-34066\" class=\"wp-caption-text\">Figure 9: Output showing index.html welcome page<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_34068\" aria-describedby=\"caption-attachment-34068\" style=\"width: 638px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure10.jpg\"><img decoding=\"async\" class=\"wp-image-34068 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure10.jpg\" alt=\"Java Servlet Figure 10: Output showing redirection to ServletDemo.java\" width=\"638\" height=\"242\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure10.jpg 638w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure10-300x113.jpg 300w\" sizes=\"(max-width: 638px) 100vw, 638px\" \/><\/a><figcaption id=\"caption-attachment-34068\" class=\"wp-caption-text\">Figure 10: Output showing redirection to ServletDemo.java<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2><a name=\"Filter\"><\/a>5. Filter<\/h2>\n<p>Filters transform the content of requests, responses, and header information from one format to another. These are reusable codes.<\/p>\n<ul>\n<li>Filter class is declared in the deployment descriptor.<\/li>\n<li>It is used to write reusable components.<\/li>\n<li>The request is process before it is called using filters.<\/li>\n<li>It can be used under a web application for some tasks like:\n<ul>\n<li>Validation<\/li>\n<li>Compression<\/li>\n<li>Verification<\/li>\n<li>Internationalization<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><a name=\"Interface\"><\/a>5.1 Interface<\/h3>\n<p>It consists of these 3 filters:<\/p>\n<p><figure id=\"attachment_34071\" aria-describedby=\"caption-attachment-34071\" style=\"width: 548px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Filter-API-Interfaces.jpg\"><img decoding=\"async\" class=\"wp-image-34071 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Filter-API-Interfaces.jpg\" alt=\"Java Servlet Figure 11: Filter API Interfaces\" width=\"548\" height=\"199\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Filter-API-Interfaces.jpg 548w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Filter-API-Interfaces-300x108.jpg 300w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/a><figcaption id=\"caption-attachment-34071\" class=\"wp-caption-text\">Figure 11: Filter API Interfaces<\/figcaption><\/figure><\/p>\n<p><strong>Filter<\/strong><\/p>\n<p>This is the initial and basic interface which all filter class should implement. <code>Java.servlet.Filter<\/code> interface has the following methods:<\/p>\n<table border=\"2\">\n<tbody>\n<tr style=\"height: 50px;\">\n<th style=\"width: 400px; background-color: #dedede;\"><strong>Methods<\/strong><\/th>\n<th style=\"background-color: #dedede;\"><strong>Description<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td><code>init(FilterConfig)<\/code><\/td>\n<td>This method initializes a filter<\/td>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td><code>doFilter(ServletRequest, ServletResponse, FilterChain)<\/code><\/td>\n<td>This method encapsulates the service logic on ServletRequest to generate ServletResponse. FilterChain is to forward request\/response pair to the next filter.<\/td>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td><code>destroy()<\/code><\/td>\n<td>It destroys the instance of the filter class.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>FilterConfig<\/strong><\/p>\n<p>Its object is used when the filters are initialized. Deployment descriptor (web.xml) consists of configuration information. The object of FilterConfig interface is used to fetch configuration information about filter specified in <em>web.xml<\/em>. Its methods are mentioned below:<\/p>\n<table border=\"2\">\n<tbody>\n<tr style=\"height: 50px;\">\n<th style=\"width: 400px; background-color: #dedede;\"><strong>Methods<\/strong><\/th>\n<th style=\"background-color: #dedede;\"><strong>Description<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td width=\"390\"><code>getFilterName()<\/code><\/td>\n<td width=\"399\">It returns the name of filter in web.xml<\/td>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td width=\"390\"><code>getInitParameter(String)<\/code><\/td>\n<td width=\"399\">It returns specified initialization parameter\u2019s value from web.xml<\/td>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td width=\"390\"><code>getInitParameterNames()<\/code><\/td>\n<td width=\"399\">It returns enumeration of all initialization parameters of filter.<\/td>\n<\/tr>\n<tr style=\"height: 50px;\">\n<td width=\"390\"><code>getServletContext()<\/code><\/td>\n<td width=\"399\">It returns ServletContext object.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>FilterChain<\/strong><\/p>\n<p>It stores information about more than 1 filter (chain). All filters in this chain should be applied on request before processing of a request.<\/p>\n<h3><a name=\"Example\"><\/a>5.2 Example<\/h3>\n<p>This is an example showing filters application in NetBeansIDE. Create a WebApplication project <em>WebApplicationFilterDemo<\/em> in the same ways as shown under <em>Demo<\/em> section. New Filter can be added in the web application by Right Clicking on <em>Project Directory -&gt; New -&gt; Filter<\/em><\/p>\n<p><figure id=\"attachment_34074\" aria-describedby=\"caption-attachment-34074\" style=\"width: 532px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure11.jpg\"><img decoding=\"async\" class=\"wp-image-34074 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure11.jpg\" alt=\"Java Servlet Figure 12: Add new Filter to web application\" width=\"532\" height=\"378\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure11.jpg 532w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure11-300x213.jpg 300w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/a><figcaption id=\"caption-attachment-34074\" class=\"wp-caption-text\">Figure 12: Add new Filter to web application<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_34075\" aria-describedby=\"caption-attachment-34075\" style=\"width: 624px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure12.jpg\"><img decoding=\"async\" class=\"wp-image-34075 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure12.jpg\" alt=\"Java Servlet Figure 13: Add Class Name as NewFilter and click on Next\" width=\"624\" height=\"384\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure12.jpg 624w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure12-300x184.jpg 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/a><figcaption id=\"caption-attachment-34075\" class=\"wp-caption-text\">Figure 13: Add Class Name as NewFilter and click on Next<\/figcaption><\/figure><\/p>\n<p>Configure Filter Deployment by checking \u201c<em>Add information to deployment descriptor (web.xml)<\/em>\u201d. Now, the <em>Next <\/em>button is disabled here due to an error highlighted in Figure 13. The error \u201c<em>Enter at least one URL pattern<\/em>\u201d can be solved by clicking on \u201c<em>New<\/em>\u201d.<\/p>\n<p><figure id=\"attachment_34076\" aria-describedby=\"caption-attachment-34076\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure13.jpg\"><img decoding=\"async\" class=\"wp-image-34076 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure13.jpg\" alt=\"Java Servlet Figure 14: Configure Filter Deployment by checking \u201cAdd information to deployment descriptor(web.xml)\u201d\" width=\"636\" height=\"446\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure13.jpg 636w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure13-300x210.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-34076\" class=\"wp-caption-text\">Figure 14: Configure Filter Deployment by checking \u201cAdd information to deployment descriptor(web.xml)\u201d<\/figcaption><\/figure><\/p>\n<p>Now, filter is mapped by adding URL-pattern as shown in Figure 15.<\/p>\n<p><figure id=\"attachment_34077\" aria-describedby=\"caption-attachment-34077\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure14.jpg\"><img decoding=\"async\" class=\"wp-image-34077 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure14.jpg\" alt=\"Java Servlet Figure 15: Filter mapping\" width=\"636\" height=\"447\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure14.jpg 636w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure14-300x210.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-34077\" class=\"wp-caption-text\">Figure 15: Filter mapping<\/figcaption><\/figure><\/p>\n<p>After adding new filter and clicking on OK, the error will get resolved. Now, add <em>init-parameter<\/em> with name and value. Then click <em>Finish<\/em>.<\/p>\n<p><figure id=\"attachment_34078\" aria-describedby=\"caption-attachment-34078\" style=\"width: 572px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-15.jpg\"><img decoding=\"async\" class=\"wp-image-34078 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-15.jpg\" alt=\"Java Servlet Figure 16: Adding init-parameter\" width=\"572\" height=\"290\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-15.jpg 572w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-15-300x152.jpg 300w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/a><figcaption id=\"caption-attachment-34078\" class=\"wp-caption-text\">Figure 16: Adding init-parameter<\/figcaption><\/figure><\/p>\n<p><span style=\"text-decoration: underline;\">Listing 4: web.xml<\/span><\/p>\n<p>The Filter <em>NewFilter<\/em> can be applied to every servlet as \/* is specified here for URL-pattern.<\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;web-app version=\"3.1\" xmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_3_1.xsd\"&gt;\n&lt;filter&gt;\n&lt;filter-name&gt;NewFilter&lt;\/filter-name&gt;\n&lt;filter-class&gt;NewFilter&lt;\/filter-class&gt;\n  &lt;init-param&gt;\n      &lt;param-name&gt;newParam&lt;\/param-name&gt;\n      &lt;param-value&gt;valueOne&lt;\/param-value&gt;\n  &lt;\/init-param&gt;\n&lt;\/filter&gt;\n&lt;filter-mapping&gt;\n     &lt;filter-name&gt;NewFilter&lt;\/filter-name&gt;\n     &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;\n&lt;\/filter-mapping&gt;\n&lt;session-config&gt;\n    &lt;session-timeout&gt;\n            30\n   &lt;\/session-timeout&gt;\n&lt;\/session-config&gt;\n&lt;\/web-app&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\">Listing 5: NewFilter.java<\/span><\/p>\n<pre class=\"brush:java\">import java.io.*;\nimport javax.servlet.*;\nimport javax.servlet.http.*;\nimport java.util.*;\n\npublic class NewFilter implements Filter {\n\n\tpublic void init(FilterConfigfilterConfig) {        \n\t\t\/\/ init parameter\n\t\tString value = filterConfig.getInitParameter(\"newParam\"); \n\n\t\t\/\/ displaying init parameter value\n\t\tSystem.out.println(\"The Parameter value: \" + value); \n\t}\n\n\tpublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)\n\t\t\tthrows IOException, ServletException {\n\n\t\t\/\/ IP address of the client machine.   \n\t\tString remoteAddress = request.getRemoteAddr();\n\n\t\t\/\/ Returns the remote address\n\t\tSystem.out.println(\"Remote Internet Protocl Address: \"+ remoteAddress);\n\n\t\tchain.doFilter(request,response);\n\t}\n\n\tpublic void destroy( ){\n\n\t}\n}\n<\/pre>\n<p><figure id=\"attachment_34079\" aria-describedby=\"caption-attachment-34079\" style=\"width: 637px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure16.jpg\"><img decoding=\"async\" class=\"wp-image-34079 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure16.jpg\" alt=\"Java Servlet Figure 17: Showing console output\" width=\"637\" height=\"112\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure16.jpg 637w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure16-300x52.jpg 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/a><figcaption id=\"caption-attachment-34079\" class=\"wp-caption-text\">Figure 17: Showing console output<\/figcaption><\/figure><\/p>\n<h2><a name=\"Session\"><\/a>6. Session<\/h2>\n<p>It is a collection of HTTP requests between client and server. The session is destroyed when it expires and its resources are back to the servlet engine.<\/p>\n<h3><a name=\"Session Handling\"><\/a>6.1 Session Handling<\/h3>\n<p>It is a means to keep track of session data. This represents the data transferred in a session. It is used when session data from one session may be required by a web server for completing tasks in same or different sessions. Session handling is also known assession tracking.<br \/>\n[ulp id=&#8217;evUVoGhmMT2e7Bse&#8217;]<br \/>\n&nbsp;<\/p>\n<h3><a name=\"Mechanisms of Session Handling\"><\/a>6.2 Mechanisms of Session Handling<\/h3>\n<p>There are four mechanisms for session handling:<\/p>\n<p><strong>URL rewriting<\/strong>: The session data required in the next request is appended to the URL path used by the client to make the next request.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>Query String:<\/em> A string appended after the requested URI is query string. The string is appended with separator as \u2018?\u2019 character.<\/p>\n<p>Example 1): <code>http:\/\/localhost:8080\/newproject\/login?user=test&amp;passwd=abcde<\/code><\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>Path Info:<\/em> It is the part of the request URI. Session data can be added to the path info part of the request URI.<\/p>\n<p>Example 2): <code>http:\/\/localhost:8080\/newproject\/myweb\/login;user=test&amp;passwd=abcde<\/code><\/p>\n<p><strong>Hidden form field<\/strong>:\u00a0 A type of HTML form field which remains hidden in the view. Some other form fields are: textbox, password etc. This approach can be used with form-based requests. It is just used for hiding user data from other different types of users.<\/p>\n<p>Example 3: <code>&lt;input type=\"hidden\" username=\"name\" value=\"nameOne\"\/&gt;<\/code><\/p>\n<p><strong>Cookies<\/strong>: It is a file containing the information that is sent to a client by a server. Cookies are saved at the client side after being transmitted to clients (from server)through the HTTP response header.<\/p>\n<p>Cookies are considered best when we want to reduce the network traffic. Its attributes are name, value, domain, version number, path, and comment. The package <code>javax.servlet.http<\/code> consists of a class names Cookie.<\/p>\n<p>Some methods in <code>javax.servlet.http.Cookie<\/code> class are listed below:<\/p>\n<ul>\n<li><code>setValue (String)<\/code><\/li>\n<li><code>getValue()<\/code><\/li>\n<li><code>getName()<\/code><\/li>\n<li><code>setComment(String)<\/code><\/li>\n<li><code>getComment()<\/code><\/li>\n<li><code>setVersion(String)<\/code><\/li>\n<li><code>getVersion()<\/code><\/li>\n<li><code>setDomain(String)<\/code><\/li>\n<li><code>setPath(String)<\/code><\/li>\n<li><code>getPath()<\/code><\/li>\n<li><code>setSecure(boolean)<\/code><\/li>\n<li><code>getSecure(boolean)<\/code><\/li>\n<\/ul>\n<p><strong>HTTP session<\/strong>: It provides asession management service implemented through HttpSession object.<\/p>\n<p>Some HttpSession object methods are listed here; this is referred from the official <a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/servlet\/http\/HttpSession.html\">Oracle Documentation<\/a>:<\/p>\n<table border=\"2\">\n<tbody>\n<tr style=\"height: 70px;\">\n<th style=\"width: 250px; background-color: #dedede;\"><strong>Method<\/strong><\/th>\n<th style=\"background-color: #dedede;\"><strong>Description<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public Object getAttribute(String name)<\/code><\/td>\n<td width=\"399\">It returns the object bound with the specified name in this session or\u00a0null\u00a0if no object is bound under the name.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public Enumeration getAttributeNames()<\/code><\/td>\n<td width=\"399\">It returns Enumeration\u00a0of\u00a0String\u00a0objects containing the names of all the objects bound to this session.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public String getId()<\/code><\/td>\n<td width=\"399\">It returns a string containing the unique identifier assigned to this session.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public long getCreationTime()<\/code><\/td>\n<td width=\"399\">It returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public long getLastAccessedTime()<\/code><\/td>\n<td width=\"399\">It returns the last time the client sent a request associated with this session.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public int getMaxInactiveInterval()<\/code><\/td>\n<td width=\"399\">It returns the maximum time interval, in seconds that the servlet container will keep this session open between client accesses.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public void invalidate()<\/code><\/td>\n<td width=\"399\">It Invalidates this session then unbinds any objects bound to it.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>public boolean isNew()<\/code><\/td>\n<td width=\"399\">It returns\u00a0true\u00a0if the client does not yet know about the session or if the client chooses not to join the session.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3><a name=\"Example\"><\/a>6.3 Example<\/h3>\n<p>Session Information like session id, session creation time, last accessed time and others is printed under this example.<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 6: ServletSession.java<\/span><\/p>\n<pre class=\"brush:java\">import java.io.IOException;\nimport java.io.PrintWriter;\nimport java.util.Date;\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport javax.servlet.http.HttpSession;\n\npublic class ServletSession extends HttpServlet {\n\n@Override\n protected void doGet(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n           \/\/ session object creation\n           HttpSessionnewSession = request.getSession(true);\n      \/\/ Session creation time.\n      Date cTime = new Date(newSession.getCreationTime());\n      \/\/ The last time the client sent a request.\n      Date lTime = new Date( newSession.getLastAccessedTime());\n\n      \/* sets the time, in seconds, between client requests before the servlet container    \n      invalidates this session *\/\n      newSession.setMaxInactiveInterval(1 * 60 * 60);\n      String str = \"Website | Session\";\n\n      response.setContentType(\"text\/html\");\n      PrintWriter out = response.getWriter();\n\n      String document =\n      \"&lt;!doctype html public \\\"-\/\/w3c\/\/dtd html 4.0 \" +\n      \"transitional\/\/en\\\"&gt;\\n\";\n      out.println(document +\n                \"&lt;html&gt;\\n\" +\n                \"&lt;head&gt;&lt;title&gt;\" + str + \"&lt;\/title&gt;&lt;\/head&gt;\\n\" +\n                \"&lt;body bgcolor=\\\"#bbf5f0\\\"&gt;\\n\" +\n                 \"&lt;h2&gt;Website: Displaying Session Information&lt;\/h2&gt;\\n\" +\n                \"&lt;table border=\\\"2\\\"&gt;\\n\" +\n                \"&lt;tr&gt;\\n\" +\n                \"  &lt;td&gt;Unique identifier assigned to this session&lt;\/td&gt;\\n\" +\n                \"  &lt;td&gt;\" + newSession.getId() + \"&lt;\/td&gt;\"\n              + \"&lt;\/tr&gt;\\n\" +\n                \"&lt;tr&gt;\\n\" +\n                \"  &lt;td&gt;The time when this session was created&lt;\/td&gt;\\n\" +\n                \"  &lt;td&gt;\" + cTime + \n                \"  &lt;\/td&gt;\"\n              + \"&lt;\/tr&gt;\\n\" +\n                \"&lt;tr&gt;\\n\" +\n                \"  &lt;td&gt;The last time the client sent a request associated with this session&lt;\/td&gt;\\n\"\n              + \"  &lt;td&gt;\" + lTime + \n                \"  &lt;\/td&gt;\"\n              + \"&lt;\/tr&gt;\\n\" +\n                \"&lt;\/tr&gt;\\n\" +\n                \"&lt;tr&gt;\\n\" +\n                \"  &lt;td&gt; the maximum time interval, in seconds that the servlet container will keep this session open between client accesses.&lt;\/td&gt;\\n\" +\n                \"  &lt;td&gt;\" + newSession.getMaxInactiveInterval() + \n                \"  &lt;\/td&gt;\"\n              + \"&lt;\/tr&gt;\\n\" +\n                     \"&lt;\/table&gt;\\n\" +\n                \"&lt;\/body&gt;&lt;\/html&gt;\");\n  }\n}<\/pre>\n<p><figure id=\"attachment_34082\" aria-describedby=\"caption-attachment-34082\" style=\"width: 621px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure17.jpg\"><img decoding=\"async\" class=\"wp-image-34082 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure17.jpg\" alt=\"Java Servlet Figure 18: Displaying output\" width=\"621\" height=\"191\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure17.jpg 621w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure17-300x92.jpg 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/a><figcaption id=\"caption-attachment-34082\" class=\"wp-caption-text\">Figure 18: Displaying output<\/figcaption><\/figure><\/p>\n<h2><a name=\"Exception Handling\"><\/a>7. Exception Handling<\/h2>\n<p>Exceptions are used to handle errors. It is a reaction to unbearable conditions. Here comes the role of web.xml i.e. deployment description which is used to run JSP and servlet pages. The container searches the configurations in web.xml for a match. So, in web.xml use these exception-type elements for match with the thrown exception type when a servlet throws an exception.<\/p>\n<h3><a name=\"Error Code Configuration\"><\/a>7.1 Error Code Configuration<\/h3>\n<p>The \/HandlerClass servlet gets called when an error with status code 403 occurs as shown below:<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 7: For Error code 403<\/span><\/p>\n<pre class=\"brush:xml\">&lt;error-page&gt;\n  &lt;error-code&gt;403&lt;\/error-code&gt;\n  &lt;location&gt;\/HandlerClass&lt;\/location&gt;\n&lt;\/error-page&gt;\n<\/pre>\n<h3><a name=\"Exception-Type Configuration\"><\/a>7.2 Exception-Type Configuration<\/h3>\n<p>If the application throws\u00a0IOException, then \/HandlerClass servlet gets called by the container:<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 8: For Exception Type IOException<\/span><\/p>\n<pre class=\"brush:xml\">&lt;error-page&gt;\n  &lt;exception-type&gt;java.io.IOException&lt;\/exception-type &gt;\n  &lt;location&gt;\/HandlerClass&lt;\/location&gt;\n&lt;\/error-page&gt;<\/pre>\n<p>If you want to avoid the overhead of adding separate elements, then use <code>java.lang.Throwable<\/code> as exception-type:<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 9: For all exceptions mention java.lang.Throwable:<\/span><\/p>\n<pre class=\"brush:xml\">&lt;error-page&gt;\n  &lt;exception-type&gt;java.lang.Throwable&lt;\/exception-type &gt;\n  &lt;location&gt;\/HandlerClass&lt;\/location&gt;\n&lt;\/error-page&gt;<\/pre>\n<h2><a name=\"Debugging\"><\/a>8. Debugging<\/h2>\n<p>Client-server interactions are in large number in Servlets. This makes errors difficult to locate. Different ways can be followed for location warnings and errors.<\/p>\n<h3><a name=\"Message Logging\"><\/a>8.1 Message Logging<\/h3>\n<p>Logs are provided for getting information about warning and error messages. For this a standard logging method is used. Servlet API can generate this information using log() method. Using Apache Tomcat, these logs can be found in TomcatDirectory\/logs.<\/p>\n<h3><a name=\"Java Debugger\"><\/a>8.2 Java Debugger<\/h3>\n<p>Servlets can be debugged using JDB Debugger i.e. Java Debugger. In this the program being debugged is sun.servlet.http.HttpServer. Set debugger&#8217;s class path for finding the following classes:<\/p>\n<ul>\n<li>servlet.http.HttpServer<\/li>\n<li>server_root\/servlets and server_root\/classes: Through this the debugger sets breakpoints in a servlet.<\/li>\n<\/ul>\n<h3><a name=\"Headers\"><\/a>8.3 Headers<\/h3>\n<p>Users should have some information related to structure of HTTP headers. Issues can be judged using them which can further locate some unknown errors. Information related to HTTP headers can help you in locating errors. Studying request and response can help in guessing what is not going well.<\/p>\n<h3><a name=\"Refresh\"><\/a>8.4 Refresh<\/h3>\n<p>Refresh your browser&#8217;s web page to avoid it from caching previous request. At some stages, browser shows request performed previously. This is a known point but can be a problem for those who are working correctly but unable to display the result properly.<\/p>\n<p><span style=\"text-decoration: underline;\">Listing 21: ServletDebugging.java<\/span><\/p>\n<p>Here, Servlet Debugging is shown which displays the errors in Tomcat log.<\/p>\n<pre class=\"brush:java\">import java.io.IOException;\nimport java.io.PrintWriter;\nimport javax.servlet.ServletContext;\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\npublic class ServletDebugging extends HttpServlet {\n\n\t@Override\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response)\n\t\t\tthrows ServletException, IOException {\n\n\t\t\/\/ parameter \"name\"\n\t\tString strpm = request.getParameter(\"name\");\n\n\t\tServletContext context = getServletContext( );\n\n\t\t\/\/ checks if the parameter is set or not\n\t\tif (strpm == null || strpm.equals(\"\"))\n\t\tcontext.log(\"No message received:\", new IllegalStateException(\"Sorry, the \n\t\tparameter is missing.\"));\n\t\telse\n\t\tcontext.log(\"Here is the visitor's message: \" +strpm);\n\t\t\n\t}\n}\n<\/pre>\n<p><figure id=\"attachment_34084\" aria-describedby=\"caption-attachment-34084\" style=\"width: 597px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure18.jpg\"><img decoding=\"async\" class=\"wp-image-34084 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure18.jpg\" alt=\"Java Servlet Figure 19: Output as visible in Apache Tomcat log\" width=\"597\" height=\"87\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure18.jpg 597w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure18-300x43.jpg 300w\" sizes=\"(max-width: 597px) 100vw, 597px\" \/><\/a><figcaption id=\"caption-attachment-34084\" class=\"wp-caption-text\">Figure 19: Output as visible in Apache Tomcat log<\/figcaption><\/figure><\/p>\n<h2><a name=\"Internationalization\"><\/a>9. Internationalization<\/h2>\n<p>For building a global website, some important points are considered which includes language related to user&#8217;s nationality. Internationalization isenabling a website for providing content translated in different languages according to user&#8217;s nationality.<\/p>\n<h3><a name=\"Methods\"><\/a>9.1 Methods<\/h3>\n<p>For finding visitors local region and language, these methods are used:<\/p>\n<table border=\"2\">\n<tbody>\n<tr style=\"height: 70px;\">\n<th style=\"width: 250px; background-color: #dedede;\"><strong>Method<\/strong><\/th>\n<th style=\"background-color: #dedede;\"><strong>Description<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>String getCountry()<\/code><\/td>\n<td width=\"399\">Returns the country code.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>String getDisplayCountry()<\/code><\/td>\n<td width=\"399\">Returns a name for the visitors\u2019 country.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>String getLanguage()<\/code><\/td>\n<td width=\"399\">Returns the language code.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>String getDisplayLanguage()<\/code><\/td>\n<td width=\"399\">Returns a name for the visitors\u2019 language.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>String getISO3Country()<\/code><\/td>\n<td width=\"399\">Returns a three-letter abbreviation for the visitors country.<\/td>\n<\/tr>\n<tr style=\"height: 70px;\">\n<td width=\"399\"><code>String getISO3Language()<\/code><\/td>\n<td width=\"399\">Returns a three-letter abbreviation for the visitors language.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3><a name=\"ExampleI\"><\/a>9.2 Example<\/h3>\n<p>The example displays the current locale of a user. Following project is created in NetBeansIDE:<\/p>\n<pre class=\"brush:bash\">Project Name: WebApplicationInternationalization\nProject Location: C:\\Users\\Test\\Documents\\NetBeansProjects\nServlet: ServletLocale\nURL Pattern: \/ServletLocale\n<\/pre>\n<p><span style=\"text-decoration: underline;\">Listing 22: ServletLocale.java<\/span><\/p>\n<pre class=\"brush:java\">import java.io.IOException;\nimport java.io.PrintWriter;\nimport java.util.Locale;\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\npublic class ServletLocale extends HttpServlet {\n\n@Override\n    protected void doGet(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n           \/\/Get the client's Locale\n      Locale newloc = request.getLocale();\n      String country = newloc.getCountry();\n\n      \/\/ Set response content type\n      response.setContentType(\"text\/html\");\n      PrintWriter out = response.getWriter();\n\n      \/\/ this sets the page title and body content\n      String title = \"Finding Locale of current user\";\n      String docType =\n      \"&lt;!doctype html public \\\"-\/\/w3c\/\/dtd html 4.0 \" +\n      \"transitional\/\/en\\\"&gt;\\n\";\n       out.println(docType +\n        \"&lt;html&gt;\\n\" +\n        \"&lt;head&gt;&lt;title&gt;\" + title + \"&lt;\/title&gt;&lt;\/head&gt;\\n\" +\n        \"&lt;body bgcolor=\\\"#C0C0C0\\\"&gt;\\n\" + \"&lt;h3&gt;\" + country + \"&lt;\/h3&gt;\\n\" +\n        \"&lt;\/body&gt;&lt;\/html&gt;\");\n    }\n}\n<\/pre>\n<p><span style=\"text-decoration: underline;\">Listing23: index.html<\/span> with location hyperlink as URL-pattern &#8211; <em>ServletLocale<\/em><\/p>\n<pre class=\"brush:html\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;User's Location&lt;\/title&gt;\n&lt;meta charset=\"UTF-8\"&gt;\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;p&gt;Click on the following link for finding the locale of visitor:&lt;\/p&gt;\n    &lt;a href=\"ServletLocale\"&gt;&lt;b&gt;Location&lt;\/b&gt;&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p><span style=\"text-decoration: underline;\">Listing24: web.xml<\/span> with URL-pattern as \/<em>ServletLocale<\/em><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;web-app version=\"3.1\" xmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_3_1.xsd\"&gt;\n&lt;servlet&gt;\n    &lt;servlet-name&gt;ServletLocale&lt;\/servlet-name&gt;\n    &lt;servlet-class&gt;ServletLocale&lt;\/servlet-class&gt;\n&lt;\/servlet&gt;\n&lt;servlet-mapping&gt;\n     &lt;servlet-name&gt;ServletLocale&lt;\/servlet-name&gt;\n     &lt;url-pattern&gt;\/ServletLocale&lt;\/url-pattern&gt;\n&lt;\/servlet-mapping&gt;\n&lt;session-config&gt;\n&lt;session-timeout&gt;\n            30\n&lt;\/session-timeout&gt;\n&lt;\/session-config&gt;\n&lt;\/web-app&gt;<\/pre>\n<p><figure id=\"attachment_34086\" aria-describedby=\"caption-attachment-34086\" style=\"width: 493px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure19.jpg\"><img decoding=\"async\" class=\"wp-image-34086 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure19.jpg\" alt=\"Java Servlet Figure 20: Displaying index.html\" width=\"493\" height=\"190\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure19.jpg 493w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure19-300x115.jpg 300w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/a><figcaption id=\"caption-attachment-34086\" class=\"wp-caption-text\">Figure 20: Displaying index.html<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_34087\" aria-describedby=\"caption-attachment-34087\" style=\"width: 483px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-20.jpg\"><img decoding=\"async\" class=\"wp-image-34087 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-20.jpg\" alt=\"Java Servlet Figure 21: Displaying the locale as output\" width=\"483\" height=\"257\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-20.jpg 483w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Figure-20-300x159.jpg 300w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/a><figcaption id=\"caption-attachment-34087\" class=\"wp-caption-text\">Figure 21: Displaying the locale as output<\/figcaption><\/figure><\/p>\n<h2><a name=\"References\"><\/a>10. References<\/h2>\n<h3><a name=\"Website\"><\/a>10.1 Website<\/h3>\n<ul>\n<li><a href=\"https:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/servlet\/http\/package-summary.html\">Official Oracle Documentation<\/a><\/li>\n<li><a href=\"http:\/\/java.sun.com\/reference\/docs\/\">Sun Developer Network<\/a><\/li>\n<li><a href=\"https:\/\/netbeans.org\/downloads\/\">Free NetBeans Download<\/a><\/li>\n<li><a href=\"http:\/\/tomcat.apache.org\/download-80.cgi\">Free Apache Download<\/a><\/li>\n<li><a href=\"http:\/\/www.java.com\/en\/download\/index.jsp\">Free Java Download<\/a><\/li>\n<\/ul>\n<h3><a name=\"Book\"><\/a>10.2 Books<\/h3>\n<ul>\n<li>Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam, by Bryan Basham, Kathy Sierra , Bert Bates<\/li>\n<li>Servlet and JSP (A Tutorial), by Budi Kurniawan<\/li>\n<\/ul>\n<h2><a name=\"Conclusion\"><\/a>11. Conclusion<\/h2>\n<p>Servlet is fast in performance and easy to use when compared with traditional Common Gateway Interfaces (CGI). Through this guide you can easily learn the concepts related to Java Servlets. The project codes are developed under NetBeansIDE, so you will get an idea about some of its amazing user-friendly features as well.<\/p>\n<h2><a name=\"Download\"><\/a>12. Download<\/h2>\n<p>This was a tutorial on Java Servlets.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this tutorial here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/12\/Servlet_Project_Code.zip\"><strong>Servlet_Project_Code<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java Servlets is a Java based web technology. Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems. A servlet can almost be thought of as an applet that runs on the server side&#8211;without a face. Java servlets make many &hellip;<\/p>\n","protected":false},"author":435,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[845,1039],"class_list":["post-34048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-servlet","tag-ultimate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)<\/title>\n<meta name=\"description\" content=\"Check out our Java Servlet Tutorial where we explain the concepts related to Java Servlets. You can also download our FREE Java Servlet Ultimate Guide!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)\" \/>\n<meta property=\"og:description\" content=\"Check out our Java Servlet Tutorial where we explain the concepts related to Java Servlets. You can also download our FREE Java Servlet Ultimate Guide!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-12T14:00:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-05T14:28:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kaushik Pal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kaushik Pal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html\"},\"author\":{\"name\":\"Kaushik Pal\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f55d97bcbb667fad09fd920ff58241c0\"},\"headline\":\"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)\",\"datePublished\":\"2014-12-12T14:00:25+00:00\",\"dateModified\":\"2023-12-05T14:28:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html\"},\"wordCount\":2575,\"commentCount\":8,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Servlet\",\"Ultimate\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html\",\"name\":\"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2014-12-12T14:00:25+00:00\",\"dateModified\":\"2023-12-05T14:28:55+00:00\",\"description\":\"Check out our Java Servlet Tutorial where we explain the concepts related to Java Servlets. You can also download our FREE Java Servlet Ultimate Guide!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/12\\\/java-servlet-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f55d97bcbb667fad09fd920ff58241c0\",\"name\":\"Kaushik Pal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/adef7fefc125ddbf673e62d843a68595a91b537d91016bb71e69cc8f7a148ec1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/adef7fefc125ddbf673e62d843a68595a91b537d91016bb71e69cc8f7a148ec1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/adef7fefc125ddbf673e62d843a68595a91b537d91016bb71e69cc8f7a148ec1?s=96&d=mm&r=g\",\"caption\":\"Kaushik Pal\"},\"description\":\"Kaushik has 16 years of experience as a technical architect and software consultant in enterprise application and product development. He has interest in new technology and innovation area along with technical writing. His main focus is on web architecture, web technologies, java\\\/j2ee, Open source, big data and semantic technologies. He has demonstrated his expertise in requirement analysis, architecture design &amp; implementation, technical use case preparation, and software development. His experience has spanned in different domains like Insurance, banking, airlines, shipping, document management etc. Kaushik worked with a wide variety of technologies starting from Mainframe (IBM S\\\/390), midrange (AS\\\/400), web technologies and open source area. He has worked with clients like IBM, Lexmark, United Airlines and many more.\",\"sameAs\":[\"http:\\\/\\\/www.techalpine.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/kaushik-pal\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)","description":"Check out our Java Servlet Tutorial where we explain the concepts related to Java Servlets. You can also download our FREE Java Servlet Ultimate Guide!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)","og_description":"Check out our Java Servlet Tutorial where we explain the concepts related to Java Servlets. You can also download our FREE Java Servlet Ultimate Guide!","og_url":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-12-12T14:00:25+00:00","article_modified_time":"2023-12-05T14:28:55+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Kaushik Pal","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Kaushik Pal","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html"},"author":{"name":"Kaushik Pal","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f55d97bcbb667fad09fd920ff58241c0"},"headline":"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)","datePublished":"2014-12-12T14:00:25+00:00","dateModified":"2023-12-05T14:28:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html"},"wordCount":2575,"commentCount":8,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Servlet","Ultimate"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html","name":"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2014-12-12T14:00:25+00:00","dateModified":"2023-12-05T14:28:55+00:00","description":"Check out our Java Servlet Tutorial where we explain the concepts related to Java Servlets. You can also download our FREE Java Servlet Ultimate Guide!","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/12\/java-servlet-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Java Servlet Tutorial \u2013 The ULTIMATE Guide (PDF Download)"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f55d97bcbb667fad09fd920ff58241c0","name":"Kaushik Pal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/adef7fefc125ddbf673e62d843a68595a91b537d91016bb71e69cc8f7a148ec1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/adef7fefc125ddbf673e62d843a68595a91b537d91016bb71e69cc8f7a148ec1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/adef7fefc125ddbf673e62d843a68595a91b537d91016bb71e69cc8f7a148ec1?s=96&d=mm&r=g","caption":"Kaushik Pal"},"description":"Kaushik has 16 years of experience as a technical architect and software consultant in enterprise application and product development. He has interest in new technology and innovation area along with technical writing. His main focus is on web architecture, web technologies, java\/j2ee, Open source, big data and semantic technologies. He has demonstrated his expertise in requirement analysis, architecture design &amp; implementation, technical use case preparation, and software development. His experience has spanned in different domains like Insurance, banking, airlines, shipping, document management etc. Kaushik worked with a wide variety of technologies starting from Mainframe (IBM S\/390), midrange (AS\/400), web technologies and open source area. He has worked with clients like IBM, Lexmark, United Airlines and many more.","sameAs":["http:\/\/www.techalpine.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/kaushik-pal"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/34048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/435"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=34048"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/34048\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=34048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=34048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=34048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}