The program below is written in Java and is used to develop a Web Server that supports these status codes: 200 OK, 302 Moved Temporarily, and 404 NOT FOUND. The code can emulate an HTTP server in your local machine. Let me know if you find the code below helpful and if you have any additional suggestions:
HTTP Web Server: Java
/*
* An HTTP Web Server that supports these status codes: 200 OK, 302 Moved Temporarily and 404 NOT FOUND.
* The code can be tested using any Web Browser.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author jnbgames.dev
*/
public class HTTPWebServer {
private static ServerSocket serverSocket;
public static void main(String[] args) throws IOException {
// give some values
String port = "7788"; // if not specified
String redirection = "https://www.daniweb.com/"; // if not specified
String redirector = "test.txt"; //if not specified
// Open the Server Socket and bind it to admin specified port
serverSocket = new ServerSocket(Integer.valueOf(port));
while (true) {
System.out.println("HTTP Web Server is waiting...");
// Open a new Thread and pass in Socket && REDIRECTIONS info
new HttpServerThread(serverSocket.accept(), redirection, redirector);
}
}
}
class HttpServerThread extends Thread {
private Socket connectionFromBrowser;
private BufferedReader in;
private PrintWriter out;
private String redirection;
private String redirector;
public HttpServerThread(Socket connectionFromClient, String redirection, String redirector) throws IOException {
this.connectionFromBrowser = connectionFromClient;
this.in = new BufferedReader(new InputStreamReader(connectionFromBrowser.getInputStream()));
this.out = new PrintWriter(connectionFromBrowser.getOutputStream());
this.redirection = redirection;
this.redirector = redirector;
start();
}
public void run() {
try {
System.out.println("Connection established in " + connectionFromBrowser.getInetAddress().getHostName());
// read HTTP request line
String request = in.readLine();
// Get filename in the request
if (request.compareToIgnoreCase("GET / HTTP/1.1") == 0 || request.compareToIgnoreCase("GET / HTTP/1.0") == 0) {
http200();
} else if (request.regionMatches(5, redirector, 0, redirector.length())) {
http302(redirection);
} else {
http404();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// method for 404
public void http404() throws IOException {
// Create HTTP Response Header
String header404 = "HTTP/1.1 404 Not Found\r\n"
+ "Content-type: text/html\r\n\r\n";
// Create message body
String html404 = "<html>\n"
+ "<body>\n"
+ " <hgroup>\n"
+ " <center>\n"
+ " <h1>404</h1>\n"
+ " \n"
+ " <h1>Page Not Found</h1>\n"
+ " </center> \n"
+ " </hgroup>\n"
+ "</body>\n"
+ "</html>";
// send HTTP 404 Response
out.println(header404 + html404);
out.flush();
connectionFromBrowser.close();
}
// method of 302
public void http302(String red) throws IOException {
// Create HTTP Response Header
String header302 = "HTTP/1.1 302 Moved Temporarily\r\n"
+ "Location: " + red + "\r\n\r\n";
// send HTTP 302
out.println(header302);
out.flush();
connectionFromBrowser.close();
}
// method 200
public void http200() throws IOException {
// Create HTTP Response Header
String header200 = "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/html\r\n"
+ "Server: Simo\r\n\r\n";
// Create simple HTML message to display
String html = "<html>\n"
+ "<body>\n"
+ " <hgroup>\n"
+ " <center>\n"
+ " <h1> Welcome to Server 01</h1>\n"
+ " </center> \n"
+ " </hgroup>\n"
+ "</body>\n"
+ "</html>";
// Send HTTP 200 OK Response & HTML CONTENT
out.println(header200 + html);
out.flush();
connectionFromBrowser.close();
}
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
jnbgames.dev commented: I agree with you! I will try text blocks, they will be much helpful in this case! +0
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
rproffitt 2,662 "Nothing to see here." Moderator
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
rproffitt 2,662 "Nothing to see here." Moderator
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
rproffitt 2,662 "Nothing to see here." Moderator
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.