{"id":265,"date":"2018-07-27T06:34:00","date_gmt":"2018-07-27T06:34:00","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=265"},"modified":"2018-08-02T08:33:46","modified_gmt":"2018-08-02T08:33:46","slug":"java-servlets-implementation","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/java-servlets-implementation\/","title":{"rendered":"Java Servlets &#8211; Implementation"},"content":{"raw":"<strong>Tomcat Installation<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Step 1: Download and Install Tomcat<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Go to <a href=\"https:\/\/tomcat.apache.org\/download-80.cgi\">https:\/\/tomcat.apache.org\/download-80.cgi <\/a>to download and install Tomcat. This is shown in Figure 29.1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-266 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157.png\" alt=\"\" width=\"525\" height=\"298\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 2. Then go to the Binary Distribution\/Core\/ and download the \"zip\" package.<\/strong>\r\n\r\n&nbsp;\r\n\r\nThis is shown in Figure 29.2.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-267 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158.png\" alt=\"\" width=\"537\" height=\"350\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Check the installed directory to ensure that it contains the following sub-directories like bin folder, logs folder, webapps folder, work folder, temp folder, conf folder and lib folder as shown in Figure 29.3.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-268 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159.png\" alt=\"\" width=\"591\" height=\"334\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Create Environment Variables<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now, We need to create an environment variable called \"JAVA_HOME\" and set it to our JDK installed directory. To create the JAVA_HOME environment variable in Windows, follow these steps:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Step 1<\/strong><strong>: <\/strong>Right Click \u201cMy Computer\u201d then go to Properties. This is shown in Figure 29.4.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-269 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160.png\" alt=\"\" width=\"477\" height=\"379\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Step 2<\/strong><strong>: <\/strong>Select \u201cAdvanced System Settings\u201d on the left side of the control panel. This is shown in Figure\r\n\r\n29.5.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-270 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161.png\" alt=\"\" width=\"575\" height=\"340\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Step 3: <\/strong>Select \u201cEnvironment variables\u201d in the System properties dialog box. This is shown in Figure 29.6.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-271 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162.png\" alt=\"\" width=\"561\" height=\"338\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Step 4<\/strong><strong>: <\/strong>Select \"New\" (or \"Edit\" for modification). In \"Variable Name\", enter \"JAVA_HOME\". In \"Variable Value\", enter your JDK installed directory (e.g., \"c:\\Program Files\\Java\\jdk1.8.0_05\"). This is shown in Figure 29.7.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-272 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163.png\" alt=\"\" width=\"546\" height=\"326\" \/><\/p>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-273 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164.png\" alt=\"\" width=\"542\" height=\"326\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>SERVLET IMPLEMENTATION<\/strong>\r\n\r\n&nbsp;\r\n\r\nTo create a new Servlet do the following steps:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Step 1<\/strong><strong>: <\/strong>Go to webapps folder in Tomcat Apache directory, then create New Folder and name it as \u201cHelloWorld\u201d. This is shown in Figure 29.8.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-274 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165.png\" alt=\"\" width=\"542\" height=\"326\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Step 2<\/strong><strong>: <\/strong>Create a new folder inside the HelloWorld folder and name it as \u201cWEB-INF\u201d.Inside the WEB-INF folder, create a folder name \u201cclasses\u201d as shown in Figure 29.9.<\/p>\r\n&nbsp;\r\n\r\n<strong>Step 3<\/strong><strong>:<\/strong>Create a Servlet page inside the Helloworld folder and name it as \u201cHelloWorld.java\u201d.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-275 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166.png\" alt=\"\" width=\"522\" height=\"340\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0HelloWorld.java<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class Hello extends HttpServlet\r\n\r\n{\r\n\r\npublic void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException\r\n\r\n{\r\n\r\nres.setContentType(\"text\/html\");\r\n\r\nPrintWriter out=res.getWriter();\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out.println(\"&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello WorldServlet&lt;\/title&gt;&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;&lt;h1&gt;HelloWorld&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Step 4<\/strong><strong>: <\/strong>Inside the WEB-INF folder, Create an XML file named \u201cweb.xml\u201d<strong>.<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n\r\n&lt;web-app 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\"version=\"3.1\"&gt;\r\n\r\n&lt;session-config&gt;\r\n\r\n&lt;session-timeout&gt; 30\r\n\r\n&lt;\/session-timeout&gt;\r\n\r\n&lt;\/session-config&gt;\r\n\r\n&lt;servlet&gt;\r\n\r\n&lt;servlet-name&gt;hello&lt;\/servlet-name&gt;\r\n\r\n&lt;servlet-class&gt;HelloWorld&lt;\/servlet-class&gt;\r\n\r\n&lt;\/servlet&gt;\r\n\r\n&lt;servlet-mapping&gt;\r\n\r\n&lt;servlet-name&gt;hello&lt;\/servlet-name&gt;\r\n\r\n&lt;url-pattern&gt;\/HelloWorld&lt;\/url-pattern&gt;\r\n\r\n&lt;\/servlet-mapping&gt; &lt;\/web-app&gt;\r\n\r\n&nbsp;\r\n\r\n<strong>Step 5<\/strong>: Compile HelloWorld.java file.\r\n\r\n&nbsp;\r\n\r\nFollow these steps in compilation:\r\n\r\n&nbsp;\r\n<ol>\r\n \t<li><span style=\"font-size: 1em\">Go to cmd prompt, then go to the directory \u201cC:\\apache-tomcat-8.0.14\\webapps\\HelloWorld\u201d as shown in Figure 29.10.<\/span><\/li>\r\n<\/ol>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-276 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167.png\" alt=\"\" width=\"610\" height=\"307\" \/><\/p>\r\n&nbsp;\r\n<ol start=\"2\">\r\n \t<li>Now we need to include the servlet-api.jar file to compile the HelloWorld servlet file.<\/li>\r\n \t<li>Go to lib folder inside the apache folder ( for example : C:\\apache-tomcat-8.0.14\\lib\\servlet-api.jar)<\/li>\r\n \t<li>Now compile the Servlet by specifying the classpath of the servlet-api.jar as shown in Figure 29.11. <strong>javac -cp \"C:\\apache-tomcat-8.0.14\\lib\\servlet-api.jar\" HelloWorld.java<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-277 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168.png\" alt=\"\" width=\"594\" height=\"249\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n\u00a0 \u00a0 5. After compilation, the class file will be generated inside the HelloWorld folder.\r\n\r\n<span style=\"font-size: 1em\">6. Cut the HelloWorld.class file and paste it inside the WEB-INF\\classes folder. This is shown in FigurE 29.12<\/span>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-278 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169.png\" alt=\"\" width=\"601\" height=\"330\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Step 6<\/strong><strong>: <\/strong>Start the Tomcat Server.\r\n\r\n&nbsp;\r\n\r\nFollow these steps for starting the server.\r\n<ol>\r\n \t<li style=\"text-align: justify\">Go to bin folder inside the apache folder and double click the startup.bat file. It will start the Tomcat server. This is shown in Figure 29.13.<\/li>\r\n<\/ol>\r\n<img class=\"size-full wp-image-279 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170.png\" alt=\"\" width=\"582\" height=\"345\" \/>\r\n<ol start=\"2\">\r\n \t<li>Now go to web browser and type the following url.<\/li>\r\n<\/ol>\r\nH<strong style=\"text-align: initial;font-size: 1em\">ttp:\/\/localhost:8080\/HelloWorld\/HelloWorld<\/strong>\r\n\r\nThe output will be displayed as shown in Figure 29.14\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-280 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171.png\" alt=\"\" width=\"551\" height=\"349\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 The doGet method<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<\/strong><\/p>\r\n<p style=\"text-align: justify\">This method services a GET request. The method uses request to get the information that was sent to it and the method does not return a value; instead, it uses response to get an I\/O stream, and outputs its response. Since the method does I\/O, it can throw an IOException. Any other type of exception should be encapsulated as a ServletException.<\/p>\r\n<strong>Parameters:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The <strong>request<\/strong> object is an <a href=\"https:\/\/tomcat.apache.org\/tomcat-5.5-doc\/servletapi\/javax\/servlet\/http\/HttpServletRequest.html\"><strong>HttpServletRequest<\/strong> <\/a>object that contains the request the client has made of the servlet.<\/p>\r\n<p style=\"text-align: justify\">The <strong>response<\/strong> object is an <a href=\"https:\/\/tomcat.apache.org\/tomcat-5.5-doc\/servletapi\/javax\/servlet\/http\/HttpServletResponse.html\"><strong>HttpServletResponse<\/strong> <\/a>object that contains the response the servlet sends to the client.<\/p>\r\n&nbsp;\r\n\r\n<strong>Using the HttpServletResponse<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The second parameter to doGet (or doPost) is HttpServletResponse response. Everything sent via the Web has a \u201cMIME type\u201d. The first thing we <em>must<\/em> do with response is set the <em>MIME type<\/em> of our reply.<\/p>\r\n&nbsp;\r\n\r\nresponse.setContentType(\"text\/html\");\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This tells the client to interpret the page as HTML. Because we will be outputting character data, we need a PrintWriter, handily provided for us by the getWriter method of response.<\/p>\r\n&nbsp;\r\n\r\nprintWriter out = response.getWriter();\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Then use the println method of out one or more times to write the response to the client .<\/p>\r\n&nbsp;\r\n\r\nout.println(docType +\"&lt;HTML&gt;\\n\" +\"&lt;HEAD&gt; ... &lt;\/BODY&gt;&lt;\/HTML&gt;\");\r\n\r\n&nbsp;\r\n\r\n<strong>First Servlet Program<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form\u00a0 method = \"get\"\r\n\r\naction=\"http:\\\\127.0.0.1:8080\\servlet-html\\servlet\\Example\"&gt;\r\n\r\n&lt;<span style=\"font-size: 1em;text-align: initial\">input type=submit value = \"submit\"&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Sample Servlet Program<\/strong>\r\n\r\n&nbsp;\r\n\r\nFollowing is the sample source code structure of a servlet example.\r\n\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\npublic class Example extends HttpServlet\r\n\r\n{\r\n\r\npublic void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException ,IOException\r\n\r\n{\r\n\r\nres.setContentType(\"text\/html\");\r\n\r\nPrintWriter out = res.getWriter();\r\n\r\nout.println(\"&lt;html&gt;Hai----JNF&lt;\/html&gt;\");\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Hello.java<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example illustrates about implementing a 'Hello' Servlet that extends the HttpServlet class. The code provides the implementation of the doGet() method. Notice that get request is the default request method.<\/p>\r\n<p style=\"text-align: justify\">Initially we set the content type to be text\/html. Then we use the getWriter() method of the HttpServletResponse to write the response to the servlet which returns an OuputStream. This OutputStream is assigned to a PrintWriter object 'out'. So now using this 'out' object, we write the html response to the client.<\/p>\r\n&nbsp;\r\n\r\nThis html code will print the Hello World text in green color in the client response browser window.\r\n\r\n<\/div>\r\nimport java.io.*;\r\n\r\nimport java.util.*;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\npublic class Hello extends HttpServlet\r\n\r\n{\r\n\r\nprotected void doGet(HttpServletRequest req, HttpServletResponse res)\r\n\r\nthrows ServletException, IOException\r\n\r\n{\r\n\r\nres.setContentType(\"text\/html\");\r\n\r\nPrintWriter out = res.getWriter();\r\n\r\nout.println(\"&lt;html&gt;\");\u00a0 out.println(\"&lt;body&gt;\");\r\n\r\nout.println(\"&lt;h1&gt;&lt;font color='green'&gt;Hello World!&lt;\/font&gt;&lt;\/h1&gt;\");\r\n\r\nout.println(\"&lt;\/body&gt;\");\r\n\r\nout.println(\"&lt;\/html&gt;\");\r\n\r\nout.close();\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output of Hello Servlet<\/strong>\r\n\r\n&nbsp;\r\n\r\nOpen a browser window and type the path where our servlet is stored i.e.,\r\n\r\n&nbsp;\r\n\r\n<strong>localhost:8084\/MyFirstServlet\/servlet\/Hello<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe output message \"HelloWorld\" is displayed in green color as shown in Figure 29.15.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-281 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172.png\" alt=\"\" width=\"530\" height=\"279\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Parameters to doGet<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The parameters are the way in which a user can send information to the Web Server. Input parameters from the client HTML form is read from the HttpServletRequest parameter. Our first program does not get any input. Output is sent through the HttpServletResponse object, which we have named as response.I\/O in Java is very flexible but also quite complex, so this object acts as an \u201cassistant\u201d<\/p>\r\n&nbsp;\r\n\r\n<strong>Getting the parameters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">From the request object we can read the parameter submitted by the user's browser either through an HTTP GET or POST method.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Basically we need to know that we can call the request.getParameter(paramname) method where the paramname is the name of paramater to get the passed parameter from inside the Servlet.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Input parameters are retrieved via messages to the HttpServletRequest object request. Most of the interesting methods of the class HttpServletRequest are inherited from the super interface ServletRequest.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0public Enumeration getParameterNames()<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This returns an Enumeration of the parameter names. If the key names are not known, the Servlet can enumerate (iterate) through the keys and obtain data in that manner.<\/p>\r\n&nbsp;\r\n\r\n<strong>Enumeration enum = request.getParameterNames();<\/strong>\r\n\r\n<strong>while(enum.hasMoreElements()) {<\/strong>\r\n\r\n<strong>String key = (String) enum.nextElement();<\/strong>\r\n\r\n<strong>\/\/ \u2026<\/strong>\r\n\r\n&nbsp;\r\n\r\nIf no parameters are retrieved, it returns an empty Enumeration.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are two important methods used with HttpServletRequest object to retrieve the parameters sent from the client HTML page:<\/p>\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0<strong>public String getParameter(String name)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This method returns the value of the parameter name as a String. If the parameter doesn\u2019t exist, it returns null. If name has multiple values, only the first value is returned.<\/p>\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0<strong>public String[] getParameterValues(name)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This method returns an array of values of the parameter name. If the parameter doesn\u2019t exist, it returns null.Here, we illustrate with an example program how we can pass parameters to Servlets, that can be read and processed by the Servlet.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example 1 - Passing Parameters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">First we develop an HTML page that sends information to a Servlet. To implement this requirement we have to make one html form. In this example we have created an HTML form that has a drop-down list in which user can select any color displayed in the list. A submit button is provided which on pressing it, the request will go to the server and servlet will do the corresponding processing.<\/p>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Example1.html<\/strong>\r\n\r\n&nbsp;\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form method = \"get\" action=\"http:\\\\127.0.0.1:8080\\servlet-html\\servlet\\Example1\"&gt;\r\n\r\n&lt;input type=submit value=\"submit\"&gt;\r\n\r\n&lt;input type=input name=\"Read\" value=\"\"&gt;\r\n\r\n&lt;select name=\u201cColor\"&gt;\r\n\r\n&lt;option &gt;Red\r\n\r\n&lt;option &gt;Blue\r\n\r\n&lt;option value=\"dss\"&gt;Green\r\n\r\n&lt;option value=\"dss\"&gt;Yellow\r\n\r\n&lt;\/select&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n\r\n<strong>Example1.java<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the servlet class the value is read from the form by using the method getParameter(). The output is then displayed to the client by the object of the PrintWriter class.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\npublic class Example1 extends HttpServlet\r\n\r\n{\r\n\r\npublic void doGet(HttpServletRequest req,HttpServletResponse res)\r\n\r\nthrows ServletException ,IOException\r\n\r\n{\r\n\r\nres.setContentType(\"text\/html\");\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">PrintWriter out = res.getWriter();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String str= req.getParameter(\"Read\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String str2= req.getParameter(\u201cColor\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out.println(\"Hai\u00a0 \"+ str + str2);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 <\/strong>\r\n\r\n<strong>Example 2 - Passing Parameters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example illustrates about sending the parameters through which a user can send information to the Web Server. For example, we write a HTML page in the client side where we send the message entered in a text box to the server, so that the Servlet reads this text value and send it as response to the client. And we will also have one submit button, on pressing the submit button the request will go to the server and Servlet will do the corresponding validation.<\/p>\r\n&nbsp;\r\n\r\n<strong>ParamPassing.html<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form method=GET action=\"http:\/\/localhost:8080\/servlet-html\/ParamPassing.html\"&gt;\r\n\r\n&lt;input type=submit name=\"Submit\" value=\"First\"&gt;\r\n\r\n&lt;input type=text name=\"TextBox\" value=\"\"&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0ParamPassing.java<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The servlet class collects the value from the text box by using the method getParameter(). The output is then displayed to the client by the object of the PrintWriter class.<\/p>\r\n&nbsp;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class ParamPassing extends HttpServlet\r\n\r\n{\r\n\r\npublic void doGet(HttpServletRequestreq,HttpServletResponse res) throws IOException,ServletException\r\n\r\n{\r\n\r\nres.setContentType(\"text\/html\");\r\n\r\nPrintWriterpw=res.getWriter();\r\n\r\nString str=req.getParameter(\"TextBox\"); \/\/pw.println(\"&lt;html&gt;&lt;body&gt; \"+str+ \"&lt;\/body&gt;&lt;\/html&gt;\"); pw.println(str);\r\n\r\n}\r\n\r\npublic void doPost(HttpServletRequestreq, HttpServletResponse res) throws IOException,ServletException\r\n\r\n{\r\n\r\ndoGet(req,res);\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe output of this example is shown in Figure 29.16.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-282 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173.png\" alt=\"\" width=\"575\" height=\"246\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0The doPost method<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can use <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/servlet\/http\/HttpServlet.html#doPost%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse%29\">doPost() <\/a>when we want to intercept on <a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec9.html#sec9.5\">HTTP POST requests. <\/a>The doPost() performs invisible data submission whereas doGet() performs visible data submission. The doGet() method is not recommended for everything and doPost() is generally used to carry passwords.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Using \"GET\" the request parameters are passed to the server by appending at the end of the URL, whereas in a \"POST\" request form elements or parameters are passed as a part of HTTP body and does not append at the end of URL. So some sensitive information is sent to the server, a POST request is sent.<\/p>\r\n&nbsp;\r\n\r\n<strong>HelloPost.html<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to send a POST request to server, we need to mention the method to be \"POST\" in the form as shown in the HTML code.<\/p>\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;\r\n\r\nMy Page\r\n\r\n&lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;form\u00a0 method=\"Post\"\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">action=\"http:\/\/127.0.0.1:8080\/servlet\/HelloPost\"&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;input type=\"text\" name=\"username\" value=\"\"&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;input type=submit &gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong> HelloPost.java<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">By looking at POST request, the servlet engine will call appropriate class as mapped in web.xml and invokes the doget() method, which in turn calls the dopost() method.<\/p>\r\n&nbsp;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class HelloPost extends HttpServlet\r\n\r\n{\r\n\r\npublic void doPost(HttpServletRequest req,HttpServletResponse res)\r\n\r\nthrows ServletException, IOException\r\n\r\n{\r\n\r\nres.setContentType(\"text\/html\");\r\n\r\nPrintWriter out=res.getWriter();\r\n\r\nout.println(\"&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello\r\n\r\nPage&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;Hello&lt;\/body&gt;&lt;\/html&gt;\");\r\n\r\n}\r\n\r\n}\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Passing Parameters and doPost method<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example illustrates the use of doPost by overriding this method. The doPost method inturn invokes the doGet method which reads the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport javax.servlet.*;\r\n\r\nimport javax.servlet.http.*;\r\n\r\npublic class Test extends HttpServlet\r\n\r\n{\r\n\r\npublic void doGet(HttpServletRequest req, HttpServletResponse res) throws\r\n\r\nServletException, IOException\r\n\r\n{\r\n\r\nres.setContentType(\u201ctext\/html\u201d); PrintWriter out = res.getWriter(); String pin = req.getParameter(\u201cto\u201d); String orig = req.getParameter(\u201cfrom\u201d); out.println(\u201cSending page to \u201c + pin + \u201c from \u201c + orig);\r\n\r\n\/<span style=\"font-size: 1em\">\/ Actually send the page.<\/span>\r\n\r\n}\r\n\r\npublic void doPost(HttpServletRequest in, HttpServletResponse out) throws\r\n\r\nServletException, IOException\r\n\r\n{\r\n\r\ndoGet(in, out);\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module we have discussed about the installation of Tomcat to work with Servlets. The module covers the method of acquiring the required software components to use Tomcat for web application development, thereby demonstrating how to implement Servlets. Ultimately, this section has provided simple Servlet programs to have a clear understanding about creating dynamic web applications.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n<ul>\r\n \t<li><em>https:\/\/www.cis.upenn.edu\/~matuszek\/cit597-2003\/...\/34-<strong>servlets<\/strong>.<strong>ppt<\/strong><\/em><\/li>\r\n \t<li>Herbert Schildt, \" <em>Java: The Complete Reference\", 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n&nbsp;","rendered":"<p><strong>Tomcat Installation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 1: Download and Install Tomcat<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Go to <a href=\"https:\/\/tomcat.apache.org\/download-80.cgi\">https:\/\/tomcat.apache.org\/download-80.cgi <\/a>to download and install Tomcat. This is shown in Figure 29.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-266 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157.png\" alt=\"\" width=\"525\" height=\"298\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157.png 525w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-157-350x199.png 350w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2. Then go to the Binary Distribution\/Core\/ and download the &#8220;zip&#8221; package.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>This is shown in Figure 29.2.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-267 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158.png\" alt=\"\" width=\"537\" height=\"350\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158.png 537w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158-300x196.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158-225x147.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-158-350x228.png 350w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Check the installed directory to ensure that it contains the following sub-directories like bin folder, logs folder, webapps folder, work folder, temp folder, conf folder and lib folder as shown in Figure 29.3.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-268 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159.png\" alt=\"\" width=\"591\" height=\"334\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159.png 591w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159-225x127.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-159-350x198.png 350w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Create Environment Variables<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now, We need to create an environment variable called &#8220;JAVA_HOME&#8221; and set it to our JDK installed directory. To create the JAVA_HOME environment variable in Windows, follow these steps:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Step 1<\/strong><strong>: <\/strong>Right Click \u201cMy Computer\u201d then go to Properties. This is shown in Figure 29.4.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-269 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160.png\" alt=\"\" width=\"477\" height=\"379\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160.png 477w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160-300x238.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160-225x179.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-160-350x278.png 350w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong><strong>: <\/strong>Select \u201cAdvanced System Settings\u201d on the left side of the control panel. This is shown in Figure<\/p>\n<p>29.5.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-270 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161.png\" alt=\"\" width=\"575\" height=\"340\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161.png 575w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161-300x177.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161-225x133.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-161-350x207.png 350w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Step 3: <\/strong>Select \u201cEnvironment variables\u201d in the System properties dialog box. This is shown in Figure 29.6.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-271 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162.png\" alt=\"\" width=\"561\" height=\"338\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162.png 561w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162-300x181.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-162-350x211.png 350w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Step 4<\/strong><strong>: <\/strong>Select &#8220;New&#8221; (or &#8220;Edit&#8221; for modification). In &#8220;Variable Name&#8221;, enter &#8220;JAVA_HOME&#8221;. In &#8220;Variable Value&#8221;, enter your JDK installed directory (e.g., &#8220;c:\\Program Files\\Java\\jdk1.8.0_05&#8221;). This is shown in Figure 29.7.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-272 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163.png\" alt=\"\" width=\"546\" height=\"326\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163.png 546w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-163-350x209.png 350w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-273 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164.png\" alt=\"\" width=\"542\" height=\"326\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164.png 542w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164-300x180.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164-225x135.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-164-350x211.png 350w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>SERVLET IMPLEMENTATION<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>To create a new Servlet do the following steps:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Step 1<\/strong><strong>: <\/strong>Go to webapps folder in Tomcat Apache directory, then create New Folder and name it as \u201cHelloWorld\u201d. This is shown in Figure 29.8.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-274 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165.png\" alt=\"\" width=\"542\" height=\"326\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165.png 542w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165-300x180.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165-225x135.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-165-350x211.png 350w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Step 2<\/strong><strong>: <\/strong>Create a new folder inside the HelloWorld folder and name it as \u201cWEB-INF\u201d.Inside the WEB-INF folder, create a folder name \u201cclasses\u201d as shown in Figure 29.9.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong><strong>:<\/strong>Create a Servlet page inside the Helloworld folder and name it as \u201cHelloWorld.java\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-275 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166.png\" alt=\"\" width=\"522\" height=\"340\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166.png 522w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166-300x195.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166-225x147.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-166-350x228.png 350w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0HelloWorld.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class Hello extends HttpServlet<\/p>\n<p>{<\/p>\n<p>public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException<\/p>\n<p>{<\/p>\n<p>res.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p>PrintWriter out=res.getWriter();<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out.println(&#8220;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello WorldServlet&lt;\/title&gt;&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;&lt;h1&gt;HelloWorld&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Step 4<\/strong><strong>: <\/strong>Inside the WEB-INF folder, Create an XML file named \u201cweb.xml\u201d<strong>.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<\/p>\n<p>&lt;web-app xmlns=&#8221;http:\/\/xmlns.jcp.org\/xml\/ns\/javaee&#8221; xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:schemaLocation=&#8221;http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_3_1.xsd&#8221;version=&#8221;3.1&#8243;&gt;<\/p>\n<p>&lt;session-config&gt;<\/p>\n<p>&lt;session-timeout&gt; 30<\/p>\n<p>&lt;\/session-timeout&gt;<\/p>\n<p>&lt;\/session-config&gt;<\/p>\n<p>&lt;servlet&gt;<\/p>\n<p>&lt;servlet-name&gt;hello&lt;\/servlet-name&gt;<\/p>\n<p>&lt;servlet-class&gt;HelloWorld&lt;\/servlet-class&gt;<\/p>\n<p>&lt;\/servlet&gt;<\/p>\n<p>&lt;servlet-mapping&gt;<\/p>\n<p>&lt;servlet-name&gt;hello&lt;\/servlet-name&gt;<\/p>\n<p>&lt;url-pattern&gt;\/HelloWorld&lt;\/url-pattern&gt;<\/p>\n<p>&lt;\/servlet-mapping&gt; &lt;\/web-app&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong>: Compile HelloWorld.java file.<\/p>\n<p>&nbsp;<\/p>\n<p>Follow these steps in compilation:<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><span style=\"font-size: 1em\">Go to cmd prompt, then go to the directory \u201cC:\\apache-tomcat-8.0.14\\webapps\\HelloWorld\u201d as shown in Figure 29.10.<\/span><\/li>\n<\/ol>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-276 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167.png\" alt=\"\" width=\"610\" height=\"307\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167.png 610w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167-300x151.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-167-350x176.png 350w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/p>\n<p>&nbsp;<\/p>\n<ol start=\"2\">\n<li>Now we need to include the servlet-api.jar file to compile the HelloWorld servlet file.<\/li>\n<li>Go to lib folder inside the apache folder ( for example : C:\\apache-tomcat-8.0.14\\lib\\servlet-api.jar)<\/li>\n<li>Now compile the Servlet by specifying the classpath of the servlet-api.jar as shown in Figure 29.11. <strong>javac -cp &#8220;C:\\apache-tomcat-8.0.14\\lib\\servlet-api.jar&#8221; HelloWorld.java<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-277 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168.png\" alt=\"\" width=\"594\" height=\"249\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168.png 594w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168-300x126.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168-65x27.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168-225x94.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-168-350x147.png 350w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0 \u00a0 5. After compilation, the class file will be generated inside the HelloWorld folder.<\/p>\n<p><span style=\"font-size: 1em\">6. Cut the HelloWorld.class file and paste it inside the WEB-INF\\classes folder. This is shown in FigurE 29.12<\/span><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-278 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169.png\" alt=\"\" width=\"601\" height=\"330\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169.png 601w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169-300x165.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-169-350x192.png 350w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong><strong>: <\/strong>Start the Tomcat Server.<\/p>\n<p>&nbsp;<\/p>\n<p>Follow these steps for starting the server.<\/p>\n<ol>\n<li style=\"text-align: justify\">Go to bin folder inside the apache folder and double click the startup.bat file. It will start the Tomcat server. This is shown in Figure 29.13.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-279 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170.png\" alt=\"\" width=\"582\" height=\"345\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170.png 582w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170-225x133.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-170-350x207.png 350w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/p>\n<ol start=\"2\">\n<li>Now go to web browser and type the following url.<\/li>\n<\/ol>\n<p>H<strong style=\"text-align: initial;font-size: 1em\">ttp:\/\/localhost:8080\/HelloWorld\/HelloWorld<\/strong><\/p>\n<p>The output will be displayed as shown in Figure 29.14<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-280 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171.png\" alt=\"\" width=\"551\" height=\"349\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171.png 551w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171-300x190.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171-225x143.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-171-350x222.png 350w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 The doGet method<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<\/strong><\/p>\n<p style=\"text-align: justify\">This method services a GET request. The method uses request to get the information that was sent to it and the method does not return a value; instead, it uses response to get an I\/O stream, and outputs its response. Since the method does I\/O, it can throw an IOException. Any other type of exception should be encapsulated as a ServletException.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The <strong>request<\/strong> object is an <a href=\"https:\/\/tomcat.apache.org\/tomcat-5.5-doc\/servletapi\/javax\/servlet\/http\/HttpServletRequest.html\"><strong>HttpServletRequest<\/strong> <\/a>object that contains the request the client has made of the servlet.<\/p>\n<p style=\"text-align: justify\">The <strong>response<\/strong> object is an <a href=\"https:\/\/tomcat.apache.org\/tomcat-5.5-doc\/servletapi\/javax\/servlet\/http\/HttpServletResponse.html\"><strong>HttpServletResponse<\/strong> <\/a>object that contains the response the servlet sends to the client.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Using the HttpServletResponse<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The second parameter to doGet (or doPost) is HttpServletResponse response. Everything sent via the Web has a \u201cMIME type\u201d. The first thing we <em>must<\/em> do with response is set the <em>MIME type<\/em> of our reply.<\/p>\n<p>&nbsp;<\/p>\n<p>response.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This tells the client to interpret the page as HTML. Because we will be outputting character data, we need a PrintWriter, handily provided for us by the getWriter method of response.<\/p>\n<p>&nbsp;<\/p>\n<p>printWriter out = response.getWriter();<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Then use the println method of out one or more times to write the response to the client .<\/p>\n<p>&nbsp;<\/p>\n<p>out.println(docType +&#8221;&lt;HTML&gt;\\n&#8221; +&#8221;&lt;HEAD&gt; &#8230; &lt;\/BODY&gt;&lt;\/HTML&gt;&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>First Servlet Program<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form\u00a0 method = &#8220;get&#8221;<\/p>\n<p>action=&#8221;http:\\\\127.0.0.1:8080\\servlet-html\\servlet\\Example&#8221;&gt;<\/p>\n<p>&lt;<span style=\"font-size: 1em;text-align: initial\">input type=submit value = &#8220;submit&#8221;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 Sample Servlet Program<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Following is the sample source code structure of a servlet example.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>public class Example extends HttpServlet<\/p>\n<p>{<\/p>\n<p>public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException ,IOException<\/p>\n<p>{<\/p>\n<p>res.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p>PrintWriter out = res.getWriter();<\/p>\n<p>out.println(&#8220;&lt;html&gt;Hai&#8212;-JNF&lt;\/html&gt;&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Hello.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example illustrates about implementing a &#8216;Hello&#8217; Servlet that extends the HttpServlet class. The code provides the implementation of the doGet() method. Notice that get request is the default request method.<\/p>\n<p style=\"text-align: justify\">Initially we set the content type to be text\/html. Then we use the getWriter() method of the HttpServletResponse to write the response to the servlet which returns an OuputStream. This OutputStream is assigned to a PrintWriter object &#8216;out&#8217;. So now using this &#8216;out&#8217; object, we write the html response to the client.<\/p>\n<p>&nbsp;<\/p>\n<p>This html code will print the Hello World text in green color in the client response browser window.<\/p>\n<\/div>\n<p>import java.io.*;<\/p>\n<p>import java.util.*;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>public class Hello extends HttpServlet<\/p>\n<p>{<\/p>\n<p>protected void doGet(HttpServletRequest req, HttpServletResponse res)<\/p>\n<p>throws ServletException, IOException<\/p>\n<p>{<\/p>\n<p>res.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p>PrintWriter out = res.getWriter();<\/p>\n<p>out.println(&#8220;&lt;html&gt;&#8221;);\u00a0 out.println(&#8220;&lt;body&gt;&#8221;);<\/p>\n<p>out.println(&#8220;&lt;h1&gt;&lt;font color=&#8217;green&#8217;&gt;Hello World!&lt;\/font&gt;&lt;\/h1&gt;&#8221;);<\/p>\n<p>out.println(&#8220;&lt;\/body&gt;&#8221;);<\/p>\n<p>out.println(&#8220;&lt;\/html&gt;&#8221;);<\/p>\n<p>out.close();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output of Hello Servlet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Open a browser window and type the path where our servlet is stored i.e.,<\/p>\n<p>&nbsp;<\/p>\n<p><strong>localhost:8084\/MyFirstServlet\/servlet\/Hello<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The output message &#8220;HelloWorld&#8221; is displayed in green color as shown in Figure 29.15.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-281 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172.png\" alt=\"\" width=\"530\" height=\"279\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172.png 530w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172-300x158.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172-225x118.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-172-350x184.png 350w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 Parameters to doGet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The parameters are the way in which a user can send information to the Web Server. Input parameters from the client HTML form is read from the HttpServletRequest parameter. Our first program does not get any input. Output is sent through the HttpServletResponse object, which we have named as response.I\/O in Java is very flexible but also quite complex, so this object acts as an \u201cassistant\u201d<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Getting the parameters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From the request object we can read the parameter submitted by the user&#8217;s browser either through an HTTP GET or POST method.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Basically we need to know that we can call the request.getParameter(paramname) method where the paramname is the name of paramater to get the passed parameter from inside the Servlet.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Input parameters are retrieved via messages to the HttpServletRequest object request. Most of the interesting methods of the class HttpServletRequest are inherited from the super interface ServletRequest.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0public Enumeration getParameterNames()<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This returns an Enumeration of the parameter names. If the key names are not known, the Servlet can enumerate (iterate) through the keys and obtain data in that manner.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Enumeration enum = request.getParameterNames();<\/strong><\/p>\n<p><strong>while(enum.hasMoreElements()) {<\/strong><\/p>\n<p><strong>String key = (String) enum.nextElement();<\/strong><\/p>\n<p><strong>\/\/ \u2026<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>If no parameters are retrieved, it returns an empty Enumeration.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are two important methods used with HttpServletRequest object to retrieve the parameters sent from the client HTML page:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0<strong>public String getParameter(String name)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This method returns the value of the parameter name as a String. If the parameter doesn\u2019t exist, it returns null. If name has multiple values, only the first value is returned.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0<strong>public String[] getParameterValues(name)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This method returns an array of values of the parameter name. If the parameter doesn\u2019t exist, it returns null.Here, we illustrate with an example program how we can pass parameters to Servlets, that can be read and processed by the Servlet.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1 &#8211; Passing Parameters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">First we develop an HTML page that sends information to a Servlet. To implement this requirement we have to make one html form. In this example we have created an HTML form that has a drop-down list in which user can select any color displayed in the list. A submit button is provided which on pressing it, the request will go to the server and servlet will do the corresponding processing.<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Example1.html<\/strong><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0 \u00a0 \u00a0&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form method = &#8220;get&#8221; action=&#8221;http:\\\\127.0.0.1:8080\\servlet-html\\servlet\\Example1&#8243;&gt;<\/p>\n<p>&lt;input type=submit value=&#8221;submit&#8221;&gt;<\/p>\n<p>&lt;input type=input name=&#8221;Read&#8221; value=&#8221;&#8221;&gt;<\/p>\n<p>&lt;select name=\u201cColor&#8221;&gt;<\/p>\n<p>&lt;option &gt;Red<\/p>\n<p>&lt;option &gt;Blue<\/p>\n<p>&lt;option value=&#8221;dss&#8221;&gt;Green<\/p>\n<p>&lt;option value=&#8221;dss&#8221;&gt;Yellow<\/p>\n<p>&lt;\/select&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example1.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the servlet class the value is read from the form by using the method getParameter(). The output is then displayed to the client by the object of the PrintWriter class.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>public class Example1 extends HttpServlet<\/p>\n<p>{<\/p>\n<p>public void doGet(HttpServletRequest req,HttpServletResponse res)<\/p>\n<p>throws ServletException ,IOException<\/p>\n<p>{<\/p>\n<p>res.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">PrintWriter out = res.getWriter();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String str= req.getParameter(&#8220;Read&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String str2= req.getParameter(\u201cColor&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out.println(&#8220;Hai\u00a0 &#8220;+ str + str2);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 <\/strong><\/p>\n<p><strong>Example 2 &#8211; Passing Parameters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example illustrates about sending the parameters through which a user can send information to the Web Server. For example, we write a HTML page in the client side where we send the message entered in a text box to the server, so that the Servlet reads this text value and send it as response to the client. And we will also have one submit button, on pressing the submit button the request will go to the server and Servlet will do the corresponding validation.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ParamPassing.html<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form method=GET action=&#8221;http:\/\/localhost:8080\/servlet-html\/ParamPassing.html&#8221;&gt;<\/p>\n<p>&lt;input type=submit name=&#8221;Submit&#8221; value=&#8221;First&#8221;&gt;<\/p>\n<p>&lt;input type=text name=&#8221;TextBox&#8221; value=&#8221;&#8221;&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0ParamPassing.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The servlet class collects the value from the text box by using the method getParameter(). The output is then displayed to the client by the object of the PrintWriter class.<\/p>\n<p>&nbsp;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class ParamPassing extends HttpServlet<\/p>\n<p>{<\/p>\n<p>public void doGet(HttpServletRequestreq,HttpServletResponse res) throws IOException,ServletException<\/p>\n<p>{<\/p>\n<p>res.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p>PrintWriterpw=res.getWriter();<\/p>\n<p>String str=req.getParameter(&#8220;TextBox&#8221;); \/\/pw.println(&#8220;&lt;html&gt;&lt;body&gt; &#8220;+str+ &#8220;&lt;\/body&gt;&lt;\/html&gt;&#8221;); pw.println(str);<\/p>\n<p>}<\/p>\n<p>public void doPost(HttpServletRequestreq, HttpServletResponse res) throws IOException,ServletException<\/p>\n<p>{<\/p>\n<p>doGet(req,res);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output of this example is shown in Figure 29.16.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-282 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173.png\" alt=\"\" width=\"575\" height=\"246\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173.png 575w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173-300x128.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173-225x96.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-173-350x150.png 350w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0The doPost method<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can use <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/servlet\/http\/HttpServlet.html#doPost%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse%29\">doPost() <\/a>when we want to intercept on <a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec9.html#sec9.5\">HTTP POST requests. <\/a>The doPost() performs invisible data submission whereas doGet() performs visible data submission. The doGet() method is not recommended for everything and doPost() is generally used to carry passwords.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Using &#8220;GET&#8221; the request parameters are passed to the server by appending at the end of the URL, whereas in a &#8220;POST&#8221; request form elements or parameters are passed as a part of HTTP body and does not append at the end of URL. So some sensitive information is sent to the server, a POST request is sent.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>HelloPost.html<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to send a POST request to server, we need to mention the method to be &#8220;POST&#8221; in the form as shown in the HTML code.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt;<\/p>\n<p>My Page<\/p>\n<p>&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;form\u00a0 method=&#8221;Post&#8221;\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">action=&#8221;http:\/\/127.0.0.1:8080\/servlet\/HelloPost&#8221;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;input type=&#8221;text&#8221; name=&#8221;username&#8221; value=&#8221;&#8221;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;input type=submit &gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong> HelloPost.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">By looking at POST request, the servlet engine will call appropriate class as mapped in web.xml and invokes the doget() method, which in turn calls the dopost() method.<\/p>\n<p>&nbsp;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class HelloPost extends HttpServlet<\/p>\n<p>{<\/p>\n<p>public void doPost(HttpServletRequest req,HttpServletResponse res)<\/p>\n<p>throws ServletException, IOException<\/p>\n<p>{<\/p>\n<p>res.setContentType(&#8220;text\/html&#8221;);<\/p>\n<p>PrintWriter out=res.getWriter();<\/p>\n<p>out.println(&#8220;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello<\/p>\n<p>Page&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;Hello&lt;\/body&gt;&lt;\/html&gt;&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Passing Parameters and doPost method<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example illustrates the use of doPost by overriding this method. The doPost method inturn invokes the doGet method which reads the request data, write the response headers, get the response&#8217;s writer or output stream object, and finally, write the response data.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import javax.servlet.*;<\/p>\n<p>import javax.servlet.http.*;<\/p>\n<p>public class Test extends HttpServlet<\/p>\n<p>{<\/p>\n<p>public void doGet(HttpServletRequest req, HttpServletResponse res) throws<\/p>\n<p>ServletException, IOException<\/p>\n<p>{<\/p>\n<p>res.setContentType(\u201ctext\/html\u201d); PrintWriter out = res.getWriter(); String pin = req.getParameter(\u201cto\u201d); String orig = req.getParameter(\u201cfrom\u201d); out.println(\u201cSending page to \u201c + pin + \u201c from \u201c + orig);<\/p>\n<p>\/<span style=\"font-size: 1em\">\/ Actually send the page.<\/span><\/p>\n<p>}<\/p>\n<p>public void doPost(HttpServletRequest in, HttpServletResponse out) throws<\/p>\n<p>ServletException, IOException<\/p>\n<p>{<\/p>\n<p>doGet(in, out);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module we have discussed about the installation of Tomcat to work with Servlets. The module covers the method of acquiring the required software components to use Tomcat for web application development, thereby demonstrating how to implement Servlets. Ultimately, this section has provided simple Servlet programs to have a clear understanding about creating dynamic web applications.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<ul>\n<li><em>https:\/\/www.cis.upenn.edu\/~matuszek\/cit597-2003\/&#8230;\/34-<strong>servlets<\/strong>.<strong>ppt<\/strong><\/em><\/li>\n<li>Herbert Schildt, &#8221; <em>Java: The Complete Reference&#8221;, 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":27,"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-265","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\/265","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\/265\/revisions"}],"predecessor-version":[{"id":595,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/265\/revisions\/595"}],"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\/265\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=265"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=265"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}