# Java Server 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. 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`. 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. ## Notes I will not answer the following questions: - What is a HTTP? - How do I construct an HTTP response? - What's an HTTP method? - What's an HTTP code? 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. ## Text response 1. Add a `/hello` path so when I go to `http://localhost:5000/hello`, I will see `world` as the response 2. Add a `/form?key=value` path so when I go to `http://localhost:5000/form?key=value` I will see the value as the 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`. 3. Add a `/404` path so when I go to `http://localhost:5000/404` I will see the `Are you lost?` message with a `404` response code. 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. ## Redirect 1. Add a `/redirect` path so when I go to `http://localhost:5000/redirect` I will be redirect to `http://google.com` ## Asynchronous requests 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). 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. 1. Open up your browser and go to `http://localhost:5000` 2. Open another tab and go to `http://localhost:5000` 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. 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 ## Image response ### Part 1 1. Add a new file in the resources folder 2. Add a new path `/files/[image_name]` (e.g. `/files/high5.gif`) will show the image ### Part 2 1. Add a `/files` path. The files path will list out all the files in the `src/main/resources` folder in a list. 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. ## Bonus - ORM 1. Add ORMlite with a mysql database to your dependencies 2. Add a User class with two fields `long id` and `String name` 3. Add the dao necessary to create retrieve the user 1. Create 3 users 4. Add a `/users` path to display all the users name in a list. 5. Add a `/users/1` to show information for the first user (their name and id) #### Super bonus 1. Add a `/users/new` path to show a form where user can enter a name to create a new user. 2. Add a `/users` with a `POST` method to accept the form information and create the user. Redirect to `/users/[user_id]`