Advertisement
jadenkore

webserver

Jan 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. package webserverapp;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.util.Date;
  11.  
  12. public class WebServerApp {
  13.     public static void main(String[] args) {
  14.         //The port, at which, the server is listening for requests
  15.         int port = 80;
  16.         try { //ServerSocket object is used to listen for requests
  17.             ServerSocket ss = new ServerSocket(port);            
  18.             System.out.println("Server is ready to receive command!");
  19.             while(true){ //accepting connection requests      
  20.                 Socket socket = ss.accept(); //get the input stream to read data  
  21.                 InputStream is = socket.getInputStream(); //Read data as character  
  22.                 InputStreamReader isr = new InputStreamReader(is); //Read data as lines  
  23.                 BufferedReader br = new BufferedReader(isr); //Read the string command from the user
  24.                 String command = br.readLine();          
  25.                 String response = command;
  26.                 if(command.equalsIgnoreCase("GET / HTTP/1.1")) {
  27.                     response = "<HTML> <TABLE border=\"1\"summary=\"This table gives some statistics about fruit                    flies: average height and weight, and percentage                    with red eyes (for both males and females).\"> <CAPTION><EM>A test table with merged cells</EM></CAPTION> <TR><TH rowspan=\"2\"><TH colspan=\"2\">Average     <TH rowspan=\"2\">Red<BR>eyes <TR><TH>height<TH>weight <TR><TH>Males<TD>1.9<TD>0.003<TD>40% <TR><TH>Females<TD>1.7<TD>0.002<TD>43% </TABLE> </HTML>";            
  28.                 }
  29.                 else if(command.equalsIgnoreCase("GET /anotherpage.html HTTP/1.1")) {
  30.                     response = "<HTML><h1>This is another page</h1></HTML>";
  31.                 }
  32.                 else {
  33.                     response = "<HTML><h1>404 - Page not found</h1><h2><a href=\"/\">Homepage</a></h2></HTML>";
  34.                 }
  35.                 //String response = "<HTML> <TABLE border=\"1\"summary=\"This table gives some statistics about fruit                    flies: average height and weight, and percentage                    with red eyes (for both males and females).\"> <CAPTION><EM>A test table with merged cells</EM></CAPTION> <TR><TH rowspan=\"2\"><TH colspan=\"2\">Average     <TH rowspan=\"2\">Red<BR>eyes <TR><TH>height<TH>weight <TR><TH>Males<TD>1.9<TD>0.003<TD>40% <TR><TH>Females<TD>1.7<TD>0.002<TD>43% </TABLE> </HTML>";
  36.                
  37.                 //Access to the output stream, to write data back      
  38.                 OutputStream os = socket.getOutputStream(); //Write lines                
  39.                 PrintWriter pw = new PrintWriter(os);                
  40.                 pw.println(response);                
  41.                 pw.flush();                 pw.close();      
  42.                 socket.close();        
  43.             }        
  44.         }
  45.         catch (IOException ex) {      
  46.             ex.printStackTrace();        
  47.         }    
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement