Implementing a simple HTTP Server in java using sockets is an useful application, which I thought would be worth sharing here. When I was implementing my own search engine, I always needed a light weight socket HTTP Server which I can bundle with my package and every time you need a HTTP/Web server, you need not to go for Apache Web Server which would be an overkill for simple applications which serves only static content, say a Google desktop search application where you just need a HTML user interface served through a simple HTTP Server.
In this HTTP Server we will see
1. How to process HTTP requests
2. How to handle HTTP GET methods
3. How to create multiple threads of control to handle each HTTP client
The below HTTP Server prints the client HTTP request in the home page, serves any valid files requested through GET method, otherwise prints a HTTP 404 page not found error message, lets look at the code and a simple explanation.
In this HTTP Server we will see
1. How to process HTTP requests
2. How to handle HTTP GET methods
3. How to create multiple threads of control to handle each HTTP client
The below HTTP Server prints the client HTTP request in the home page, serves any valid files requested through GET method, otherwise prints a HTTP 404 page not found error message, lets look at the code and a simple explanation.
/* | |
1 |
Explanation:
The code is pretty simple, in main (lines 133 - 143), we accept new connections and invoke a thread on the newly created HTTPServer instance which will process the request.
In Line 53, while (inFromClient.ready()) loops till all HTTP request parameters are recieved, this means reading all lines in the input HTTP request are read, the ready function of BufferedReader tells whether the stream is ready to be read or not, therefore we process the HTTP request from the client till the stream is ready thereby ensuring that we get the complete request available for processing, including all request headers.
In Line 67, we use fileName = URLDecoder.decode(fileName) to decode the request GET method URL, most HTTP coding errors happen because the input HTTP request is not URLDecoded as a result we won't get the correct parameters to process, therefore one needs to URLDecode the input request string.
Other things are straightforward, we print the HTTP GET Request headers in the homepage, else the requested file is served, else a 404 Page not found error is output if the input GET request is not a valid file in the file system.
In Line 53, while (inFromClient.ready()) loops till all HTTP request parameters are recieved, this means reading all lines in the input HTTP request are read, the ready function of BufferedReader tells whether the stream is ready to be read or not, therefore we process the HTTP request from the client till the stream is ready thereby ensuring that we get the complete request available for processing, including all request headers.
In Line 67, we use fileName = URLDecoder.decode(fileName) to decode the request GET method URL, most HTTP coding errors happen because the input HTTP request is not URLDecoded as a result we won't get the correct parameters to process, therefore one needs to URLDecode the input request string.
Other things are straightforward, we print the HTTP GET Request headers in the homepage, else the requested file is served, else a 404 Page not found error is output if the input GET request is not a valid file in the file system.
Output:
1. http://127.0.0.1:5000 should print the home page with the HTTP request parameters
2. http://127.0.0.1:5000/myHTTPServer.java would download the java source file, provided you run the java class file in the same location or try any other file in the filesystem by giving the absolute path, that would be downloaded.
3. http://127.0.0.1:5000/invalidfile would result in HTTP 404 file not found error.
We will see how to handle HTTP POST methods in future with an example of a file upload HTTP server.
1. http://127.0.0.1:5000 should print the home page with the HTTP request parameters
2. http://127.0.0.1:5000/myHTTPServer.java would download the java source file, provided you run the java class file in the same location or try any other file in the filesystem by giving the absolute path, that would be downloaded.
3. http://127.0.0.1:5000/invalidfile would result in HTTP 404 file not found error.
We will see how to handle HTTP POST methods in future with an example of a file upload HTTP server.




10 comments:
Nice work! Although a much simpler way of doing it is to leverage the Jetty APIs to implement all this functionality for free. An example of this if given here: http://gluga.com/programming/embedding-jetty-web-server/
This will significantly reduce your coding efforts and give you much greater flexibility for extending the functionality of your application in future maintenance.
All depends on your needs though. The above solution is a nice simple stand-alone implementation.
Hi,
Thanks for your comments and suggestions,I looked at Jetty APIs, looks cool, the example code illustrated there was also very effective.
This code I implemented to have some utility HTTP Class for one one of my project, I have also implemented a stand alone file upload server to handle POST requests (http://www.prasannatech.net/2008/11/http-web-server-java-post-file-upload.html), in future I will look in to the Jetty API for these kind of things.
To get the maximum out of this tutorial it is recommended that you have a basic grasp on how HTTP works. If the little man inside your head says what tha H is HTTP, you should go to the bottom and check some of the pointers. There you will find some nice text to explain the basic concept.
Hello sir!
Im a newbie to Java, how does the client side program contact with this server? I mean let's say i have a form and i need to send that info to the server and get a page back front he server. where do I store the response page?in my system?
It's a good example of programming! I understand as an HTTP server creates the response...
bye!
Just wanted to say thanks. Is is blogs like these that just simplify things and spread some light on those dark programming days :)
Thank you so much
Dear Sir,
Thanks a lot for finding time to educate others...
I am a networking trainer, but curious to know how this work..
The program starts from public static void main() and create an instance of ServerSocket class (I remember ServerSocket class belongs to java.net package). Then it create an infinite loop.
Could you please explain what is Socket class? Is is from java.io package?
After that an instance of myHTTPServer is created and the constructor is called. From there I am not able to follow.
Request you to kindly explain.
How can we implement an HTTP client? Any built-in classes available? This is just to create a program to download files using HTTP.
Thanks a lot
i want to use it with webservice
a cilent is calling some webservice and wenservice need to start this server
I am getting socket write error
Very Good !!
Post a Comment