0% found this document useful (0 votes)
1 views

Micro Teaching_JSP

Uploaded by

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

Micro Teaching_JSP

Uploaded by

Anshu Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

School of Computing Science and Engineering

Course Code : R1UC602C.. Course Nam: Web Technology

…….. Web Technology (Session Tracking in JSP) …….

Name of the Faculty: Dr. Avinash Dwivedi Program Name: B.Tech (CSE)
PREREQUISITES, AIM AND LEARNING
OUTCOMES
Prerequisite of topic: Basic concepts of web programming.
Aim: This session is to provide an elementary understanding of session
management in Java Server Pages(JSP). Students will learn the
importance of user session management and its implementation in web
development.

Learning Outcomes:
By the end of this session, students should be able to:
Understand the concept of session management and its
importance in web development
Apply session management principle to complete a transaction

2
SESSION TRACKING

To recognize the user It is used


to recognize the particular user.
REQUIREMENT OF SESSION
TRACKING
HTTP protocol and Web Servers are stateless.
- Web server every request is a new request to process
and they
can’t identify if it’s coming from client that has been
sending
request previously.

But sometimes in web applications, we should know


who the client is and process the request
accordingly.

For example, a shopping cart application should


know who is sending the request to add an item and
in which cart the item has to be added or who is
sending checkout request so that it can charge the
amount to correct client.
SESSION TRACKING

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 JSP.
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 PROTOCOL IN CLIENT
SERVER ARCHITECTURE
HTTP is stateless that means each request
is considered as the new request. It is
shown in the figure given below:
SESSION TRACKING
TECHNIQUES
There are four techniques used in
Session tracking:
• Cookies
• Hidden Form Field
• URL Rewriting
• HttpSession
CLIENT & SERVER
COMMUNICATION
COOKIES IN JSP

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.
Non-persistent cookie
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.
ADVANTAGES & DISADVANTAGES OF
COOKIES

Advantage of Cookies
Simplest technique of maintaining the state.
Cookies are maintained at client side.

Disadvantage of Cookies
It will not work if cookie is disabled from the browser.
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.
COOKIES API

Cookies are created using Cookie class


present in Servlet API. Cookies are added
to response object using the addCookie()
method.

This method sends cookie information over


the HTTP response
stream. getCookies() method is used to access
the cookies that are added to response object.
HOW TO CREATE COOKIE?

Cookie ck=new Cookie("user",“Amit kumar");


//creating cookie object
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.
Cookie ck=new Cookie("user","");//
deleting value of cookie
ck.setMaxAge(0);
//changing the maximum age to 0 seconds
response.addCookie(ck);
//adding cookie in the response
HOW TO GET COOKIES?

Let's see the simple code to get all the


cookies.
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++){
out.print("<br>"+ck[i].getName()
+" "+ck[i].getValue());
//printing name and value of cookie
}
SIMPLE EXAMPLE OF
JSP/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.
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.
INDEX.HTML

<html> <body>
<form action = "main.jsp" method = "GET">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body> </html>
MAIN.JSP
<%
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name",
request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name",
request.getParameter("last_name"));
// Set expiry date after 24 Hrs for both the
cookies.
firstName.setMaxAge(60*60*24);
lastName.setMaxAge(60*60*24);
// Add both the cookies in the response
header.
response.addCookie( firstName );
response.addCookie( lastName ); %>
MAIN.JSP
<html> <head> <title>Setting Cookies</title> </head>
<body>
<center>
<h1>Setting Cookies</h1>
</center>
<ul>
<li><p><b>First Name:</b>
<%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last Name:</b>
<%= request.getParameter("last_name")%>
</p></li>
</ul>
<form action="navigate.jsp" method="post">
<label for="url">Enter URL:</label>
<input type="text" id="url" name="url">
<input type="submit" value="Go">
</form> </body> </html>
NAVIGATE .JSP

<html> <head> <title>Reading Cookies</title> </head>


<body><center> <h1>Reading Cookies</h1> </center>
<% Cookie cookie = null;
Cookie[] cookies = null;
cookies = request.getCookies(); // Get an array of Cookies associated with the this domain
if( cookies != null ) {
out.println("<h2> Found Cookies Name and Value</h2>");
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
out.print("Name : " + cookie.getName( ) + ", ");
out.print("Value: " + cookie.getValue( )+" <br/>"); }
} else {
out.println("<h2>No cookies founds</h2>"); }
%> </body></html>
SESSION TRACKING
TECHNIQUES
There are four techniques used in Session
tracking:
• Cookies
• Hidden Form Field
• URL Rewriting
• HttpSession

You might also like