-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleHttpServer.java
More file actions
150 lines (106 loc) · 6 KB
/
Copy pathSimpleHttpServer.java
File metadata and controls
150 lines (106 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.tutorial_week04_questions;
/*
* 5COSC022W.2 Client-Server Architectures
* Week 04 Lab: Multi-threaded HTTP Server
* * INSTRUCTIONS:
* Follow the 40 Tasks in the Lab Tutorial PDF to implement this server.
*/
// -----------------------------------------------------------------
// Part 1: Imports and Core Setup
// -----------------------------------------------------------------
// TODO: Task 01: Import the HttpServer class from com.sun.net.httpserver
// TODO: Task 02: Import the HttpHandler interface
// TODO: Task 03: Import the HttpExchange class
// TODO: Task 04: Import java.io.IOException
// TODO: Task 05: Import java.io.OutputStream
// TODO: Task 06: Import java.net.InetSocketAddress
// TODO: Task 07: Import java.util.concurrent.Executors
// TODO: Task 08: Import java.util.concurrent.atomic.AtomicInteger
// (Note: You may need to add 'import java.io.InputStream;' for Part 5)
public class SimpleHttpServer {
public static void main(String[] args) throws IOException {
// TODO: Task 09: Define an integer variable 'port' and set it to 8000
// TODO: Task 10: Create the HttpServer instance (HttpServer.create)
// using new InetSocketAddress(port) and backlog 0.
// -----------------------------------------------------------------
// Part 2: Routing and Threading
// -----------------------------------------------------------------
// TODO: Task 11: Create a context for "/" and instantiate new RootHandler()
// TODO: Task 12: Create a context for "/greet" and instantiate new GreetHandler()
// TODO: Task 13: Create a context for "/echo" and instantiate new EchoHandler()
// TODO: Task 14: Create a context for "/stats" and instantiate new StatsHandler()
// TODO: Task 15: Call server.setExecutor(...)
// TODO: Task 16: Pass Executors.newCachedThreadPool() into the executor setter
// TODO: Task 17: Print "Server started on port 8000" to the console
// TODO: Task 18: Call server.start() to begin listening
}
// -----------------------------------------------------------------
// Part 3: The Root Handler
// -----------------------------------------------------------------
// TODO: Task 19: Create a static inner class named RootHandler that implements HttpHandler
/*
static class RootHandler implements HttpHandler {
// TODO: Task 20: Override the handle method taking HttpExchange as a parameter
@Override
public void handle(HttpExchange exchange) throws IOException {
// TODO: Task 21: Define a String variable named 'response' containing "Server is online and running."
// TODO: Task 22: Send response headers using exchange.sendResponseHeaders
// TODO: Task 23: Use status code 200 and response.length() for the headers
// TODO: Task 24: Open a try-with-resources block to get the output stream: exchange.getResponseBody()
// try (OutputStream os = ...) {
// TODO: Task 25: Write the response bytes using os.write(response.getBytes())
// TODO: Task 26: Ensure the try block closes automatically
// }
}
}
*/
// -----------------------------------------------------------------
// Part 4: The Greet Handler
// -----------------------------------------------------------------
// TODO: Task 27: Create the GreetHandler class implementing HttpHandler
/*
static class GreetHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
// TODO: Task 28: Get the query string using exchange.getRequestURI().getQuery()
// TODO: Task 29: Initialize a String variable 'name' to "Stranger"
// TODO: Task 30: Check if the query is not null
// TODO: Task 31: Split the query string by the "&" character
// TODO: Task 32: Loop through the split pairs and split each by "="
// TODO: Task 33: If the key equals "name", update the 'name' variable
// TODO: Task 34: Send headers (200) and write the response "Hello, [Name]!"
}
}
*/
// -----------------------------------------------------------------
// Part 5: Echo and Stats (POST & Concurrency)
// -----------------------------------------------------------------
// TODO: Task 35: Define EchoHandler. Inside handle, check if method is "POST"
/*
static class EchoHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
// if (POST) {
// TODO: Task 36: Read input stream (getRequestBody), convert to String, and write as response
// } else {
// TODO: Task 37: If NOT POST, send 405 Method Not Allowed
// }
}
}
*/
// TODO: Task 38: Define StatsHandler. Create a private final AtomicInteger requestCount = new AtomicInteger(0);
/*
static class StatsHandler implements HttpHandler {
// (Field definition goes here)
@Override
public void handle(HttpExchange exchange) throws IOException {
// TODO: Task 39: Call requestCount.incrementAndGet()
// TODO: Task 40: Write the response "Total requests: " + count
}
}
*/
}