com.javaranch.common
Class Servlets

java.lang.Object
  |
  +--com.javaranch.common.Servlets

public class Servlets
extends java.lang.Object

This class simplifies the exchange of objects between clients and Servlets via the HTTP protocol.

All receiving is embedded within an HTTP POST command.

All objects sent and received must implement Serializable.

The server-side methods in this class interact with the client-side methods in com.javaranch.common.HTTP.

Example:


   Client applet


    import java.applet.* ;
    import java.awt.* ;
    import java.awt.event.* ;
    import com.javaranch.common.* ;

    public class ObjectApplet extends Applet implements ActionListener
    {

        private static final String servlet = "http://www.javaranch.com/servlet/ObjectServer" ;
        private static final String[] data = {"jan","feb","mar","apr","may","jun"};
        private TextField field;  // shows my results

        public void actionPerformed( ActionEvent e )
        {
            try
            {
                String[] s = (String[]) HTTP.send( servlet , data );
                field.setText( s[0] +'|'+ s[1] +'|'+ s[2] +'|'+ s[3] +'|'+ s[4] +'|'+ s[5] );
            }
            catch( Exception ex )
            {
                field.setText("uh oh... " + ex );
            }
        }

        public void init()
        {
            Button b = new Button("send data");
            b.addActionListener( this );
            add( b );
            field = new TextField("start",50);
            add( field );
        }

    }


   Server servlet

   import java.io.* ;
   import javax.servlet.http.* ;
   import javax.servlet.* ;
   import com.javaranch.common.* ;

   public class ObjectServer extends HttpServlet implements SingleThreadModel
   {

       public void doPost( HttpServletRequest req , HttpServletResponse resp )
       {
           resp.setContentType("application/binary");
           try
           {
               // the object sent is a string array.  Sort it backwards and send it back.
               String[] old = (String[])Servlets.getObjectFromClient( req );
               String[] s = new String[ old.length ];
               int topIndex = old.length - 1 ;
               for( int i = 0 ; i < old.length ; i++ )
               {
                   s[ topIndex - i ] = old[ i ] ;
               }
               Servlets.sendObjectToClient( resp , s );
           }
           catch ( Exception e )
           {
               System.out.println( "ObjectServer: " + e );
           }
       }

   }

    
- - - - - - - - - - - - - - - - -

Copyright (c) 1999-2004 Paul Wheaton

Copyright (c) 1999-2000 EarthWatch, Inc. You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

I originally developed this class while working as a contractor at EarthWatch, Inc. in Longmont, Colorado. They gave me permission to distribute this code this way provided that their message would also be carried along. Their message is that they hire Java programmers and would like you to consider working with them. I have to say that my experience with them was first rate and I would encourage engineers to work there. Check out their web site at http://www.digitalglobe.com.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton


Method Summary
static java.lang.Object getObjectFromClient(HttpServletRequest req)
          Called by servlets to receive objects from applets or other client programs.
static void makeColumns(java.io.PrintWriter out, java.lang.String[] text, java.lang.String[] tags, int indent, int width)
          When making lists of things with radio buttons or checkboxes in PRE areas.
static void sendObjectToClient(HttpServletResponse resp, java.lang.Object obj)
          Called by servlets to send objects to applets or other client programs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getObjectFromClient

public static java.lang.Object getObjectFromClient(HttpServletRequest req)
                                            throws java.lang.Exception
Called by servlets to receive objects from applets or other client programs.

java.lang.Exception

sendObjectToClient

public static void sendObjectToClient(HttpServletResponse resp,
                                      java.lang.Object obj)
                               throws java.lang.Exception
Called by servlets to send objects to applets or other client programs.

java.lang.Exception

makeColumns

public static void makeColumns(java.io.PrintWriter out,
                               java.lang.String[] text,
                               java.lang.String[] tags,
                               int indent,
                               int width)
When making lists of things with radio buttons or checkboxes in PRE areas.

Parameters:
out - Where all this stuff will be written.

text - The text that the users will look at and will be arranged nice.

tags - The tags that go with the text.

indent - How many spaces to indent each line.

width - The number of characters wide after the indent.



Copyright ©2004 Paul Wheaton All Rights Reserved