0% found this document useful (0 votes)
12 views11 pages

Understanding HTTP Requests and Methods

The document provides an overview of HTTP requests, detailing the structure and various methods such as GET, POST, PUT, PATCH, and DELETE, which correspond to CRUD operations. It also discusses session tracking techniques in servlets, including cookies, hidden form fields, URL rewriting, and the HttpSession interface, emphasizing their advantages and disadvantages. Additionally, it explains how cookies work, their types, and the functionality of the Cookie class in Java servlets.

Uploaded by

vrawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Understanding HTTP Requests and Methods

The document provides an overview of HTTP requests, detailing the structure and various methods such as GET, POST, PUT, PATCH, and DELETE, which correspond to CRUD operations. It also discusses session tracking techniques in servlets, including cookies, hidden form fields, URL rewriting, and the HttpSession interface, emphasizing their advantages and disadvantages. Additionally, it explains how cookies work, their types, and the functionality of the Cookie class in Java servlets.

Uploaded by

vrawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

HTTP Requests

The request sent by the computer to a web server, contains all sorts of potentially interesting
information; it is known as HTTP requests.

The HTTP client sends the request to the server in the form of request message which includes
following information:

o The Request-line
o The analysis of source IP address, proxy and port
o The analysis of destination IP address, protocol, port and host
o The Requested URI (Uniform Resource Identifier)
o The Request method and Content
o The User-Agent header
o The Connection control header
o The Cache control header
The HTTP request method indicates the method to be performed on the resource identified by
the Requested URI (Uniform Resource Identifier). This method is case-sensitive and should
be used in uppercase.

The HTTP request methods are:

HTTP Request Description

GET Asks to get the resource at the requested URL.

Asks the server to accept the body info attached.


POST It is like GET request with extra info sent with
the request.

Asks for only the header part of whatever a GET


HEAD
would return. Just like GET but with no body.

Asks for the loopback of the request message, for


TRACE
testing or troubleshooting.

Says to put the enclosed info (the body) at the


PUT
requested URL.

DELETE Says to delete the resource at the requested URL.

Asks for a list of the HTTP methods to which the


OPTIONS
thing at the request URL can respond
HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify
what action is to be performed on a particular resource. The most commonly used HTTP
request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to
the CRUD operations (create, read, update, and delete).

Table of Content
 HTTP Requests
 Implementing GET Request
 Making a POST Request
 Making a PATCH Request
 Making a DELETE Request
HTTP Requests
HTTP Requests are the message sent by the client to request the data from the server or to
perform some actions. Different HTTP requests are:
 GET: GET request is used to read/retrieve data from a web server. GET returns an HTTP
status code of 200 (OK) if the data is successfully retrieved from the server.
 POST: POST request is used to send data (file, form data, etc.) to the server. On
successful creation, it returns an HTTP status code of 201.
 PUT: A PUT request is used to modify the data on the server. It replaces the entire
content at a particular location with data that is passed in the body payload. If there are no
resources that match the request, it will generate one.
 PATCH: PATCH is similar to PUT request, but the only difference is, it modifies a part
of the data. It will only replace the content that you want to update.
 DELETE: A DELETE request is used to delete the data on the server at a specified
location.
Session Tracking in Servlets

1. Session Tracking
2. Session Tracking Techniques

Session simply means a particular interval of time.

Session Tracking is a way to maintain state (data) of an user. It is also known as session
management in servlet.

Http protocol is a stateless so we need to maintain state using session tracking techniques. Each
time user requests to the server, server treats the request as the new request. So we need to
maintain the state of an user to recognize to particular user.

HTTP is stateless that means each request is considered as the new request. It is shown in the
figure given below:

Why use Session Tracking?


To recognize the user It is used to recognize the particular user.

Session Tracking Techniques


There are four techniques used in Session tracking:

1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Cookies in Servlet
A cookie is a small piece of information that is persisted between the multiple client requests.

A cookie has a name, a single value, and optional attributes such as a comment, path and domain
qualifiers, a maximum age, and a version number.

How Cookie works


By default, each request is considered as a new request. In cookies technique, we add cookie
with response from the servlet. So cookie is stored in the cache of the browser. After that if
request is sent by the user, cookie is added with request by default. Thus, we recognize the user
as the old user.

Types of Cookie
There are 2 types of cookies in servlets.

1. Non-persistent cookie
2. Persistent cookie

Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.

Persistent cookie
It is valid for multiple session . It is not removed each time when user closes the browser. It is
removed only if user logout or signout.

Advantage of Cookies

1. Simplest technique of maintaining the state.


2. Cookies are maintained at client side.
Disadvantage of Cookies

1. It will not work if cookie is disabled from the browser.


2. Only textual information can be set in Cookie object.

Note: Gmail uses cookie technique for login. If you disable the cookie, gmail won't work.

Cookie class
[Link] class provides the functionality of using cookies. It provides a lot of
useful methods for cookies.

Constructor of Cookie class

Constructor Description

Cookie() constructs a cookie.

constructs a cookie with a specified name and


Cookie(String name, String value)
value.
2) Hidden Form Field

1. Hidden Form Field


2. Example of Hidden Form Field

In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of
an user.

In such case, we store the information in the hidden field and get it from another servlet. This
approach is better if we have to submit form in all the pages and we don't want to depend on the
browser.

Let's see the code to store value in hidden field.

1. <input type="hidden" name="uname" value="Vimal Jaiswal">


Here, uname is the hidden field name and Vimal Jaiswal is the hidden field value.

Real application of hidden form field


It is widely used in comment form of a website. In such case, we store page id or page name in
the hidden field so that each page can be uniquely identified.

Advantage of Hidden Form Field

1. It will always work whether cookie is disabled or not.

Disadvantage of Hidden Form Field:

1. It is maintained at server side.


2. Extra form submission is required on each pages.
3. Only textual information can be used.

Example of using Hidden Form Field

In this example, we are storing the name of the user in a hidden textfield and getting that value
from another servlet.
)URL Rewriting

1. URL Rewriting
2. Advantage of URL Rewriting
3. Disadvantage of URL Rewriting
4. Example of URL Rewriting

In URL rewriting, we append a token or identifier to the URL of the next Servlet or the next
resource. We can send parameter name/value pairs using the following format:

url?name1=value1&name2=value2&??

A name and a value is separated using an equal = sign, a parameter name/value pair is separated
from another parameter using the ampersand(&). When the user clicks the hyperlink, the
parameter name/value pairs will be passed to the server. From a Servlet, we can use
getParameter() method to obtain a parameter value.
Advantage of URL Rewriting

1. It will always work whether cookie is disabled or not (browser independent).


2. Extra form submission is not required on each pages.

Disadvantage of URL Rewriting

1. It will work only with links.


2. It can send Only textual information.
4) HttpSession interface

1. HttpSession interface
2. How to get the HttpSession object
3. Commonly used methods of HttpSession interface
4. Example of using HttpSession

In such case, container creates a session id for each [Link] container uses this id to identify the
particular [Link] object of HttpSession can be used to perform two tasks:

1. bind objects
2. view and manipulate information about a session, such as the session identifier, creation
time, and last accessed time.

How to get the HttpSession object ?


The HttpServletRequest interface provides two methods to get the object of HttpSession:

1. public HttpSession getSession():Returns the current session associated with this request,
or if the request does not have a session, creates one.
2. public HttpSession getSession(boolean create):Returns the current HttpSession
associated with this request or, if there is no current session and create is true, returns a
new session.

Commonly used methods of HttpSession interface

1. public String getId():Returns a string containing the unique identifier value.


2. public long getCreationTime():Returns the time when this session was created,
measured in milliseconds since midnight January 1, 1970 GMT.
3. public long getLastAccessedTime():Returns the last time the client sent a request
associated with this session, as the number of milliseconds since midnight January 1,
1970 GMT.
4. public void invalidate():Invalidates this session then unbinds any objects bound to it.

Example of using HttpSession

In this example, we are setting the attribute in the session scope in one servlet and getting that
value from the session scope in another servlet. To set the attribute in the session scope, we have
used the setAttribute() method of HttpSession interface and to get the attribute, we have used the
getAttribute method.

[Link]
1. <form action="servlet1">
2. Name:<input type="text" name="userName"/><br/>
3. <input type="submit" value="go"/>
4. </form>

You might also like