Last Topics From Unit-4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Session

Session simply means a particular interval of time.

 Session: The session is a time interval devoted to an activity.


 Session Tracking: Session Tracking or session management is a way of
maintaining the state of the user.
 Need of Session Tracking: ...
 Way of Session Tracking in servlet:

Way of Session Tracking in servlet:

1. Cookie.
2. Hidden form field.
3. Url rewriting.
4. HttpSession.

COOKIES

Cookies are small files of information that a web server generates and sends to a
web browser. Web browsers store the cookies they receive for a predetermined
period of time, or for the length of a user's session on a website.

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.
Cookie class

javax.servlet.http.Cookie 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.

Cookie(String name, String value) constructs a cookie with a specified name and value.

Useful Methods of Cookie class

There are given some commonly used methods of the Cookie class.

Method Description

public void setMaxAge(int Sets the maximum age of the cookie in seconds.
expiry)

public String getName() Returns the name of the cookie. The name cannot be
changed after creation.

public String getValue() Returns the value of the cookie.

public void setName(String changes the name of the cookie.


name)

public void setValue(String changes the value of the cookie.


value)
Other methods required for using Cookies
For adding cookie or getting the value from the cookie, we need some methods
provided by other interfaces. They are:

1. public void addCookie(Cookie ck):method of HttpServletResponse interface is


used to add cookie in response object.
2. public Cookie[] getCookies():method of HttpServletRequest interface is used
to return all the cookies from the browser.

How to create Cookie?

Let's see the simple code to create cookie.

1. Cookie ck=new Cookie("user","sonoo jaiswal");//creating cookie object


2. response.addCookie(ck);//adding cookie in the response

How to delete Cookie?

Let's see the simple code to delete cookie. It is mainly used to logout or signout the
user.

1. Cookie ck=new Cookie("user","");//deleting value of cookie


2. ck.setMaxAge(0);//changing the maximum age to 0 seconds
3. response.addCookie(ck);//adding cookie in the response

How to get Cookies?

Let's see the simple code to get all the cookies.

1. Cookie ck[]=request.getCookies();
2. for(int i=0;i<ck.length;i++){
3. out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());//printing name and value
of cookie
4. }
Simple example of Servlet Cookies

In this example, we are storing the name of the user in the cookie object and
accessing it in another servlet. As we know well that session corresponds to the
particular user. So if you access it from too many browsers with different values,
you will get the different value.

index.html
1. <form action="servlet1" method="post">
2. Name:<input type="text" name="userName"/><br/>
3. <input type="submit" value="go"/>
4. </form>

URL Rewriting
What is URL rewriting in Java servlet?

Url rewriting is a process of appending or modifying any url structure while


loading a page. The request made by client is always a new request and the server
can not identify whether the current request is send by a new client or the previous
same client.

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.

You might also like