Prgramming Prject: Building a Web Server Submissin Instructin: Grup prject Submit yur cde thrugh Bb by Dec. 8, 2014 11:59 PM. Yu need t generate a simple index.html page displaying all yur grup members name. Fr a Webserver, a template is given. Yur grup needs t cmplete the cde. Then, generate a multi-threaded webserver. Yu will develp a Web server. Yu shuld be able t demnstrate that yur Web server is capable f delivering yur hme page t a Web brwser. We are ging t implement versin 1.0 f HTTP, as defined in RFC 1945, where separate HTTP requests are sent fr each cmpnent f the Web page. The server will be able t handle a service requests. The server listens t a fixed prt. When it receives a TCP cnnectin request, it sets up a TCP cnnectin thrugh anther prt and services the request in a separate thread. After this prgram is running prperly, yu will add the cde required t generate an apprpriate respnse. As yu are develping the cde, yu can test yur server frm a Web brwser. But remember that yu are nt serving thrugh the standard prt 80, s yu need t specify the prt number within the URL that yu give t yur brwser. Fr example, if yur machine's name is hst.smeschl.edu, yur server is listening t prt 6789, and yu want t retrieve the file index.html, then yu wuld specify the fllwing URL within the brwser: http://hst.smeschl.edu:6789/index.html If yu mit ":6789", the brwser will assume prt 80 which mst likely will nt have a server listening n it. When the server encunters an errr, it sends a respnse message with the apprpriate HTML surce s that the errr infrmatin is displayed in the brwser windw. Web Server in Java: Part A In the fllwing steps, we will g thrugh the cde fr the first implementatin f ur Web Server. Wherever yu see "", yu will need t supply a missing detail. Our first implementatin f the Web server will be multi-threaded, where the prcessing f each incming request will take place inside a separate thread f executin. This allws the server t service multiple clients in parallel, r t perfrm multiple file transfers t a single client in parallel. When we create a new thread f executin, we need t pass t the Thread's cnstructr an instance f sme class that implements the Runnable interface. This is the reasn that we define a separate class called HttpRequest. The structure f the Web server is shwn belw: imprt java.i.* ; imprt java.net.* ; imprt java.util.* ; public final class WebServer public static vid main(string argv[]) thrws Exceptin
final class HttpRequest implements Runnable Nrmally, Web servers prcess service requests that they receive thrugh well-knwn prt number 80. Yu can chse any prt higher than 1024, but remember t use the same prt number when making requests t yur Web server frm yur brwser. public static vid main(string argv[]) thrws Exceptin // Set the prt number. int prt = 6789; Next, we pen a scket and wait fr a TCP cnnectin request. Because we will be servicing request messages indefinitely, we place the listen peratin inside f an infinite lp. This means we will have t terminate the Web server by pressing ^C n the keybard. // Establish the listen scket. // Prcess HTTP service requests in an infinite lp. while (true) // Listen fr a TCP cnnectin request. When a cnnectin request is received, we create an HttpRequest bject, passing t its cnstructr a reference t the Scket bject that represents ur established cnnectin with the client. // Cnstruct an bject t prcess the HTTP request message. HttpRequest request = new HttpRequest( ); // Create a new thread t prcess the request. Thread thread = new Thread(request); // Start the thread. thread.start(); In rder t have the HttpRequest bject handle the incming HTTP service request in a separate thread, we first create a new Thread bject, passing t its cnstructr a reference t the HttpRequest bject, and then call the thread's start() methd. After the new thread has been created and started, executin in the main thread returns t the tp f the message prcessing lp. The main thread will then blck, waiting fr anther TCP cnnectin request,
while the new thread cntinues running. When anther TCP cnnectin request is received, the main thread ges thrugh the same prcess f thread creatin regardless f whether the previus thread has finished executin r is still running. This cmpletes the cde in main(). Fr the remainder f the lab, it remains t develp the HttpRequest class. We declare tw variables fr the HttpRequest class: CRLF and scket. Accrding t the HTTP specificatin, we need t terminate each line f the server's respnse message with a carriage return (CR) and a line feed (LF), s we have defined CRLF as a cnvenience. The variable scket will be used t stre a reference t the cnnectin scket, which is passed t the cnstructr f this class. The structure f the HttpRequest class is shwn belw: final class HttpRequest implements Runnable final static String CRLF = "\r\n"; Scket scket; // Cnstructr public HttpRequest(Scket scket) thrws Exceptin this.scket = scket; // Implement the run() methd f the Runnable interface. public vid run() private vid prcessrequest() thrws Exceptin In rder t pass an instance f the HttpRequest class t the Thread's cnstructr, HttpRequest must implement the Runnable interface, which simply means that we must define a public methd called run() that returns vid. Mst f the prcessing will take place within prcessrequest(), which is called frm withinrun(). Up until this pint, we have been thrwing exceptins, rather than catching them. Hwever, we can nt thrw exceptins frm run(), because we must strictly adhere t the declaratin f run() in the Runnable interface, which des nt thrw any exceptins. We will place all the prcessing cde in prcessrequest(), and frm there, thrw exceptins t run(). Within run(), we explicitly catch and handle exceptins with a try/catch blck. // Implement the run() methd f the Runnable interface. public vid run() try prcessrequest(); catch (Exceptin e) System.ut.println(e);
Nw, let's develp the cde within prcessrequest(). We first btain references t the scket's input and utput streams. Then we wrap InputStreamReader and BufferedReader filters arund the input stream. Hwever, we wn't wrap any filters arund the utput stream, because we will be writing bytes directly int the utput stream. private vid prcessrequest() thrws Exceptin // Get a reference t the scket's input and utput streams. InputStream is = ; DataOutputStream s = ; // Set up input stream filters. BufferedReader br = ; Nw we are prepared t get the client's request message, which we d by reading frm the scket's input stream. The readline() methd f the BufferedReader class will extract characters frm the input stream until it reaches an end-f-line character, r in ur case, the end-f-line character sequence CRLF. The first item available in the input stream will be the HTTP request line. (See Sectin 2.2 f the textbk fr a descriptin f this and the fllwing fields.) // Get the request line f the HTTP request message. String requestline = ; // Display the request line. System.ut.println(); System.ut.println(requestLine); After btaining the request line f the message header, we btain the header lines. Since we dn't knw ahead f time hw many header lines the client will send, we must get these lines within a lping peratin. // Get and display the header lines. String headerline = null; while ((headerline = br.readline()).length()!= 0) System.ut.println(headerLine); We dn't need the header lines, ther than t print them t the screen, s we use a temprary String variable, headerline, t hld a reference t their values. The lp terminates when the expressin (headerline = br.readline()).length() evaluates t zer, which will ccur when headerline has zer length. This will happen when the empty line terminating the header lines is read. (See the HTTP Request Message diagram in Sectin 2.2 f the textbk)
In the next step f this lab, we will add cde t analyze the client's request message and send a respnse. But befre we d this, let's try cmpiling ur prgram and testing it with a brwser. Add the fllwing lines f cde t clse the streams and scket cnnectin. // Clse streams and scket. s.clse(); br.clse(); scket.clse(); After yur prgram successfully cmpiles, run it with an available prt number, and try cntacting it frm a brwser. T d this, yu shuld enter int the brwser's address text bx the IP address f yur running server. Fr example, if yur machine name is hst.smeschl.edu, and yu ran the server with prt number 6789, then yu wuld specify the fllwing URL: http://hst.smeschl.edu:6789/ The server shuld display the cntents f the HTTP request message. Check that it matches the message frmat shwn in the HTTP Request Message diagram in Sectin 2.2 f the textbk.