Web Server
The first assignment is designed to familiarise you with Java networking API. This assignment, however, is rather high-level compared to the following ones. Your task is to develop a small web server with Java that is capable of serving static content. By carrying out this assignment, you will learn how to use server sockets with HTTP protocol; later you will learn means to work with underlying protocols (on both server and client side).
Reference material
- HTTP overview (http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol)
- Java networking tutorial (http://docs.oracle.com/javase/tutorial/networking/index.html)
- Official Java documentation (http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html)
- PuTTY client (http://www.chiark.greenend.org.uk/~sgtatham/putty/)
Note that if you use OS X or Linux, you can use the built-in telnet client.
Problem 1
This is the main part of assignment: implement a simple web server using ServerSocket class that is able to accept connections on some predefined port (e.g., 8888) and serve HTML pages and images (PNG support would be enough) from a local directory for corresponding GET requests.
Example: if some client requests a file “dir1/subdir1/helloworld.htm” and such file exists (with respect to the server’s working directory), server should return the contents of that file in response (as well as corresponding headers, etc.)
To start with this task, implement a program that would accept connections and return some string, then change this string into a valid HTTP response, then implement HTML file reading / transfer support, then add support for image files.
Don’t forget about handling invalid requests or requests for non-existent files. If no file is specified by request and there is a file called “index.htm” or “index.html” in corresponding directory, its contents should be returned in response.
Your server program is supposed to work in a loop; it is OK to terminate it with combination like Ctrl-D (or Ctrl-C) in terminal emulator.
Problem 2
Prepare a few (2-3) HTML files (small ones are recommended) and a few (2-3) PNG images and organize them in a hierarchy of directories (e.g., “dir1″, “dir1/subdir1″ and “dir2/subdir2″ with some files).
Try to access your running server program from web browser (e.g., if you specified port 8888, try “http://localhost:8888″ in browser address bar). Test the behaviour of your program with requests of existent and non-existent files, HTML and image files, requests with no file names specified. Include corresponding web browser screenshots in your report!
Problem 3
Instead of using web browser, repeat your requests with a telnet client (you may want to use PuTTY if you use Windows). You will have to type some HTTP related commands by hand this time. Include screenshots of your terminal emulator (or paste the telnet session as text) in your report!
Submission
The deadline for submitting your assignment is February 7, 2013, 23:59.
Submission must be packed in a single ZIP archive and it must include:
- all of your source code files in non-default Java packages (e.g., your classes should belong to package 1dv201.my_package_name); your source code files are expected to have sufficient amount of comments
- report with answers for problems, specified example outputs / screenshots, short comments on implementation, etc. as a PDF or HTML file
Submission archive must be sent to Kostiantyn ( kk222bt@student.lnu.se ); please include 1DV201 Assignment 1 in your email topic and include your name and student ID (e.g., “kk222bt”) in your email body.
Pay attention: the first assignment of this course must be carried out individually!