Scaffold your ideas into application
Get your free alpha access now.
Only 445 left, Hurry!
18 Jul 2016

j2ee tutorial for beginners ( What is a server?)

Before we get started with J2EE aka developing web application, we need to understand a few basic things about web. The first and foremost is the question, What is a server? When we say server we expect this.

Sure they are high performance hardware stacked together running servers but they are not really servers on their own. A server is merely any piece of code which responds to network request, it could be as simple as ping server or as complex as database server.

The first servers were simply programs which served information over network either in text format or binary. When there was a need for sharing documents, then HTTP and HTML protocols were developed. The early HTTP/web servers simply read a file and served it as it is, it was far off from web servers which we know.

We would begin our journey by creating a simple server which serves the famous hello world over network. Since this is a J2EE tutorial, we would be writing it in Java.

package com.scaffoldthis; 
import java.net.ServerSocket;
import java.net.Socket; 
public class MySuperServer { 
    public static void main(String[] args) throws Exception 
    { 
        ServerSocket socket = new ServerSocket(5050);
        while (true) { 
            Socket client = socket.accept();
            client.getOutputStream().write("Hello World".getBytes());
            client.close();
        } 
     } 
}

This server would say hello world to every client who connects to port 5050 and then close the connection. That is so rude, right? however if we do not close the connection then program would simply wait endlessly for the client to communicate and would not be able to handle any other request.

Since we want our server to be able to handle multiple request, we should ideally process each request as early as possible and then move on to next client. That is easier said than done, just imagine if between accept and close, we need to go to database and fetch a few rows and process them to generate some data, it may block the request for long.

How about if we do it in parallel? If we could spawn multiple threads we could handle more request easily. Lets modify our design to a multi threaded server.

import java.io.IOException;
import java.net.Socket;

public class ServiceThread implements Runnable {
    Socket client;

    public ServiceThread(Socket conn) {
        this.client = conn;
    }

    public void run() {
        try {
            this.client.getOutputStream().write("Hello World".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.scaffoldthis;

import java.net.ServerSocket;
import java.net.Socket;

public class MySuperServer {
    public static void main(String[] args) throws Exception {
        ServerSocket socket = new ServerSocket(5050);
        while (true) {
            Socket client = socket.accept();
            new Thread(new ServiceThread(client)).run();
            client.close();
        }
    }
}

Voila! We have a highly scalable server which can service infinite request. Unfortunately this design suffers from a major issue, which is that it has to spawn a new thread per request and since each thread need memory, at some point the server runs out of memory. By increasing the RAM, you can keep servicing more and more request and depending on what you are building, it may be fine, but for some of the application, this design would not work.

Some of the example are chat server, which need multiple live connection but not every one is sending message at the same time or gaming server where we again need multiple live connection to handle the game movement but not every character is moving all the time.

This is known as C10K problem and some of the servers like NGinx and LightHTTPD solve this issue by Non Blocking I/O, which may not suite the requirement of every application.

Depending on your application needs, you would need to choose between traditional server architecture or Non Blocking architecture.


Stats:
170 views
Scaffold your ideas into application
Get your free alpha access now.
Only 445 left, Hurry!

j2ee tutorial for beginners (Servlets)

Servlets are main workhorse for J2EE web application.

Part 3: j2ee tutorial for beginners (JSP)

In this part we explore basics of View technology of J2EE

Part 4: j2ee tutorial for beginners (JDBC)

JDBC is the foundation of persistence in Java. This tutorial explore

Comments:

Leave your comments