|
@@ -0,0 +1,54 @@
|
|
1
|
+# Java Server
|
|
2
|
+The training wheels are coming off....sort of. In this lab, I set up the basic server for you. Your task is to handle other type of responses. You can refactor the code however you see fit and I highly advise you to do so.
|
|
3
|
+
|
|
4
|
+Read the wiki page [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) and figure out how to write the response for each of the request. I wrote a sample one for you to see. In `src/main/java/Main.java`, run the `main` method. Once the server is up, go `http://localhost:5000`, note it says `Hello world`.
|
|
5
|
+
|
|
6
|
+Your task for this lab is to read the [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) and figure out how to generate the proper response. Remember to write unit test.
|
|
7
|
+
|
|
8
|
+## Notes
|
|
9
|
+I will not answer the following questions:
|
|
10
|
+- What is a HTTP?
|
|
11
|
+- How do I construct an HTTP response?
|
|
12
|
+- What's an HTTP method?
|
|
13
|
+- What's an HTTP code?
|
|
14
|
+
|
|
15
|
+If you come to me, I will show you [LMGTFY](http://lmgtfy.com/)(Let me google that for you). I will answer anything related to why your code doesn't work. I will also answer design related questions. I will also help you construct the test if you know what message you want to write.
|
|
16
|
+
|
|
17
|
+## Text response
|
|
18
|
+1. Add a `/hello` path which will return `world` as a response
|
|
19
|
+2. Add a `/form?key=value` path which will return the value as a response. For example if I call `/form?search=blue`, the server will return `blue`. If I call `/form?color=red`, the server will return `red`. The response code should be `201`.
|
|
20
|
+3. Add a `/404` path which will `Are you lost?` with `404` response code.
|
|
21
|
+2. Edit the `/form?key=value` path so it will only response if it is a `POST` request. Any other type of request will a `404` response.
|
|
22
|
+
|
|
23
|
+## Redirect
|
|
24
|
+1. Add a `/redirect` path which will redirect you to `http://google.com`
|
|
25
|
+
|
|
26
|
+## Asynchronous requests
|
|
27
|
+Currently our server can handle one request at a time. This is called Synchronous. We want multiple users to access the server at the same time. This is called Asynchronous. Read up on the term [here](https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean).
|
|
28
|
+
|
|
29
|
+1. In the `run` method of the `RequestHandler`, add ` Thread.sleep(5000);` before the try and catch to make your server go to sleep for 5s before processing your request.
|
|
30
|
+ 1. Open up your browser and go to `http://localhost:5000`
|
|
31
|
+ 2. Open another tab and go to `http://localhost:5000`
|
|
32
|
+ 3. Note how long it takes for the second to get a response. That's because it has to wait for the first one to complete, then it will process the second request.
|
|
33
|
+2. Look into the [ExecutorService](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html) to see how you can use it to handle asynchronous requests
|
|
34
|
+
|
|
35
|
+## Image response
|
|
36
|
+### Part 1
|
|
37
|
+1. Add a new file in the resources folder
|
|
38
|
+2. Add a new path `/files/[image_name]` (e.g. `/files/high5.gif`) will show the image
|
|
39
|
+
|
|
40
|
+### Part 2
|
|
41
|
+1. Add a `/files` path. The files path will list out all the files in the `src/main/resources` folder in a list.
|
|
42
|
+2. Clicking on any of the file names will display the content of the file. If it is a picture, then show it as a picture.
|
|
43
|
+
|
|
44
|
+## Bonus - ORM
|
|
45
|
+1. Add ORMlite with a mysql database to your dependencies
|
|
46
|
+2. Add a User class with two fields `long id` and `String name`
|
|
47
|
+3. Add the dao necessary to create retreive the user
|
|
48
|
+ 1. Create 3 users
|
|
49
|
+4. Add `/users` path to display all the users name in a list.
|
|
50
|
+5. Add `/users/1` to show information for the first user (their name and id)
|
|
51
|
+
|
|
52
|
+#### Super bonus
|
|
53
|
+1. Add `/users/new` path to show a form where user can enter a name to create a new user.
|
|
54
|
+2. Add `/users` with a `POST` method to accept the form information and create the user. Redirect to `/users/[user_id]`
|