Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Java-Socket-Http-Server

A socket http server implemented in Java with url mapping feature , which helps rapid development of web project.

The api package contains the files required for implementing a project. And the "Runner.java" in the default package is one such implementation.

Below is the code in the main method of "Runner.java" p1

Blue box

Here first we need to create url mappings. So a "GET" request with "/" url will be served by the "index.html" page from the "html" directory.

Red box

This is another way of doing the above step where the abstract class "AbstractResponse"'s getResponse is implemented. It has dynamic behaviour as it has the handle of the "Request" object.

Green box

This is the loop that serves the incoming requests. "server.accept()" gets the request "server.sendResponse()" send the response from the mapping based on the url pattern.

Below picture shows the output at "http://localhost:8888" in the browser: p2

Below picture shows the classes (and some important portion from each) inside the api package: p2

AbstractResponse.java

This is an abstract class to provide dynamic responses through the Mappings.

Mapping.java

This class has two constructors to set url mappings. Already demonstrated above in the first image in the blue and red box.

Request.java

The parse() method in this class extracts out all the information and attribute out of request , which helps in creating dynamic web pages.

Response.java

The constructor for this takes a plain String as input and attaches the http header to it thus making it browser readable.

Server.java

This class is baically the first point to use any of the above classes.