Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package webserverapp;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Date;
- public class WebServerApp {
- public static void main(String[] args) {
- //The port, at which, the server is listening for requests
- int port = 80;
- try { //ServerSocket object is used to listen for requests
- ServerSocket ss = new ServerSocket(port);
- System.out.println("Server is ready to receive command!");
- while(true){ //accepting connection requests
- Socket socket = ss.accept(); //get the input stream to read data
- InputStream is = socket.getInputStream(); //Read data as character
- InputStreamReader isr = new InputStreamReader(is); //Read data as lines
- BufferedReader br = new BufferedReader(isr); //Read the string command from the user
- String command = br.readLine();
- String response = command;
- if(command.equalsIgnoreCase("GET / HTTP/1.1")) {
- 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>";
- }
- else if(command.equalsIgnoreCase("GET /anotherpage.html HTTP/1.1")) {
- response = "<HTML><h1>This is another page</h1></HTML>";
- }
- else {
- response = "<HTML><h1>404 - Page not found</h1><h2><a href=\"/\">Homepage</a></h2></HTML>";
- }
- //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>";
- //Access to the output stream, to write data back
- OutputStream os = socket.getOutputStream(); //Write lines
- PrintWriter pw = new PrintWriter(os);
- pw.println(response);
- pw.flush(); pw.close();
- socket.close();
- }
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement