Programming Project: Building a Web Server

Similar documents
UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

Lab 4. Name: Checked: Objectives:

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

COP2800 Homework #3 Assignment Spring 2013

OASIS SUBMISSIONS FOR FLORIDA: SYSTEM FUNCTIONS

Project #1 - Fraction Calculator

To start your custom application development, perform the steps below.

LAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C

Guide to getting started in J2ME for the Motorola A780 phone

Integrating QuickBooks with TimePro

Ascii Art Capstone project in C

PAY EQUITY HEARINGS TRIBUNAL. Filing Guide. A Guide to Preparing and Filing Forms and Submissions with the Pay Equity Hearings Tribunal

Getting Started with the Web Designer Suite

Reading and writing data in files

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2

Relius Documents ASP Checklist Entry

REFWORKS: STEP-BY-STEP HURST LIBRARY NORTHWEST UNIVERSITY

Secure File Transfer Protocol (SFTP) Interface for Data Intake User Guide

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Systems & Operating Systems

Municode Website Instructions


These tasks can now be performed by a special program called FTP clients.

Using the Swiftpage Connect List Manager

Test Pilot User Guide

Using CppSim to Generate Neural Network Modules in Simulink using the simulink_neural_net_gen command

Using the Swiftpage Connect List Manager

Access the site directly by navigating to in your web browser.

Adverse Action Letters

Proper Document Usage and Document Distribution. TIP! How to Use the Guide. Managing the News Page

Element Creator for Enterprise Architect

ONTARIO LABOUR RELATIONS BOARD. Filing Guide. A Guide to Preparing and Filing Forms and Submissions with the Ontario Labour Relations Board

Louisiana 2D Bar Code General Information

Network programming 14/01/2013. Introduction. Session objectives. Client/Server working model. Advanced Java Programming Course

TECHNICAL REQUIREMENTS

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2 Iterative Code Design handout Style Guidelines handout

Launching Xacta 360 Marketplace AMI Guide June 2017

Managing Your Access To The Open Banking Directory How To Guide

Towne Information Systems, Inc. Inter-Office Correspondence

Element Creator for Enterprise Architect

CS1150 Principles of Computer Science Introduction (Part II)

Uploading Files with Multiple Loans

User Guide. ACE Data Source. OnCommand Workflow Automation (WFA) Abstract PROFESSIONAL SERVICES

Faculty Textbook Adoption Instructions

Simple Regression in Minitab 1

Online Image Viewing Agent User Guide. Texas FAIR Plan Association 1

INSTALLING CCRQINVOICE

Infrastructure Series

The programming for this lab is done in Java and requires the use of Java datagrams.

Preparing a REST API. Rules of REST APIs, API patterns, Typical CRUD operations

User Guide. Document Version: 1.0. Solution Version:

Interfacing to MATLAB. You can download the interface developed in this tutorial. It exists as a collection of 3 MATLAB files.

VMware AirWatch Certificate Authentication for Cisco IPSec VPN

Create Your Own Report Connector

CSCI L Topics in Computing Fall 2018 Web Page Project 50 points

ClassFlow Administrator User Guide

ROCK-POND REPORTING 2.1

BMC Remedyforce Integration with Bomgar Remote Support


1on1 Sales Manager Tool. User Guide

Step 3:- You Will See the Sign-in Page. Then Enter your Login ID & Password and Click on the Sign in Button.

ONLINE GRANT APPLICATION INSTRUCTIONS

Client Configurations

AngularJS. Unit Testing AngularJS Directives with Karma & Jasmine

Reviewer Information Sheet for Committee Members

History of Java. VM (Java Virtual Machine) What is JVM. What it does. 1. Brief history of Java 2. Java Version History

Traffic Shaping (Part 1)

Guide for Referees 2018

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

McGill University School of Computer Science COMP-206. Software Systems. Due: September 29, 2008 on WEB CT at 23:55.

A Purchaser s Guide to CondoCerts

COURSE DIRECTIVES FOR FACULTY

Extended Traceability Report for Enterprise Architect

Applet: Java program that are typically embedded in XHTML documents.

Enterprise Installation

Copyrights and Trademarks

BMC Remedyforce Integration with Remote Support

3 AXIS STAGE CONTROLLER

Date: October User guide. Integration through ONVIF driver. Partner Self-test. Prepared By: Devices & Integrations Team, Milestone Systems

Automatic imposition version 5

Xilinx Answer Xilinx PCI Express DMA Drivers and Software Guide

C Sc 335 Practice Test 1 Section Leader Name 150pts. 3. List two Java structures that allow for polymorphic messages (2pts)

$ARCSIGHT_HOME/current/user/agent/map. The files are named in sequential order such as:

Gemini Intercom Quick Start Guide

Guidance for Submitting an application or Nomination in AAS Ishango Online System

I - EDocman Installation EDocman component EDocman Categories module EDocman Documents Module...2

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture2 Functions

HeW Electronic Bill Pay Training Manual F o r H e W S y s t e m s

Overview of Data Furnisher Batch Processing

Campuses that access the SFS nvision Windows-based client need to allow outbound traffic to:

OPERATING MANUAL. Exceed GERMANY. Electronic Postage Computing Scale CONTENTS

Arius 3.0. Release Notes and Installation Instructions. Milliman, Inc Peachtree Road, NE Suite 1900 Atlanta, GA USA

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

But for better understanding the threads, we are explaining it in the 5 states.

BI Publisher TEMPLATE Tutorial

HOW TO REGISTER FOR THE TEAS ASSESSMENT 1. CREATE A NEW ACCOUNT. How to Register for the TEAS Assessment 1

html o Choose: Java SE Development Kit 8u45

CS1150 Principles of Computer Science Methods

I. Introduction: About Firmware Files, Naming, Versions, and Formats

Transcription:

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.