| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| tanszek:oktatas:iss_t:http_server [2026/03/09 09:56] – knehez | tanszek:oktatas:iss_t:http_server [2026/03/09 09:57] (current) – knehez |
|---|
| |
| Let's create an empty Java project, Then create files called //404.html// and an //index.html// with the following content. Let's note that the error handling is redundant, i.e. the handling of //errors 404// and //501// is written twice. | Let's create an empty Java project, Then create files called //404.html// and an //index.html// with the following content. Let's note that the error handling is redundant, i.e. the handling of //errors 404// and //501// is written twice. |
| |
| **Task**: create a generic error handling function to return errors to the client side. | |
| |
| Content of //404.html//: | Content of //404.html//: |
| out.println("Content-type: " + content); | out.println("Content-type: " + content); |
| out.println("Content-length: " + fileLength); | out.println("Content-length: " + fileLength); |
| out.println(); // blank line between headers and content, very important ! | out.println(); // blank line between headers and content, very important |
| out.flush(); // flush character output stream buffer | out.flush(); // flush character output stream buffer |
| |
| out.println("Content-type: " + content); | out.println("Content-type: " + content); |
| out.println("Content-length: " + fileLength); | out.println("Content-length: " + fileLength); |
| out.println(); // blank line between headers and content, very important ! | out.println(); // blank line between headers and content, very important |
| out.flush(); // flush character output stream buffer | out.flush(); // flush character output stream buffer |
| |
| } | } |
| </sxh> | </sxh> |
| | **Task 1.**: create a generic error handling function to return errors to the client side. |
| |
| **Task 1.**: refactor the code and eliminate duplicate parts. Let's create a separate function to return a general HTTP response:: | **Task 2.**: refactor the code and eliminate duplicate parts. Let's create a separate function to return a general HTTP response: |
| |
| <sxh java> | <sxh java> |
| out.println("Content-type: " + content); | out.println("Content-type: " + content); |
| out.println("Content-length: " + fileLength); | out.println("Content-length: " + fileLength); |
| out.println(); // blank line between headers and content, very important ! | out.println(); // blank line between headers and content, very important |
| out.flush(); // flush character output stream buffer | out.flush(); // flush character output stream buffer |
| </sxh> | </sxh> |
| |
| **Task 2.**: modify the source code so that it can also return images. To do this, first add <img src="…> to index.html and copy an arbitrary image next to the html-s.. | **Task 3.**: modify the source code so that it can also return images. To do this, first add <img src="…> to index.html and copy an arbitrary image next to the html-s. |
| |