5

So after establishing my listening for a connection and accepting one:

ServerSocket serverSock = new ServerSocket(6789);
Socket sock = serverSock.accept();

When I type into my browser localhost:6789/index.html how do I handle this incoming GET request and return index.html? index.html is in the same directory.

Firstly I want to very that index.html actually exists and if not I return a HTTP 404 message. Then I will close the connection.

2
  • 2
    Are you writing this as a learning exercise? There are many ready-made Java-based web servers which you can use to handle HTTP requests. Commented Sep 12, 2012 at 10:51
  • @codebox No, I'm fairly new to java. Can you show me an example of something that fits my needs? Commented Sep 12, 2012 at 10:59

2 Answers 2

12

Handling GET and other requests is actually very simple but you must know the specification of the HTTP protocol.

The first thing to do is to get the SocketInputStream of the client and the path of the file to return. The first line of the HTTP request comes in this form: GET /index.html HTTP/1.1. Here is a code example that does that:

SocketInputStream sis = sock.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(sis));
String request = br.readLine(); // Now you get GET index.html HTTP/1.1
String[] requestParam = request.split(" ");
String path = requestParam[1];

You create a new File object and checks if that file exists. If the file does not exist, you return a 404 response to the client. Otherwise you read the file and send its content back to the client:

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
File file = new File(path);
if( !file.exist()){
  out.write("HTTP 404") // the file does not exists  
}
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
String line;
while((line = bfr.readLine()) != null){
  out.write(line);
}

bfr.close();
br.close();
out.close();    

Here is the complete code summary:

ServerSocket serverSock = new ServerSocket(6789);
Socket sock = serverSock.accept();

InputStream sis = sock.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(sis));
String request = br.readLine(); // Now you get GET index.html HTTP/1.1`
String[] requestParam = request.split(" ");
String path = requestParam[1];

PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
File file = new File(path);
if (!file.exists()) {
     out.write("HTTP 404"); // the file does not exists
}
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
String line;
while ((line = bfr.readLine()) != null) {
    out.write(line);
}

bfr.close();
br.close();
out.close();
Sign up to request clarification or add additional context in comments.

2 Comments

Cheers Dimitri, can you explain why is "index.html" served only when I type localhost:6789/index.html and not localhost:6789/blahblah? Which line checks this? EDIT: Sorry looks like my internet connection died while I was editing this comment.
It's because blahblah is not hosted in your server. See my complete edited answer
2

If you just want a Java-based web server which handles HTTP requests for you then you should look at Tomcat, which will look after things like returning static files automatically, and will also allow you to define Java code to provide custom responses to specific requests.

You should read some kind of Tomcat Quick Start guide, and get a basic understanding of Java Servlets and JSPs.

An alternative, which can be easier be set up and configure is Jetty, so you might want to have a look at that one as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.