{"id":26741,"date":"2021-04-30T23:10:06","date_gmt":"2021-05-01T03:10:06","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/echoserver-java-a-simple-http-server-that-creates-a-web-page-showing-all-data-sent-from-the-client-browser-including-all-http-request-headers-sent-form-the-client-uses-the-following-classes-pro\/"},"modified":"2021-04-30T23:10:06","modified_gmt":"2021-05-01T03:10:06","slug":"echoserver-java-a-simple-http-server-that-creates-a-web-page-showing-all-data-sent-from-the-client-browser-including-all-http-request-headers-sent-form-the-client-uses-the-following-classes-pro","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26741","title":{"rendered":"EchoServer.java  A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming"},"content":{"rendered":"<pre>\nEchoServer.java  A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: \n\nimport java.net.*;\nimport java.io.*;\nimport java.util.StringTokenizer;\n\n\/** A simple HTTP server that generates a Web page showing all\n *  of the data that it received from the Web client (usually\n *  a browser). To use this server, start it on the system of\n *  your choice, supplying a port number if you want something\n *  other than port 8088. Call this system server.com. Next,\n *  start a Web browser on the same or a different system, and\n *  connect to http:\/\/server.com:8088\/whatever. The resultant\n *  Web page will show the data that your browser sent. For\n *  debugging in servlet or CGI programming, specify\n *  http:\/\/server.com:8088\/whatever as the ACTION of your HTML\n *  form. You can send GET or POST data; either way, the\n *  resultant page will show what your browser sent.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\npublic class EchoServer extends NetworkServer {\n  protected int maxRequestLines = 50;\n  protected String serverName = &quot;EchoServer&quot;;\n\n  \/** Supply a port number as a command-line\n   *  argument. Otherwise, use port 8088.\n   *\/\n\n  public static void main(String[] args) {\n    int port = 8088;\n    if (args.length &gt; 0) {\n      try {\n        port = Integer.parseInt(args[0]);\n      } catch(NumberFormatException nfe) {}\n    }\n    new EchoServer(port, 0);\n  }\n\n  public EchoServer(int port, int maxConnections) {\n    super(port, maxConnections);\n    listen();\n  }\n\n  \/** Overrides the NetworkServer handleConnection method to\n   *  read each line of data received, save it into an array\n   *  of strings, then send it back embedded inside a PRE\n   *  element in an HTML page.\n   *\/\n\n  public void handleConnection(Socket server)\n      throws IOException{\n    System.out.println\n        (serverName + &quot;: got connection from &quot; +\n         server.getInetAddress().getHostName());\n    BufferedReader in = SocketUtil.getReader(server);\n    PrintWriter out = SocketUtil.getWriter(server);\n    String[] inputLines = new String[maxRequestLines];\n    int i;\n    for (i=0; i&lt;maxrequestlines ; i++) {\n      inputLines[i] = in.readLine();\n      if (inputLines[i] == null) \/\/ Client closed connection.\n        break;\n      if (inputLines[i].length() == 0) { \/\/ Blank line.\n        if (usingPost(inputLines)) {\n          readPostData(inputLines, i, in);\n          i = i + 2;\n        }\n        break;\n      }\n    }\n    printHeader(out);\n    for (int j=0; j&lt;i; j++) {\n      out.println(inputLines[j]);\n    }\n    printTrailer(out);\n    server.close();\n  }\n\n  \/\/ Send standard HTTP response and top of a standard Web page.\n  \/\/ Use HTTP 1.0 for compatibility with all clients.\n\n  private void printHeader(PrintWriter out) {\n    out.println\n      (&quot;HTTP\/1.0 200 OKrn&quot; +\n       &quot;Server: &quot; + serverName + &quot;rn&quot; +\n       &quot;Content-Type: text\/htmlrn&quot; +\n       &quot;rn&quot; +\n       &quot;n&quot; +\n       &quot;n&quot; +\n       &quot;n&quot; +\n       &quot;  <title>&quot; + serverName + &quot; Results<\/title>n&quot; +\n       &quot;n&quot; +\n       &quot;n&quot; +\n       &quot;n&quot; +\n       &quot;<h1 ALIGN=\"&quot;CENTER&quot;\">&quot; + serverName +\n         &quot; Results<\/h1>n&quot; +\n       &quot;Here is the request line and request headersn&quot; +\n       &quot;sent by your browser:n&quot; +\n       &quot;<\/pre>\n<pre>&quot;);\n  }\n\n  \/\/ Print bottom of a standard Web page.\n\n  private void printTrailer(PrintWriter out) {\n    out.println\n      (&quot;<\/pre>\n<p>n&quot; +<br \/>\n       &quot;n&quot; +<br \/>\n       &quot;n&quot;);<br \/>\n  }<\/p>\n<p>  \/\/ Normal Web page requests use GET, so this server can simply<br \/>\n  \/\/ read a line at a time. However, HTML forms can also use<br \/>\n  \/\/ POST, in which case we have to determine the number of POST<br \/>\n  \/\/ bytes that are sent so we know how much extra data to read<br \/>\n  \/\/ after the standard HTTP headers.<\/p>\n<p>  private boolean usingPost(String[] inputs) {<br \/>\n    return(inputs[0].toUpperCase().startsWith(&quot;POST&quot;));<br \/>\n  }<\/p>\n<p>  private void readPostData(String[] inputs, int i,<br \/>\n                            BufferedReader in)<br \/>\n      throws IOException {<br \/>\n    int contentLength = contentLength(inputs);<br \/>\n    char[] postData = new char[contentLength];<br \/>\n    in.read(postData, 0, contentLength);<br \/>\n    inputs[++i] = new String(postData, 0, contentLength);<br \/>\n  }<\/p>\n<p>  \/\/ Given a line that starts with Content-Length,<br \/>\n  \/\/ this returns the integer value specified.<\/p>\n<p>  private int contentLength(String[] inputs) {<br \/>\n    String input;<br \/>\n    for (int i=0; i&lt;inputs .length; i++) {<br \/>\n      if (inputs[i].length() == 0)<br \/>\n        break;<br \/>\n      input = inputs[i].toUpperCase();<br \/>\n      if (input.startsWith(&quot;CONTENT-LENGTH&quot;))<br \/>\n        return(getLength(input));<br \/>\n    }<br \/>\n    return(0);<br \/>\n  }<\/p>\n<p>  private int getLength(String length) {<br \/>\n    StringTokenizer tok = new StringTokenizer(length);<br \/>\n    tok.nextToken();<br \/>\n    return(Integer.parseInt(tok.nextToken()));<br \/>\n  }<br \/>\n}<\/p>\n<p>NetworkServer.java  A starting point for network servers. <\/p>\n<p>import java.net.*;<br \/>\nimport java.io.*;<\/p>\n<p>\/** A starting point for network servers. You&#039;ll need to<br \/>\n *  override handleConnection, but in many cases listen can<br \/>\n *  remain unchanged. NetworkServer uses SocketUtil to simplify<br \/>\n *  the creation of the PrintWriter and BufferedReader.<br \/>\n *<br \/>\n *  Taken from Core Web Programming from<br \/>\n *  Prentice Hall and Sun Microsystems Press,<br \/>\n *  .<br \/>\n *  &copy; 2001 Marty Hall and Larry Brown;<br \/>\n *  may be freely used or adapted.<br \/>\n *\/<\/p>\n<p>public class NetworkServer {<br \/>\n  private int port, maxConnections;<\/p>\n<p>  \/** Build a server on specified port. It will continue to<br \/>\n   *  accept connections, passing each to handleConnection until<br \/>\n   *  an explicit exit command is sent (e.g., System.exit) or<br \/>\n   *  the maximum number of connections is reached. Specify<br \/>\n   *  0 for maxConnections if you want the server to run<br \/>\n   *  indefinitely.<br \/>\n   *\/<\/p>\n<p>  public NetworkServer(int port, int maxConnections) {<br \/>\n    setPort(port);<br \/>\n    setMaxConnections(maxConnections);<br \/>\n  }<\/p>\n<p>  \/** Monitor a port for connections. Each time one is<br \/>\n   *  established, pass resulting Socket to handleConnection.<br \/>\n   *\/<\/p>\n<p>  public void listen() {<br \/>\n    int i=0;<br \/>\n    try {<br \/>\n      ServerSocket listener = new ServerSocket(port);<br \/>\n      Socket server;<br \/>\n      while((i++ &lt; maxConnections) || (maxConnections == 0)) {<br \/>\n        server = listener.accept();<br \/>\n        handleConnection(server);<br \/>\n      }<br \/>\n    } catch (IOException ioe) {<br \/>\n      System.out.println(&quot;IOException: &quot; + ioe);<br \/>\n      ioe.printStackTrace();<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/** This is the method that provides the behavior to the<br \/>\n   *  server, since it determines what is done with the<br \/>\n   *  resulting socket. <b>Override this method in servers<br \/>\n   *  you write.<br \/>\n   *  <\/p>\n<p>\n   *  This generic version simply reports the host that made<br \/>\n   *  the connection, shows the first line the client sent,<br \/>\n   *  and sends a single line in response.<br \/>\n   *\/<\/p>\n<p>  protected void handleConnection(Socket server)<br \/>\n      throws IOException{<br \/>\n    BufferedReader in = SocketUtil.getReader(server);<br \/>\n    PrintWriter out = SocketUtil.getWriter(server);<br \/>\n    System.out.println<br \/>\n      (&quot;Generic Network Server: got connection from &quot; +<br \/>\n       server.getInetAddress().getHostName() + &quot;n&quot; +<br \/>\n       &quot;with first line &#8216;&quot; + in.readLine() + &quot;&#8217;&quot;);<br \/>\n    out.println(&quot;Generic Network Server&quot;);<br \/>\n    server.close();<br \/>\n  }<\/p>\n<p>  \/** Gets the max connections server will handle before<br \/>\n   *  exiting. A value of 0 indicates that server should run<br \/>\n   *  until explicitly killed.<br \/>\n   *\/<\/p>\n<p>  public int getMaxConnections() {<br \/>\n    return(maxConnections);<br \/>\n  }<\/p>\n<p>  \/** Sets max connections. A value of 0 indicates that server<br \/>\n   *  should run indefinitely (until explicitly killed).<br \/>\n   *\/<\/p>\n<p>  public void setMaxConnections(int maxConnections) {<br \/>\n    this.maxConnections = maxConnections;<br \/>\n  }<\/p>\n<p>  \/** Gets port on which server is listening. *\/<\/p>\n<p>  public int getPort() {<br \/>\n    return(port);<br \/>\n  }<\/p>\n<p>  \/** Sets port. <b>You can only do before &quot;connect&quot; is<br \/>\n   *  called.<\/b> That usually happens in the constructor.<br \/>\n   *\/<\/p>\n<p>  protected void setPort(int port) {<br \/>\n    this.port = port;<br \/>\n  }<br \/>\n}<\/p>\n<p>SocketUtil.java  Simplifies the creation of a PrintWriter and BufferedReader.<\/p>\n<p>import java.net.*;<br \/>\nimport java.io.*;<\/p>\n<p>\/** A shorthand way to create BufferedReaders and<br \/>\n *  PrintWriters associated with a Socket.<br \/>\n *<br \/>\n *  Taken from Core Web Programming from<br \/>\n *  Prentice Hall and Sun Microsystems Press,<br \/>\n *  &copy; 2001 Marty Hall and Larry Brown;<br \/>\n *  may be freely used or adapted.<br \/>\n *\/<\/p>\n<p>public class SocketUtil {<br \/>\n  \/** Make a BufferedReader to get incoming data. *\/<\/p>\n<p>  public static BufferedReader getReader(Socket s)<br \/>\n      throws IOException {<br \/>\n    return(new BufferedReader(<br \/>\n       new InputStreamReader(s.getInputStream())));<br \/>\n  }<\/p>\n<p>  \/** Make a PrintWriter to send outgoing data.<br \/>\n   *  This PrintWriter will automatically flush stream<br \/>\n   *  when println is called.<br \/>\n   *\/<\/p>\n<p>  public static PrintWriter getWriter(Socket s)<br \/>\n      throws IOException {<br \/>\n    \/\/ Second argument of true means autoflush.<br \/>\n    return(new PrintWriter(s.getOutputStream(), true));<br \/>\n  }<br \/>\n}<\/p>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10236<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Network Programming<br \/>Tags:Java\/J2EE\/J2MENetwork Programming<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<p><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>EchoServer.java A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: import java.net.*; import java.io.*; import java.util.StringTokenizer; \/** A simple HTTP server that generates a Web page showing all * of the data that it &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26741\">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-26741","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":10202,"url":"http:\/\/bangla.sitestree.com\/?p=10202","url_meta":{"origin":26741,"position":0},"title":"EchoServer.java A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes","author":"","date":"August 25, 2015","format":false,"excerpt":"EchoServer.java\u00a0 A simple HTTP server that creates a Web page showing all data sent from the client (browser), including all HTTP request headers sent form the client. Uses the following classes: \u00a0 import java.net.*; import java.io.*; import java.util.StringTokenizer; \/** A simple HTTP server that generates a Web page showing all\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":10204,"url":"http:\/\/bangla.sitestree.com\/?p=10204","url_meta":{"origin":26741,"position":1},"title":"ThreadedEchoServer.java A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes","author":"","date":"August 25, 2015","format":false,"excerpt":"import java.net.*; import java.io.*; \/** A multithreaded variation of EchoServer. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0*\/ public class ThreadedEchoServer extends EchoServer \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements\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":26743,"url":"http:\/\/bangla.sitestree.com\/?p=26743","url_meta":{"origin":26741,"position":2},"title":"ThreadedEchoServer.java  A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 30, 2021","format":false,"excerpt":"ThreadedEchoServer.java A multithreaded version of EchoServer, where each client request is serviced on a separate thread. Requires the following classes: import java.net.*; import java.io.*; \/** A multithreaded variation of EchoServer. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001\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":26531,"url":"http:\/\/bangla.sitestree.com\/?p=26531","url_meta":{"origin":26741,"position":3},"title":"ShowRequestHeaders.java  Servlet that shows all request headers sent by browser in current request. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"ShowRequestHeaders.java Servlet that shows all request headers sent by browser in current request. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; \/** Shows all the request headers sent on this request. * * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems\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":10166,"url":"http:\/\/bangla.sitestree.com\/?p=10166","url_meta":{"origin":26741,"position":4},"title":"ShowRequestHeaders.java Servlet that shows all request headers sent by browser in current request.","author":"","date":"August 17, 2015","format":false,"excerpt":"ShowRequestHeaders.java Servlet that shows all request headers sent by browser in current request. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; \/** Shows all the request headers sent on this request. * * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems\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":26589,"url":"http:\/\/bangla.sitestree.com\/?p=26589","url_meta":{"origin":26741,"position":5},"title":"AddressVerifier.java  Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"AddressVerifier.java Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes: import java.net.*; import java.io.*; \/** Given an e-mail address of the form user@host, * connect to port 25 of the host and issue an * 'expn'\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":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26741","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=26741"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26741\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26741"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}