HTTP Echo For testing : JSP Debug « JSP « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » JSP » JSP DebugScreenshots 
HTTP Echo For testing

import java.net.Socket;
import java.net.ServerSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;

public class HTTPEcho {
   private static final int BUF_SIZE = 1024;
   private Socket browserSocket = null;
   private Socket webServerSocket = null;
   private ServerSocket browserListenerSocket = null;
   private OutputStream toWebServer = null;
   private BufferedInputStream fromWebServer = null;
   private OutputStream toBrowser = null;
   private BufferedInputStream fromBrowser = null

   private ServerSocket createListenerSocket(int portNum)  {
      ServerSocket socket = null;
      try  {
         socket = new ServerSocket(portNum);
      }
      catch (IOException ioe)  {
         System.out.println(ioe.getMessage());
         System.exit(-1);
      }
      return socket;
   }

   private Socket createClientSocket(String host, int portNum)  {
      Socket socket = null;
      try  {
         socket = new Socket(host, portNum);
         toWebServer = socket.getOutputStream();
         fromWebServer = 
            new BufferedInputStream(socket.getInputStream());
      }
      catch (IOException ioe)  {
         System.out.println(ioe.getMessage());
         System.exit(-1);
      }
      return socket;
   }

   private Socket accept ()  {
      Socket socket = null;
      try {
         socket = browserListenerSocket.accept();
         toBrowser = socket.getOutputStream();
         fromBrowser = 
            new BufferedInputStream(socket.getInputStream());
      }
      catch (IOException e) {
         System.out.println("Accept failed: " +  e.getMessage());
         System.exit(-1);
      }
      return socket;
   }

   private void setBrowserListenerSocket(ServerSocket socket)  {
      browserListenerSocket = socket;
   }

   private void setWebServerSocket(Socket socket)  {
      webServerSocket = socket;
   }

   private void setBrowserSocket(Socket socket)  {
      browserSocket = socket;
   }

   private int readLine(InputStream in, byte[] b, int off, int lenthrows IOException {
      if (len <= 0) {
         return 0;
      }
      int count = 0, c;
      while ((c = in.read()) != -&& count < len) {
         b[off++(byte)c;
         count++;
         if (c == '\n') {
            break;
         }
      }
      return count > ? count : -1;
   }

   private void echoBuffer(BufferedInputStream in, OutputStream out)  {
      byte[] readBuf = new byte[BUF_SIZE];
      try  {
         int rlen = -1;
         int bodyLen = 0;
         String line = null;

         do  {
            line = null;
            rlen = readLine(in, readBuf, 0, BUF_SIZE);
            if (rlen > 0)  {
               line = new String(readBuf, 0, rlen)
               System.out.print(line);
               if (line.startsWith("Content-Length: ")) {
                  String size = 
                     line.substring("Content-Length: ".length()
                                    line.length());
                  bodyLen = Integer.parseInt(size.trim())
               }
               out.write(readBuf, 0, rlen);
            }
         while ((rlen > 0&& !(line.equals("\r\n")));

         if (bodyLen > 0) {
            System.out.println("<Entity-Body>");
            int count = 0;
            do  {
               rlen = in.read(readBuf, 0, BUF_SIZE);
               if (rlen > 0)  {
                  out.write(readBuf, 0, rlen);
                  count += rlen;
               }else break;
            while (count < bodyLen);
         }
         out.flush();
      }
      catch (IOException ioe)  {
         System.out.println(ioe.getMessage());
      }
      finally  {
         readBuf = null;
      }
   }

   private void echo()  {
      System.out.println("\nHTTP REQUEST:");
      echoBuffer(fromBrowser, toWebServer);

      System.out.println("\nHTTP REPLY:");
      echoBuffer(fromWebServer, toBrowser);
   }

   private void closeClientDescriptors()  {
      try  {
         fromWebServer.close();
         toWebServer.close();
         webServerSocket.close();
         fromBrowser.close();
         toBrowser.close();
         browserSocket.close();
      }
      catch (IOException ioe)  {
         System.out.println(ioe.getMessage());
      }
   }

   public static void main(String [] args) {
      HTTPEcho echo = new HTTPEcho();
      if (args.length > 1)  {
         System.out.println("Starting HTTPEcho on port: " + args[0". ");
         System.out.println("Web server host: " + args[1".");
         System.out.println("Web server is on port: " + args[2".");

      int interceptorPort = Integer.parseInt(args[0]);
      String webServerHost = args[1];
      int webServerPort = Integer.parseInt(args[2]);

      echo.setBrowserListenerSocket(echo.createListenerSocket(interceptorPort));
         while (true)  {
            echo.setBrowserSocket(echo.accept());
            echo.setWebServerSocket(echo.createClientSocket(webServerHost, 
                                    webServerPort));
            echo.echo();
            echo.closeClientDescriptors();
         }
      }
      else {
         System.out.println("Usage: java HTTPEcho " 
                            "interceptorPort " +
                            "webServerHost webServerPort");
      }
   }
}


           
       
Related examples in the same category
1. Mainline for the HTTP tracer tool
2. A command-line interface to a Web server
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.