Image credit: https://ruslanspivak.com/lsbaws-part1/LSBAWS_HTTP_request_response.png

Java HTTP server

creating a simple HTTP server in java

Sina Ahmadi
3 min readMay 15, 2018

--

After a while from my last post, I’m writing about a new project that I have been doing in the last few days. It’s an HTTP server in Java from scratch.

What is HTTP?

HTTP or Hypertext Transfer Protocol is a protocol for distributed systems to communicate and transfer data. It was developed by CERN as a means to transfer data back in the 90s. This concept which is the basis for World Wide Web has persisted and evolved throughout the years. Many applications now offer APIs (Application Programming Interface) that use simple HTTP protocols under the hood to provide a service. Therefore, it’s important for all devs and junior devops engineers to be familiar with this protocol and how it works.

HTTP Server in Java

Java already has an internal HTTP server library which could be easily used to create a working HTTP server in minutes. However, I have used raw sockets for my server’s implementation. I have also written my own classes and functions to create and interpret HTTP requests and responses, despite the fact that Java has all sorts of internal libraries to use for this reason. But this is intentional in order to better understand the underlying protocol.

How It Works

Here’s how my server works in summary:

  • Listening for socket connections from clients
  • Creating a new thread for a new connection
  • Reading client’s request
  • Processing the request read from client
  • Constructing a response based on the request
  • Writing the response back to the client
  • Closing the connection

A few simple lines of code can get you started in Java:

public class Server {
private final port = 8080; //sample listening port for server
public static void main(String[] args) {
ServerSocket serverSocket = new ServerSocket(port);
while (isRunning) {
Socket clientSocket = serverSocket.accept();
/** Handing out client's socket to a
new thread to process the request. **/
}
}
}

The Good Thing About Using Sockets

Clearly, it would have been much easier to use Java libraries or even a framework like Spring to do this project, but I chose to do it the hard way. Although working with sockets requires dealing with all kinds of resource and IO problems that will occur at some point, debugging these issues will give you insights on how everything works.
There is also the obvious gaining experience part from these exercises. A pile of stack trace messages thrown at your later by a framework could be overwhelming if you don’t know the possible underlying cause. But hopefully, you will have a better idea about the cause of issues once you get through these fundamental exercises. You will know how to write good programs and how to debug them quicker as you go along. A silver lining that keeps thing interesting for me and I find this a really valuable experience.

--

--

Sina Ahmadi
My journey as a software developer

Bachelor of Physics, Master of IT, Melb Uni graduate, Tech enthusiast, Amateur astronomer