Thursday, October 30, 2014

Planting an Embryo

As promised, today we will create the embryo of our general purpose servlet. First, we must discuss licensing. The open-source project materials found at java.net, blog content, and source code here are under the SCSL 3.0 license found at ( http://jcp.org/aboutJava/community/process/SCSL3.0.rtf ) and copyrighted by the author, Jody Sharpe.

The embryo is in the form of a simple JAVA class. It was created in NetBeans and is below. The output is an HTML file.

//START SOURCE CODE
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cab;
/**
 * The CAB will eventually for the kernel of the CABOOSE general purpose servlet
 * @author Jody Sharpe
 */
public class CAB {
    //Simulated URL String. We will eventually get all of our input from the
    //the servlet,s Request object.
    String request = "?project=caboose&module=skeleton&create_date=10.30.2014";
    //Simple response in the form of an HTML page. We will supply any response
    //via the servlet's Response object.
    String response = "<html><head><title>CABOOSE Embryo</title></head>" +
            "<body>#PROJECT#<p>#MODULE#<p>#CREATE_DATE#<p></body></html>";
   
    java.util.Map<String,String> mappingRequestNameValuePairs( String request ){
        java.util.Map<String,String> aMap = new java.util.Hashtable<String,String>();
        request = request.replace("?","");
        String[] theNameValuePairs = request.split("&");
        for( String aNameValuePair : theNameValuePairs ){
            final int theName = 0, theValue = 1;
            String [] aTuple = aNameValuePair.split("=");
            aMap.put( aTuple[theName], aTuple[theValue]);
        }
        return aMap;
    }
   
    String preparingResponse( String stencil, java.util.Map<String,String> aRequestMap ){
  
        stencil = stencil.replace("#PROJECT#",aRequestMap.get("project"));
        stencil = stencil.replace("#MODULE#",aRequestMap.get("module"));
        stencil = stencil.replace("#CREATE_DATE#",aRequestMap.get("create_date"));
       
        return( stencil );
       
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        CAB aCAB = new CAB();
        java.util.Map<String,String> aRequestMap = aCAB.mappingRequestNameValuePairs( aCAB.request );
        System.out.println( aCAB.preparingResponse( aCAB.response, aRequestMap) );
    }
}
//END SOURCE CODE

That is it. Copy, compile, and run it. Remove the package statement if necessary. This is enough for now. While working on a graduate degree in software engineering, my instructors repeatedly presented research which stated that most fulltime professional engineers only produce 20-30 lines of verified and validated code in a day. So, we have accomplished reasonable amount for part-timers in a day.

I apologize for the late post. This morning was hectic since it is time for the monthly billing cycle.
Speak with you later, Team. La-La.

No comments:

Post a Comment