User Tools

Site Tools


tanszek:oktatas:iss_t:http_server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tanszek:oktatas:iss_t:http_server [2023/03/12 17:45] – létrehozva kneheztanszek:oktatas:iss_t:http_server [2026/03/09 09:57] (current) knehez
Line 1: Line 1:
-==== Saját HTTP szerver mintapélda ====+==== 'My own HTTP' server sample ====
  
 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//:
  
-<code html>+<sxh html>
     <html>     <html>
         <body>         <body>
Line 13: Line 11:
         </body>         </body>
     </html>     </html>
-</code>+</sxh>
  
 Content of //index.html//: Content of //index.html//:
-<code html>+<sxh html>
     <html>     <html>
     <body>     <body>
Line 23: Line 21:
     </body>     </body>
     </html>     </html>
-</code>+</sxh>
  
-JavaHTTPServer.java tartalma:+Content of //JavaHTTPServer.java//:
  
-<code java>+<sxh java>
 import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
 import java.io.BufferedReader; import java.io.BufferedReader;
Line 110: Line 108:
  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
  
Line 174: Line 172:
  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
  
Line 182: Line 180:
  
 } }
-</code>+</sxh> 
 +**Task 1.**: create a generic error handling function to return errors to the client side.
  
-**Feladat**: refaktoráljuk a kódot és szüntessük meg a többszörözött részeketKészítsünk egy külön függvényt a HTTP válasz általános visszadásához:+**Task 2.**: refactor the code and eliminate duplicate partsLet's create a separate function to return general HTTP response:
  
-<code java>+<sxh java>
 out.println("HTTP/1.1 200 OK"); out.println("HTTP/1.1 200 OK");
 out.println("Server: Java HTTP Server v1.0"); out.println("Server: Java HTTP Server v1.0");
Line 192: Line 191:
 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
-</code>+</sxh>
  
-**Feladat**: módosítsuk a forráskódothogy képeket is vissza tudjon adni. Ehhez először <img src="...kiegészítést adjunk hozzá a index.html-nek és másoljunk egy tetszőleges képet a html-ek mellé.+**Task 3.**: modify the source code so that it can also return images. To do thisfirst add <img src="to index.html and copy an arbitrary image next to the html-s.
  
tanszek/oktatas/iss_t/http_server.1678643133.txt.gz · Last modified: 2023/03/12 17:45 by knehez