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
- Provide overall control.
- Receive initial requests and return client reference (HTML with Applet tag).
- Start Listen Server.
- Kill Listen Server.
Issues
- Recognize (limited) set of user actions.
- Create and start Listen Server thread.
- Stop and "destroy" Listen Server.
Listen Layer
Major Function
- Create Server Socket and await client connections.
- Receive incoming connections.
- Create and start Communication object, passing it the client connection.
- Return to wait state and listen for additional client connections.
Issues
- A very simple object.
- Tricky to stop and release port.
- Must handle Exceptions carefully.
Communication Layer
Major Function
- Manage connection with the client.
- Provide I/O form/to the client through the connection.
- Map between Protocol and TransObj.
- Pass TransObj to the appropriate AddressBook method.
Issues
- Close the connection.
- Catch and respond to Exceptions.
- Determine method to call.
- Build helper methods.
AddressBook Layer
Major Function
- Support methods that constitute API and correspond with protocol methods.
- Map between TransObj and internal data object.
- Provide persistent storage for internal data object.
Issues
- Load data object.
- Save data object.
- When and how often to do both of the above.
- Determine data structure for the data object.
- Determine mechanisms for supporting API methods with respect to the data structure.
- Build helper methods.
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 ServerThreadedAdditional 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
- buildRequestProtocol ( TransObj )
- buildResponseProtocol ( EntryObj )
- buildEntryObj ( Protocol )
- getABMethod ( RequestProtocol )
- getABKey ( EntryObj )
- getABValue ( EntryObj )
- readAB ( ABDataStructure )
- writeAB ( ABDataStructure )