Create a simple HTTP Web Server in Java

Create a simple HTTP Web Server in Java

Java is one of the most used programming language in the World. The JDK comes with a lot of exciting features. In that video, we are going to use the ServerSocket and the Socket classes to create a simple HTTP Web Server in Java.

Image for post

In the first part of this tutorial, we create the constants for the Web Server (index file, port, 404 file not found page, ?) and the Thread which will be used to manage each client connection separately.

In the second part of this tutorial, we read the data sent by the client and we write the requested file on the output stream. Finally, we try our HTTP Web Server by making some requests by using Safari. Don?t hesitate if you have any question about this tutorial.

How our Java HTTP Server works ?

Each client connection is managed in a dedicated thread. So, our JavaHTTPServer class implements the Runnable interface. In the constants, we define the Web Root, the default file, the file used when a file will be not found, the file called for an unsupported HTTP method and also the port to listen connections.

Image for post

The client connections will be managed via the Socket object available in standard in the JDK. In the main method, we start by creating a ServerSocket object also available in standard in the JDK.

We listen on the port 8080 until user halts server execution. We call the accept method of the ServerSocket instance to wait for a connection. When a connection is created, we pass the Socket object in parameter of our JavaHTTPServer class and then we start a dedicated Thread object.

For each Thread instance created, we read characters from the client via the getInputStream method of the Socket instance got. Then, we get one character output stream to client for headers. And we get a binary output stream to client for requested data.

We support only GET and HEAD HTTP methods. Otherwise, we return the file pointed by the unsupported variable. If it?s a GET or HEAD HTTP method, we try to find the requested data. If it exists, we return the requested data to the user. When we return the requested data, we send also the HTTP Headers. Don?t forget to return blank line between headers and content. It?s very important !

Complete source code of the Java HTTP Web Server

The complete source code of this simple HTTP Web Server made in Java can be found just below :

Note that the code is particularly well commented, so you should understand how our HTTP Server works easily. If you have some questions, don?t hesitate to use the comments below.

To discover more tutorials, don?t hesitate to subscribe to the SSaurel?s Channel on YouTube :

Sylvain Saurel

This channel will offer you tutorials on Android, Web Development and some others programming tips. Some tutorials and?

www.youtube.com

If you want to discover some books to learn Java programming, I advice you to read the following article with my selection of the Top 6 Best Books for Java programming :

Top 6 Best Books for learning Java Programming

Twenty-five years after its creation, Java is still the most popular programming language according to the latest TIOBE?

medium.com

15

No Responses

Write a response