{"id":26535,"date":"2021-04-27T23:10:05","date_gmt":"2021-04-28T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/searchengines-java-servlet-that-redirects-requests-to-various-search-engines-uses-the-searchspec-helper-class-accessed-by-means-of-searchengines-html-programming-code-examples-java-j2ee-j2me-s\/"},"modified":"2021-04-27T23:10:05","modified_gmt":"2021-04-28T03:10:05","slug":"searchengines-java-servlet-that-redirects-requests-to-various-search-engines-uses-the-searchspec-helper-class-accessed-by-means-of-searchengines-html-programming-code-examples-java-j2ee-j2me-s","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26535","title":{"rendered":"SearchEngines.java  Servlet that redirects requests to various search engines. Uses the SearchSpec  helper class. Accessed by means of SearchEngines.html. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet"},"content":{"rendered":"<pre>\nSearchEngines.java  Servlet that redirects requests to various search engines. Uses the SearchSpec  helper class. Accessed by means of SearchEngines.html. \n\npackage cwp;\n\nimport java.io.*;\nimport javax.servlet.*;\nimport javax.servlet.http.*;\nimport java.net.*;\n\n\/** Servlet that takes a search string, number of results per\n *  page, and a search engine name, sending the query to\n *  that search engine. Illustrates manipulating\n *  the response status line. It sends a 302 response\n *  (via sendRedirect) if it gets a known search engine,\n *  and sends a 404 response (via sendError) otherwise.\n *  <p>\n *  Taken from Core Web Programming Java 2 Edition\n *  from Prentice Hall and Sun Microsystems Press,\n *  .\n *  May be freely used or adapted.\n *\/\n\npublic class SearchEngines extends HttpServlet {\n  public void doGet(HttpServletRequest request,\n                    HttpServletResponse response)\n      throws ServletException, IOException {\n    String searchString = request.getParameter(&quot;searchString&quot;);\n    if ((searchString == null) ||\n        (searchString.length() == 0)) {\n      reportProblem(response, &quot;Missing search string.&quot;);\n      return;\n    }\n    \/\/ The URLEncoder changes spaces to &quot;+&quot; signs and other\n    \/\/ non-alphanumeric characters to &quot;%XY&quot;, where XY is the\n    \/\/ hex value of the ASCII (or ISO Latin-1) character.\n    \/\/ Browsers always URL-encode form values, so the\n    \/\/ getParameter method decodes automatically. But since\n    \/\/ we're just passing this on to another server, we need to\n    \/\/ re-encode it.\n    searchString = URLEncoder.encode(searchString);\n    String numResults = request.getParameter(&quot;numResults&quot;);\n    if ((numResults == null) ||\n        (numResults.equals(&quot;0&quot;)) ||\n        (numResults.length() == 0)) {\n      numResults = &quot;10&quot;;\n    }\n    String searchEngine =\n      request.getParameter(&quot;searchEngine&quot;);\n    if (searchEngine == null) {\n      reportProblem(response, &quot;Missing search engine name.&quot;);\n      return;\n    }\n    SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();\n    for(int i=0; i&lt;commonspecs .length; i++) {\n      SearchSpec searchSpec = commonSpecs[i];\n      if (searchSpec.getName().equals(searchEngine)) {\n        String url =\n          searchSpec.makeURL(searchString, numResults);\n        response.sendRedirect(url);\n        return;\n      }\n    }\n    reportProblem(response, &quot;Unrecognized search engine.&quot;);\n  }\n\n  private void reportProblem(HttpServletResponse response,\n                             String message)\n      throws IOException {\n    response.sendError(response.SC_NOT_FOUND,\n                       &quot;<h2>&quot; + message + &quot;&quot;);\n  }\n\n  public void doPost(HttpServletRequest request,\n                     HttpServletResponse response)\n      throws ServletException, IOException {\n    doGet(request, response);\n  }\n}\n\n\n\nSearchSpec.java\n\npackage cwp;\n\n\/** Small class that encapsulates how to construct a\n *  search string for a particular search engine.\n *  <p>\n *  Taken from Core Web Programming Java 2 Edition\n *  from Prentice Hall and Sun Microsystems Press,\n *  May be freely used or adapted.\n *\/\n\npublic class SearchSpec {\n  private String name, baseURL, numResultsSuffix;\n\n  private static SearchSpec[] commonSpecs =\n    { new SearchSpec(&quot;google&quot;,\n                     &quot;http:\/\/www.google.com\/search?q=&quot;,\n                     &quot;&amp;num=&quot;),\n      new SearchSpec(&quot;infoseek&quot;,\n                     &quot;http:\/\/infoseek.go.com\/Titles?qt=&quot;,\n                     &quot;&amp;nh=&quot;),\n      new SearchSpec(&quot;lycos&quot;,\n                     &quot;http:\/\/lycospro.lycos.com\/cgi-bin\/&quot; +\n                        &quot;pursuit?query=&quot;,\n                     &quot;&amp;maxhits=&quot;),\n      new SearchSpec(&quot;hotbot&quot;,\n                     &quot;http:\/\/www.hotbot.com\/?MT=&quot;,\n                     &quot;&amp;DC=&quot;)\n    };\n\n  public SearchSpec(String name,\n                    String baseURL,\n                    String numResultsSuffix) {\n    this.name = name;\n    this.baseURL = baseURL;\n    this.numResultsSuffix = numResultsSuffix;\n  }\n\n  public String makeURL(String searchString,\n                        String numResults) {\n    return(baseURL + searchString +\n           numResultsSuffix + numResults);\n  }\n\n  public String getName() {\n    return(name);\n  }\n\n  public static SearchSpec[] getCommonSpecs() {\n    return(commonSpecs);\n  }\n}\n\n\n<\/p><\/h2><\/p><\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10250<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Servlet<br \/>Tags:Java\/J2EE\/J2MEServlet<br \/> Post Data:2017-01-02 16:04:28<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SearchEngines.java Servlet that redirects requests to various search engines. Uses the SearchSpec helper class. Accessed by means of SearchEngines.html. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; \/** Servlet that takes a search string, number of results per * page, and a search engine name, sending the query to * that search engine. &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26535\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26535","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10170,"url":"http:\/\/bangla.sitestree.com\/?p=10170","url_meta":{"origin":26535,"position":0},"title":"SearchEngines.java Servlet that redirects requests to various search engines. Uses the SearchSpec helper class. Accessed by means of SearchEngines.html.","author":"","date":"August 19, 2015","format":false,"excerpt":"SearchEngines.java\u00a0 Servlet that redirects requests to various search engines. Uses the SearchSpec\u00a0 helper class. Accessed by means of SearchEngines.html. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; \/** Servlet that takes a search string, number of results per \u00a0*\u00a0 page, and a search engine name, sending the query\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":10181,"url":"http:\/\/bangla.sitestree.com\/?p=10181","url_meta":{"origin":26535,"position":1},"title":"ShowSession.java Servlet that uses session tracking to determine if the client is a repeat visitor. Uses the ServletUtilities class to simplify the DOCTYPE and HEAD output.","author":"","date":"August 21, 2015","format":false,"excerpt":"ShowSession.java Servlet that uses session tracking to determine if the client is a repeat visitor. Uses the ServletUtilities class to simplify the DOCTYPE and HEAD output. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; import java.util.*; \/** Simple example of session tracking. * * Taken from Core Web\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":10162,"url":"http:\/\/bangla.sitestree.com\/?p=10162","url_meta":{"origin":26535,"position":2},"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":26555,"url":"http:\/\/bangla.sitestree.com\/?p=26555","url_meta":{"origin":26535,"position":3},"title":"ShowSession.java  Servlet that uses session tracking to determine if the client is a repeat visitor. Uses the ServletUtilities  class to simplify the DOCTYPE and HEAD output. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"ShowSession.java Servlet that uses session tracking to determine if the client is a repeat visitor. Uses the ServletUtilities class to simplify the DOCTYPE and HEAD output. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; import java.util.*; \/** Simple example of session tracking. * * Taken from Core Web\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":26527,"url":"http:\/\/bangla.sitestree.com\/?p=26527","url_meta":{"origin":26535,"position":4},"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":10158,"url":"http:\/\/bangla.sitestree.com\/?p=10158","url_meta":{"origin":26535,"position":5},"title":"SimplerHelloWWW.java Servlet that uses ServletUtilities to simplify the generation of the DOCTYPE and HEAD part of the servlet.","author":"","date":"August 15, 2015","format":false,"excerpt":"SimplerHelloWWW.java\u00a0 Servlet that uses ServletUtilities\u00a0 to simplify the generation of the DOCTYPE and HEAD part of the servlet. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that generates HTML. This variation of \u00a0*\u00a0 HelloWWW uses the ServletUtilities utility class \u00a0*\u00a0 to generate the DOCTYPE, HEAD, and TITLE.\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\/26535","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26535"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26535\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26535"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}