Java Servlet Architecture

The discussion that follows is general with respect to Servlets that communicate through sockets with remote servers.  It is oriented, however, toward the Comp 118 Assignment 1.  It is also symmetric with the lesson on Java Applet Architecture.


WWW/Applet/Servlet Architecture

 

Example

 


 

Layered Servlet

as might be applied to WWW/Applet/Servlet Architecture

 

 

 


Layered Servlet

additional details

 

 


Servlet Layer

Major Function

Issues

 


Listen Layer

Major Function

Issues

 


Communication Layer

Major Function

Issues

 


AddressBook Layer

Major Function

Issues

 

 


 

Key Code Segments

 

Servlet Layer

 

public class ServerThreaded extends HttpServlet {


//*****  Mulit-threaded server, accepts multiple messages
//       default runs on jbssrv-cs.cs.unc.edu:8902

boolean serverRunning = false;
String host;
int serverPort, port;
String DEFAULT_HOST = "jbssrv-cs.cs.unc.edu";
int DEFAULT_PORT = 8902;

String message;

ListenServer listen;
  
public void doGet (HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
	{
if ( !serverRunning )  {

            setHostPort ( req );
            listen = new ListenServer ( port );
            listen.start ();
            message = "jbsServerThreaded Started on Tomcat,  Host " + host + ", port " + Integer.toString (port) ;
            sendAppletTag = true;
            returnHTML ( resp );
            serverRunning = true;
            return;
        }  // end firsttime
        else  {
            message = "jbsServerThreaded already Running";
            sendAppletTag = true;
            returnHTML ( resp );
            return;
            }
                   

    }  // end doGet
}  // end ServerThreaded

Additional Details

Setting the host value.

    try {
            InetAddress here = InetAddress.getLocalHost ();
            host = here.getHostAddress ();
        }  // end try
    catch (UnknownHostException e) { 
            System.out.println("setHostPort error: "); 
            e.printStackTrace();
        }

 


Listen Layer

 

class ListenServer extends Thread  {

ServerThreaded source;
ServerSocket listenSocket;
int port;
Socket connection;  
// **************  ListenServer

    ListenServer ( int p)  {
        super ();
        port = p;
    }  // end constructor


// **************  run

    public void run  ()  {

        try  {
            listenSocket = new ServerSocket ( port );
            while ( true )  {
                Socket connection = listenSocket.accept();
                HandleServer handleServer = new HandleServer ( connection );
                handleServer.start ();
            }  // end while

    }  catch ( IOException e )  {
        System.out.println("listenSocket IOException: " ); 
        e.printStackTrace() ;
    }  // end catch


}  // end run
}  // end ListenServer

 

Communication Layer

 

class HandleServer extends Thread  {

Socket connection;

InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;

String message;


// **************  HandleServer

    HandleServer ( Socket socket )  {
        super ();
        connection =  socket;
    }  // end constructor


// **************  run

    public void run  ()  {

        try  {

            outStream = connection.getOutputStream ();
            outDataStream = new DataOutputStream ( outStream );
            inStream = connection.getInputStream ();
            inDataStream = new DataInputStream ( inStream );

            while ( true )  {
                message = inDataStream.readUTF ();
                outDataStream.writeUTF ( message );
            }  // end while

        }  // end try

        catch ( EOFException e ) {
            try  {
                connection.close ();
                System.out.println("HandleServer: EOFException, handleSocket closed ok");
                //e.printStackTrace();
                return;
            }
            catch ( IOException ee )  {
                System.out.println("HandleServer: IOException, handleSocket closed ok");
                ee.printStackTrace();
                return;
            }  // end IOException

         }  // end catch EOFException
         catch ( IOException e )  {
            System.out.println("HandleServer: IOException caught");
            e.printStackTrace();
            return;
         }  // end catch IOException


    }  // end run


}  // end HandleServer

  

Example: http://wwwj.cs.unc.edu:8888/jbs/servlet/jbsServerThreaded


Big Picture

 

Helper Methods