Example: Using data returned from advisors

Whether you use a standard call to an existing part of the application server or add a new piece of code to be the server-side counterpart of your custom advisor, you possibly want to examine the load values returned and change server behavior.

The Java StringTokenizer class, and its associated methods, make this investigation easy to do. The content of a typical HTTP command might be
GET /index.html HTTP/1.0 90
A typical response to this command might be the following:
HTTP/1.1 200 OK
Date: Mon, 20 November 2000 14:09:57 GMT
Server: Apache/1.3.12 (Linux and UNIX)
Content-Location: index.html.en
Vary: negotiate
TCN: choice
Last-Modified: Fri, 20 Oct 2000 15:58:35 GMT
ETag: "14f3e5-1a8-39f06bab;39f06a02"
Accept-Ranges: bytes
Content-Length: 424
Connection: close
Content-Type: text/html
Content-Language: en

<!DOCTYPE HTML PUBLIC "-//w3c//DTD HTML 3.2 Final//EN">
<HTML><HEAD><TITLE>Test Page</TITLE></HEAD>
<BODY><H1>Apache server</H1>
<HR>
<P><P>This Web server is running Apache 1.3.12.
</P>
<P><IMG SRC="apache_pb.gif" ALT="">
</P></P>
</HR>
</BODY></HTML>
The items of interest are contained in the first line, specifically the HTTP return code. The HTTP specification classifies return codes that can be summarized as follows:
  • 2xx return codes are successes
  • 3xx return codes are redirections
  • 4xx return codes are client errors
  • 5xx return codes are server errors

If you know precisely what codes the server can possibly return, your code might not need to be as detailed as this example. However, keep in mind that limiting the return codes you detect might limit the future flexibility of your program.

The following example is a stand-alone Java program that contains a minimal HTTP client. The example invokes a simple, general-purpose parser for examining HTTP responses.
import java.io.*;
import java.util.*;
import java.net.*;

public class ParseTest {
   static final int iPort = 80;
   static final String sServer = "www.ibm.com";
   static final String sQuery = "GET /index.html HTTP/1.0\r\n\r\n";
   static final String sHTTP10 = "HTTP/1.0";
   static final String sHTTP11 = "HTTP/1.1";

   public static void main(String[] Arg) {
     String sHTTPVersion = null;
     String sHTTPReturnCode = null;
     String sResponse = null; int iRc = 0;
     BufferedReader brIn = null;
     PrintWriter psOut = null;
     Socket soServer= null;
     StringBuffer sbText = new
     StringBuffer(40);

   try {
     soServer = new Socket(sServer, iPort);
     brIn = new BufferedReader(new InputStreamReader(
                                   soServer.getInputStream()));
     psOut = new PrintWriter(soServer.getOutputStream());
     psOut.println(sQuery);
     psOut.flush();
     sResponse = brIn.readLine();
     try {
       soServer.close();
     } catch (Exception sc) {;}
   } catch (Exception swr) {;}

   StringTokenizer st = new StringTokenizer(sResponse, " ");
   if (true == st.hasMoreTokens()) {
     sHTTPVersion = st.nextToken();
     if (sHTTPVersion.equals(sHTTP110) || sHTTPVersion.equals(sHTTP11)) {
       System.out.println("HTTP Version: " + sHTTPVersion);
     } else { 
       System.out.println("Invalid HTTP Version: " + sHTTPVersion); 
     } 
   } else {
     System.out.println("Nothing was returned"); 
     return; 
   } 

   if (true == st.hasMoreTokens()) { 
     sHTTPReturnCode = st.nextToken(); 
     try { 
       iRc = Integer.parseInt(sHTTPReturnCode); 
     } catch (NumberFormatException ne) {;}

     switch (iRc) { 
     case(200):
       System.out.println("HTTP Response code: OK, " + iRc); 
       break; 
     case(400): case(401): case(402): case(403): case(404): 
       System.out.println("HTTP Response code: Client Error, " + iRc); 
       break; 
     case(500): case(501): case(502): case(503): 
       System.out.println("HTTP Response code: Server Error, " + iRc); 
       break; 
     default: 
       System.out.println("HTTP Response code: Unknown, " + iRc); 
       break; 
     } 
   }

   if (true == st.hasMoreTokens()) { 
     while (true == st.hasMoreTokens()) { 
       sbText.append(st.nextToken()); 
       sbText.append(" "); 
       } 
     System.out.println("HTTP Response phrase: " + sbText.toString()); 
   } 
  } 
}

Icon that indicates the type of topic Reference topic



Timestamp icon Last updated: March 23, 2018 0:18
File name: rprf_advexreturn.html