{ "key_pair_value_system": true, "answer_rating_count": "", "question_feedback_html": { "html_star": "", "html_star_feedback": "" }, "answer_average_rating_value": "", "answer_date_js": "2024-09-06T17:16:30-04:00", "answer_date": "2024-09-06 17:16:30", "is_docs_available": null, "is_excel_available": null, "is_pdf_available": null, "count_file_available": 0, "main_page": "student_question_view", "question_id": "9360989", "url": "\/study-help\/questions\/multithreaded-web-server-i-need-to-write-a-multithreaded-web-9360989", "question_creation_date_js": "2024-09-06T17:16:30-04:00", "question_creation_date": "Sep 06, 2024 05:16 PM", "meta_title": "[Solved] Multi-Threaded Web Server I need to write | SolutionInn", "meta_description": "Answer of - Multi-Threaded Web Server I need to write a multi-threaded web server to serve incoming requests from clients in a com | SolutionInn", "meta_keywords": "multi-threaded,web,server,write,serve,incoming,requests,clients,computer,network,will,implemented", "question_title_h1": "Multi-Threaded Web Server I need to write a multi-threaded web server to serve incoming requests from clients in a computer network. The web server will", "question_title": "Multi-Threaded Web Server I need to write a multi-threaded web server to", "question_title_for_js_snippet": "Multi Threaded Web Server I need to write a multi threaded web server to serve incoming requests from clients in a computer network The web server will be implemented using two Java source files and one directory folder which contains the website files The two source files are File No 1 WebRequest java File No 2 WebServer java File No 1 is already implemented but File No 2 is not implemented and should be implemented we have a simple website which has only one html file that must show the first and last name When displayed by a web browser , shown below (index html) Test Web Server Network Programming first Name last name I am the mutli threaded web server that you created Assuming an error prone network, the following points must be done 1 Study the source code written in File No 1 carefully 2 Modify the given html file to display your name and registration number 3 Write a source code for a multi threaded web server that utilises the source code in File No 1 This code (i e , the multi threaded web server code) should take two arguments from the user a) Port number b) Directory Folder path and name 4 Save the source code for the multi threaded web server in File No 2 5 Compile File No 1 and File No 2 then run File No 2 6 Access the web server from a web browser and observe the web page 7 Monitor the transport layer states and events using netstat when you access the web server using more than one tab in your web browser What happens when there is more than one client request What states are displayed on console terminal by netstat Comment on that please, let it be well commented file No 1 is written below import java io import java net import java util final class WebRequest implements Runnable final static String CRLF Socket socket String dirName Constructor public WebRequest(Socket socket, String dirName) throws Exception this socket socket this dirName dirName Implement the run() method of the Runnable interface public void run() try processRequest() catch (Exception e) System out println(e) private void processRequest() throws Exception Get a reference to the socket's input and output streams InputStream is socket getInputStream() DataOutputStream os new DataOutputStream(socket getOutputStream()) Set up input stream filters BufferedReader br new BufferedReader(new InputStreamReader(is)) Get the request line of the HTTP request message String requestLine br readLine() Extract the filename from the request line StringTokenizer tokens new StringTokenizer(requestLine) tokens nextToken() skip over the method, which should be GET String fileName tokens nextToken() Prepend a so that file request is within the current directory fileName fileName Prepend directory path fileName this dirName fileName System out println( Filename fileName) Open the requested file FileInputStream fis null boolean fileExists true try fis new FileInputStream(fileName) catch (FileNotFoundException e) fileExists false Debug info for private use System out println( Incoming ) System out println(requestLine) String headerLine null while ((headerLine br readLine()) length() 0) System out println(headerLine) Construct the response message String statusLine null String contentTypeLine null String entityBody null if (fileExists) statusLine HTTP 1 0 200 OK CRLF contentTypeLine Content Type contentType(fileName) CRLF else statusLine HTTP 1 0 404 Not Found CRLF contentTypeLine Content Type text html CRLF entityBody Not Found Not Found Send the status line os writeBytes(statusLine) Send the content type line os writeBytes(contentTypeLine) Send a blank line to indicate the end of the header lines os writeBytes(CRLF) Send the entity body if (fileExists) sendBytes(fis, os) fis close() else os writeBytes(entityBody) Close streams and socket os close() br close() socket close() private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception Construct a 1K buffer to hold bytes on their way to the socket byte buffer new byte 1024 int bytes 0 Copy requested file into the socket's output stream while ((bytes fis read(buffer)) 1) os write(buffer, 0, bytes) private static String contentType(String fileName) if (fileName endsWith( htm ) fileName endsWith( html )) return text html if (fileName endsWith( ram ) fileName endsWith( ra )) return audio x pn realaudio return application octet stream ", "question_description": "

Multi-Threaded Web Server<\/p>

I need to write a multi-threaded web server to serve incoming requests from clients in a computer network. The web server will be implemented using two Java source files and one directory\/folder which contains the website files. The two source files are: - File No. 1: WebRequest.java - File No. 2: WebServer.java<\/p>

File No. 1 is already implemented but File No. 2 is not implemented and should be implemented. we have a simple website which has only one html file that must show the first and last name. When displayed by a web browser , shown below (index.html)<\/p>

Test Web Server<\/p>

<\/p>

Network Programming <\/p>

<\/p>

first Name:<\/p>

<\/p>

last name:<\/p> <\/td>

I am the mutli-threaded web server that you created <\/p> <\/td> <\/tr> <\/tbody> <\/table>

Assuming an error-prone network, the following points must be done:<\/p>

1. Study the source code written in File No. 1 carefully.<\/p>

2. Modify the given html file to display your name and registration number. <\/p>

3. Write a source code for a multi-threaded web server that utilises the source code in File No. 1. This code (i.e., the multi-threaded web server code) should take two arguments from the user: a) Port number. b) Directory\/Folder path and name. <\/p>

4. Save the source code for the multi-threaded web server in File No. 2.<\/p>

5. Compile File No. 1 and File No. 2 then run File No. 2 <\/p>

6. Access the web server from a web browser and observe the web page.<\/p>

7. Monitor the transport-layer states and events using netstat when you access the web server using more than one tab in your web browser. What happens when there is more than one client request? What states are displayed on console\/terminal by netstat? Comment on that.<\/p>

please, let it be well commented.<\/p>

file No. 1 is written below: <\/p>

import java.io.* ; import java.net.* ; import java.util.* ;<\/p>

final class WebRequest implements Runnable { final static String CRLF = \" \"; Socket socket; String dirName; \/\/ Constructor public WebRequest(Socket socket, String dirName) throws Exception { this.socket = socket; this.dirName = dirName; } \/\/ Implement the run() method of the Runnable interface. public void run() { try { processRequest(); } catch (Exception e) { System.out.println(e); } }<\/p>

private void processRequest() throws Exception { \/\/ Get a reference to the socket's input and output streams. InputStream is = socket.getInputStream(); DataOutputStream os = new DataOutputStream(socket.getOutputStream()); \/\/ Set up input stream filters. BufferedReader br = new BufferedReader(new InputStreamReader(is)); \/\/ Get the request line of the HTTP request message. String requestLine = br.readLine();<\/p>

\/\/ Extract the filename from the request line. StringTokenizer tokens = new StringTokenizer(requestLine); tokens.nextToken(); \/\/ skip over the method, which should be \"GET\" String fileName = tokens.nextToken(); \/\/ Prepend a \".\" so that file request is within the current directory. \/\/fileName = \".\" + fileName ; \/\/ Prepend directory path fileName = this.dirName + fileName;<\/p>

System.out.println(\"Filename: \" + fileName); \/\/ Open the requested file. FileInputStream fis = null ; boolean fileExists = true ; try { fis = new FileInputStream(fileName); } catch (FileNotFoundException e) { fileExists = false ; }<\/p>

\/\/ Debug info for private use System.out.println(\"Incoming!!!\"); System.out.println(requestLine); String headerLine = null; while ((headerLine = br.readLine()).length() != 0) { System.out.println(headerLine); } \/\/ Construct the response message. String statusLine = null; String contentTypeLine = null; String entityBody = null; if (fileExists) { statusLine = \"HTTP\/1.0 200 OK\" + CRLF; contentTypeLine = \"Content-Type: \" + contentType(fileName) + CRLF; } else { statusLine = \"HTTP\/1.0 404 Not Found\" + CRLF; contentTypeLine = \"Content-Type: text\/html\" + CRLF; entityBody = \"\" + \"Not Found<\/title><\/head>\" + \"<body>Not Found<\/body><\/html>\"; } \/\/ Send the status line. os.writeBytes(statusLine);<\/p> <p> \/\/ Send the content type line. os.writeBytes(contentTypeLine);<\/p> <p> \/\/ Send a blank line to indicate the end of the header lines. os.writeBytes(CRLF);<\/p> <p> \/\/ Send the entity body. if (fileExists) { sendBytes(fis, os); fis.close(); } else { os.writeBytes(entityBody) ; }<\/p> <p> \/\/ Close streams and socket. os.close(); br.close(); socket.close(); }<\/p> <p> private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception { \/\/ Construct a 1K buffer to hold bytes on their way to the socket. byte[] buffer = new byte[1024]; int bytes = 0; \/\/ Copy requested file into the socket's output stream. while ((bytes = fis.read(buffer)) != -1) { os.write(buffer, 0, bytes); } }<\/p> <p> private static String contentType(String fileName) { if (fileName.endsWith(\".htm\") || fileName.endsWith(\".html\")) { return \"text\/html\"; } if (fileName.endsWith(\".ram\") || fileName.endsWith(\".ra\")) { return \"audio\/x-pn-realaudio\"; } return \"application\/octet-stream\" ; } }<\/p> <p> <\/p> <p> <\/p>", "transcribed_text": "", "related_book": { "title": null, "isbn": null, "edition": null, "authors": null, "cover_image": null, "uri": null, "see_more_uri": "" }, "free_related_book": { "isbn": "", "uri": "", "name": "", "edition": "" }, "question_posted": "2024-09-06 17:16:30", "see_more_questions_link": "\/study-help\/questions\/computer-science-computer-network-2023-February-20", "step_by_step_answer": "The Answer is in the image, click to view ...", "students_also_viewed": [ { "url": "\/study-help\/psychology\/3-what-is-a-duchenne-smile-1986465", "description": "3. What is a Duchenne smile?", "stars": 0 }, { "url": "\/you-are-the-manager-of-the-examination-engagement-of-the", "description": "You are the manager of the examination engagement of the financial projection of Honeys Health Foods as of December 31, 2013, and for the year then ended. The audit senior, Currie, has prepared the...", "stars": 3 }, { "url": "\/study-help\/questions\/26-the-dean-company-has-sales-of-241000-and-the-10092009", "description": "#26 The Dean Company has sales of $241,000, and the break-even point in sales dollars of $200,030. Determine the company's margin of safety percentage. Round answer to the nearest whole number....", "stars": 3 }, { "url": "\/study-help\/questions\/question-13-5-pts-colton-corporations-semiannual-bonds-have-a-7718066", "description": "Question 13 5 pts Colton Corporation's semiannual bonds have a 12-year maturity, an 8.70% nominal coupon paid semiannually, and sell at their $1,000 par value. The firm's annual bonds have the same...", "stars": 3 }, { "url": "\/study-help\/questions\/project-administrators-have-a-similar-role-to-the-project-manager-4728795", "description": "Project administrators have a similar role to the project manager. However, the project administrator must receive approval for their decisions by whom", "stars": 3 }, { "url": "\/study-help\/questions\/using-pestle-as-a-guide-explain-the-change-management-implications-4776029", "description": "Using PESTLE as a guide, explain the change management implications of not paying employees while they are checking out of the store. Assume that the store is located in a state that requires...", "stars": 3 }, { "url": "\/study-help\/questions\/over-the-next-decade-foleo-enterprises-pty-ltd-grew-exponentially-4283120", "description": "Over the next decade, Foleo Enterprises Pty Ltd grew exponentially, and whilst the increased demand for their Australian-made phones was welcome, it was unexpected and unplanned. James and Leon tried...", "stars": 3 }, { "url": "\/study-help\/questions\/pharoah-company-reported-the-following-information-in-its-general-ledger-4286910", "description": "Pharoah Company reported the following information in its general ledger at December 31. All sales were on account. At the end of the year, uncollectible accounts were estimated to be 10% of accounts...", "stars": 3 }, { "url": "\/study-help\/questions\/a-jacketedagitated-vessel-fitted-with-a-turbine-agitator-is-used-865614", "description": "A jacketed-agitated vessel, fitted with a turbine agitator, is used for heating a water stream from 10C to 54C. The tank diameter D= 0.6 m, height H= 0.6 m, and agitator diameter D = 0.2 m. Saturated...", "stars": 3 } ], "next_back_navigation": { "previous": "\/study-help\/questions\/novak-corp-is-planning-to-replace-an-old-asset-with-9360988", "next": "\/study-help\/questions\/i-week-6-acc100269va016-x-a-aleks-ashley-mckee-9360990" }, "breadcrumbs": [ { "name": "Study help", "link": "https:\/\/www.solutioninn.com\/study-help\/questions-and-answers" }, { "name": "Computer Science", "link": "https:\/\/www.solutioninn.com\/study-help\/questions-and-answers\/computer-science" }, { "name": "Databases", "link": "https:\/\/www.solutioninn.com\/study-help\/questions\/computer-science-databases" }, { "name": "Multi-Threaded Web Server I need to write a multi-threaded web server to", "link": "https:\/\/www.solutioninn.com\/study-help\/questions\/multithreaded-web-server-i-need-to-write-a-multithreaded-web-9360989" } ], "skill_details": { "skill_id": "656", "skill_name": "Databases", "parent_id": "8" } }