{"id":10160,"date":"2015-08-16T00:03:01","date_gmt":"2015-08-16T04:03:01","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10160"},"modified":"2015-08-01T09:26:14","modified_gmt":"2015-08-01T13:26:14","slug":"showmessage-java-servlet-that-demonstrates-the-use-of-initialization-parameters","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10160","title":{"rendered":"ShowMessage.java Servlet that demonstrates the use of initialization parameters."},"content":{"rendered":"<pre>ShowMessage.java\u00a0 Servlet that demonstrates the use of initialization parameters. Remember that, to use this servlet, you have to do three things:\r\n\r\n\u00a0\u00a0\u00a0 * Put the modified web.xml file in the WEB-INF directory.\r\n\u00a0\u00a0\u00a0 * Restart the server.\r\n\u00a0\u00a0\u00a0 * Use the registered servlet name (i.e., the URL http:\/\/host\/servlet\/ShowMsg), not the raw servlet name (i.e., the URL http:\/\/host\/servlet\/cwp.ShowMessage). \r\n\r\n\r\npackage cwp;\r\n\r\nimport java.io.*;\r\nimport javax.servlet.*;\r\nimport javax.servlet.http.*;\r\n\r\n\/** Example using servlet initialization. Here, the message\r\n\u00a0*\u00a0 to print and the number of times the message should be\r\n\u00a0*\u00a0 repeated is taken from the init parameters.\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition\r\n\u00a0*\u00a0 from Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 May be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class ShowMessage extends HttpServlet {\r\n\u00a0 private String message;\r\n\u00a0 private String defaultMessage = \"No message.\";\r\n\u00a0 private int repeats = 1;\r\n\r\n\u00a0 public void init() throws ServletException {\r\n\u00a0\u00a0\u00a0 ServletConfig config = getServletConfig();\r\n\u00a0\u00a0\u00a0 message = config.getInitParameter(\"message\");\r\n\u00a0\u00a0\u00a0 if (message == null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 message = defaultMessage;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String repeatString = config.getInitParameter(\"repeats\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 repeats = Integer.parseInt(repeatString);\r\n\u00a0\u00a0\u00a0 } catch(NumberFormatException nfe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ NumberFormatException handles case where repeatString\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ is null *and* case where it is in an illegal format.\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 public void doGet(HttpServletRequest request,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HttpServletResponse response)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 throws ServletException, IOException {\r\n\u00a0\u00a0\u00a0 response.setContentType(\"text\/html\");\r\n\u00a0\u00a0\u00a0 PrintWriter out = response.getWriter();\r\n\u00a0\u00a0\u00a0 String title = \"The ShowMessage Servlet\";\r\n\u00a0\u00a0\u00a0 out.println(ServletUtilities.headWithTitle(title) +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\r\n\" + title + \"\r\n\");\r\n\u00a0\u00a0\u00a0 for(int i=0; i\" + message + \"\r\n\");\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 out.println(\"\");\r\n\u00a0 }\r\n}\r\n\r\n\r\nweb.xml\r\n\r\n\r\n\r\n?\r\n\r\nShowMsg\r\ncwp.ShowMessage\r\n?\r\n\r\nmessage\r\nShibboleth\r\n\r\n?\r\n\r\nrepeats\r\n5\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ShowMessage.java\u00a0 Servlet that demonstrates the use of initialization parameters. Remember that, to use this servlet, you have to do three things: \u00a0\u00a0\u00a0 * Put the modified web.xml file in the WEB-INF directory. \u00a0\u00a0\u00a0 * Restart the server. \u00a0\u00a0\u00a0 * Use the registered servlet name (i.e., the URL http:\/\/host\/servlet\/ShowMsg), not the raw servlet name (i.e., the &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10160\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,1457,285],"class_list":["post-10160","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-servlet","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26525,"url":"http:\/\/bangla.sitestree.com\/?p=26525","url_meta":{"origin":10160,"position":0},"title":"ShowMessage.java  Servlet that demonstrates the use of initialization parameters. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"ShowMessage.java Servlet that demonstrates the use of initialization parameters. Remember that, to use this servlet, you have to do three things: * Put the modified web.xml file in the WEB-INF directory. * Restart the server. * Use the registered servlet name (i.e., the URL http:\/\/host\/servlet\/ShowMsg), not the raw servlet name\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10162,"url":"http:\/\/bangla.sitestree.com\/?p=10162","url_meta":{"origin":10160,"position":1},"title":"ThreeParams.java Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities class.","author":"","date":"August 16, 2015","format":false,"excerpt":"ThreeParams.java\u00a0 Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities\u00a0 class. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that reads three parameters from the \u00a0*\u00a0 form data. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0 from Prentice Hall and\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26527,"url":"http:\/\/bangla.sitestree.com\/?p=26527","url_meta":{"origin":10160,"position":2},"title":"ThreeParams.java  Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities  class. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"ThreeParams.java Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities class. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that reads three parameters from the * form data. * * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26517,"url":"http:\/\/bangla.sitestree.com\/?p=26517","url_meta":{"origin":10160,"position":3},"title":"HelloWorld.java  Simple servlet that generates plain text. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"HelloWorld.java Simple servlet that generates plain text. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Very simplistic servlet that generates plain text. * * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. *\/ public\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10152,"url":"http:\/\/bangla.sitestree.com\/?p=10152","url_meta":{"origin":10160,"position":4},"title":"HelloWorld.java Simple servlet that generates plain text.","author":"","date":"August 14, 2015","format":false,"excerpt":"HelloWorld.java\u00a0 Simple servlet that generates plain text. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Very simplistic servlet that generates plain text. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0 from Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 May be freely used or adapted. \u00a0*\/\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10154,"url":"http:\/\/bangla.sitestree.com\/?p=10154","url_meta":{"origin":10160,"position":5},"title":"HelloWWW.java Servlet that generates HTML. This and all remaining servlets are in the cwp package and therefore should be installed in the cwp subdirectory.","author":"","date":"August 14, 2015","format":false,"excerpt":"HelloWWW.java\u00a0 Servlet that generates HTML. This and all remaining servlets are in the cwp package and therefore should be installed in the cwp subdirectory. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that generates HTML. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10160"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10160\/revisions"}],"predecessor-version":[{"id":10161,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10160\/revisions\/10161"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10160"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}