A command-line interface to a Web server : JSP Debug « JSP « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » JSP » JSP Debug 




A command-line interface to a Web server


/**
*  Copyright (c) 2002 by Phil Hanna
*  All rights reserved.
*  
*  You may study, use, modify, and distribute this
*  software for any purpose provided that this
*  copyright notice appears in all copies.
*  
*  This software is provided without warranty
*  either expressed or implied.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Mainline for running the Web client
*/
public class MainWebClient
{
   /**
   * Reads command line parameters, creates a new
   * <code>WebClient</code> object, then invokes it.
   */
   public static void main(String[] args)
      throws IOException
   {
      // Default values

      String host = "localhost";
      int port = 80;

      // Use values from command line, if specified

      for (int i = 0; i < args.length; i++) {
         String arg = args[i];
         if (arg.equals("-h"|| arg.equals("-help")) {
            showUsage();
            return;
         }
         else
         if (arg.equals("-host"&& ++i < args.length)
            host = args[i];
         else
         if (arg.equals("-port"&& ++i < args.length)
            port = Integer.parseInt(args[i]);
      }

      // Create and start the Web client

      new WebClient(host, port).run();
   }

   /**
   * Displays the calling syntax
   */
   private static void showUsage()
   {
      String[] text = {
         "usage: java com.jspcr.debug.webclient.Main"
         " [-host <hostName>]"
         " [-port <portNumber>]",
      };
      for (int i = 0; i < text.length; i++)
         System.out.println(text[i]);
   }
}


/**
*  Copyright (c) 2002 by Phil Hanna
*  All rights reserved.
*  
*  You may study, use, modify, and distribute this
*  software for any purpose provided that this
*  copyright notice appears in all copies.
*  
*  This software is provided without warranty
*  either expressed or implied.
*/

/**
* A command-line interface to a Web server
*/
class WebClient
{
   // Instance variables

   private String host;
   private int port;

   /**
   * Creates a new Web client
   @param host the HTTP server
   @param port the server port number
   */
   public WebClient(String host, int port)
   {
      this.host = host;
      this.port = port;
   }

   /**
   * Runs the Web client
   @throws IOException if a socket error occurs
   */
   public void run() throws IOException
   {
      int contentLength = 0;

      // Open a socket to the Web host

      Socket socket = new Socket(host, port);

      // Set up to read input from the user
      // and echo it to Web host

      BufferedReader in =
         new BufferedReader(new InputStreamReader(System.in));

      PrintWriter out =
         new PrintWriter(socket.getOutputStream()true);

      // The first line is the HTTP request
      // e.g., "GET /path HTTP/1.0"

      String line = in.readLine();
      out.println(line);

      // Read and echo any other headers, stopping
      // when a blank line is encountered.

      while ((line = in.readLine()) != null) {

         line = line.trim();
         out.println(line);
         if (line.equals(""))
            break;

         // Check for a Content-Length header

         Pattern p = Pattern.compile
            ("^\\s*([^:]+):\\s+(.*\\S)\\s*$");
         Matcher m = p.matcher(line);
         if (m.matches()) {
            String name = m.group(1);
            String value = m.group(2);
            if (name.equalsIgnoreCase("Content-Length"))
               contentLength = Integer.parseInt(value);
         }
      }

      // If a non-zero content length header was used,
      // read and echo that many bytes to the Web host.

      if (contentLength > 0) {
         for (int i = 0; i < contentLength; i++)
            out.print((charin.read());
         out.flush();
      }

      // The server is now working on the request.
      // Read its output and dump to stdout

      in = new BufferedReader(
           new InputStreamReader(socket.getInputStream()));

      out = new PrintWriter(System.out);

      for (;;) {
         line = in.readLine();
         if (line == null)
            break;
         out.println(line);
      }
      
      // Close files

      in.close();
      out.close();
      socket.close();
   }
}

           
       














Related examples in the same category
1.HTTP Echo For testing
2.Mainline for the HTTP tracer tool
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.