Servlet and Client Server Communication


Servlet is used to create dynamic web applications. These applications reside on server and can be accessed by clients over the internet.

Client Server Communication -



The communication between client and server takes place using HTTP protocol. Whenever client enters a URL in the web browser it generates a HTTP Request. HTTP request contain the address of the server and web page requested by client.

Whenever request comes to the server, it processes the request and generates HTTP Response. HTTP Response contains the web page requested by client. The detailed description of each entities involved in this model is given below -

Client (Web Browser) -

A client is a general user with web browser that wants to access any website over the internet.

Server -

A server is a computer that serves (Provides services) other computers (Clients). In this case server contains the web application and whenever request came from any client, it serves the client according to request.
For Example - Apache Tomcat from Apache Software Foundation, GlassFish from Oracle.

HTTP Protocol -

Hyper text transfer protocol is used for communication between client and server over the internet. It is a stateless protocol i.e. whenever client generate a HTTP Request it sends the request to server and forget about the client and similarly whenever server generate HTTP Response, It sends it to client and forgets about server. Java uses the concept of sessions and Jsession id to control this behaviour of HTTP.

HTTP Request -

Whenever client enters a URL in the web browser it generates a HTTP Request. HTTP Request has 3 main components -
1- HTTP Method.
2- The server and web page address i.e. URL.
3- User inputs .

HTTP Response -

Whenever request comes to the server, it processes the request and generates HTTP Response. The 3 main components of HTTP Response are as follows -
1- A status code (whether request was successful or not).
2- Response content type (text, pictures, videos, html etc).
3- Response contents (Actual HTML).

HTTP Methods -

HTTP has total 8 methods -
1- GET
2- POST
3- PUT
4- HEAD
5- TRACE
6- DELETE
7- CONNECT
8- OPTIONS
The 2 most commonly used methods of HTTP are GET and POST.
GET Method - The main job of GET Method is to get or retrieve data from server. This is the most commonly used method and default method for java if you do not specified one.
Client can also send a limited amount of data with GET, but this amount of data depends upon server. The data sent using GET is appended in the URL in name/value pair like this -
https://docs.oracle.com/javaee.htm?name1= value1&name2=value2


POST Method -

The POST method as the name indicates is used to post or send data to server. For example sending the Form data like username, password etc to sever. The data send with POST is stored within POST request.