Servlet: Introduction to Web
Web consists of billions of clients and server connected through wires and wireless networks. The web clients make requests to web server. The web server receives the request, finds the resources and return the response to the client. When a server answers a request, it usually sends some type of content to the client. The client uses web browser to send request to the server. The server often sends response to the browser with a set of instructions written in HTML(HyperText Markup Language). All browsers know how to display HTML page to the client.
Web Application
A website is a collection of static files(webpages) such as HTML pages, images, graphics etc. A Web application is a web site with dynamic functionality on the server. Google, Facebook, Twitter are examples of web applications.
HTTP (Hypertext Transfer Protocol)
- HTTP is a protocol that clients and servers use on the web to communicate.
- It is similar to other internet protocols such as SMTP(Simple Mail Transfer Protocol) and FTP(File Transfer Protocol) but there is one fundamental difference.
- HTTP is a stateless protocol i.e HTTP supports only one request per connection. This means that with HTTP the clients connect to the server to send one request and then disconnects. This mechanism allows more users to connect to a given server over a period of time.
- The client sends an HTTP request and the server answers with an HTML page to the client, using HTTP.
HTTP Methods
HTTP request can be made using a variety of methods, but the ones you will use most often are Get and Post. The method name tells the server the kind of request that is being made, and how the rest of the message will be formated.
HTTP Methods and Descriptions :
Method Name | Description |
OPTIONS | Request for communication options that are available on the request/response chain. |
GET | Request to retrieve information from server using a given URI. |
HEAD | Identical to GET except that it does not return a message-body, only the headers and status line. |
POST | Request for server to accept the entity enclosed in the body of HTTP method. |
DELETE | Request for the Server to delete the resource. |
CONNECT | Reserved for use with a proxy that can switch to being a tunnel. |
PUT | This is same as POST, but POST is used to create, PUT can be used to create as well as update. It replaces all current representations of the target resource with the uploaded content. |
Difference between GET and POST requests
GET Request | POST Request |
Data is sent in header to the server | Data is sent in the request body |
Get request can send only limited amount of data | Large amount of data can be sent. |
Get request is not secured because data is exposed in URL | Post request is secured because data is not exposed in URL. |
Get request can be bookmarked and is more efficient. | Post request cannot be bookmarked. |
General Difference between PUT and POST methods
Following are some basic differences between the PUT and the POST methods :
- POST to a URL creates a child resource at a server defined URL while PUT to a URL creates/replaces the resource in its entirety at the client defined URL.
- POST creates a child resource, so POST to
/books
will create a resources that will live under the /books
resource. Eg. /books/1
. Sending the same post request twice will create two resources.
- PUT is for creating or replacing a resource at a URL known by the client.
- PUT must be used for CREATE when the client already knows the url before the resource is created.
- PUT replaces the resource at the known url if it already exists, so sending the same request twice has no effect. In other words, calls to PUT are idempotent.
Anatomy of an HTTP GET request
Get request contains path to server and the parameters added to it.
Anatomy of an HTTP POST request
Post requests are used to make more complex requests on the server. For instance, if a user has filled a form with multiple fields and the application wants to save all the form data to the database. Then the form data will be sent to the server in POST request body, which is also known as Message body.