SlideShare a Scribd company logo
Enterprise Java
Unit-2
Chapter-3
• Session
By
Prof. Sandeep Vishwakarma
Session in Servlets
HTTP is a stateless protocol. All requests and responses are
independent. But sometimes you need to keep track of
client's activity across multiple requests. For eg. When a
User logs into your website, not matter on which web page
he visits after logging in, his credentials will be with the
server, until he logs out. So this is managed by a session.
The basic concept behind session is, whenever a user starts
using our application, we can save a unique identification
information about him, in an object which is available
throughout the application, until its destroyed. So wherever
the user goes, we will always have his information and we
can always manage which user is doing what. Whenever a
user wants to exit from your application, destroy the object
with his information.
How Session Works
Session is used to store everything that we can get from the client
from all the requests the client makes
Lifecycle of HttpSession?
HttpSession object is used to store entire session with a
specific client. We can store, retrieve and remove attribute
from HttpSession object. Any servlet can have access to
HttpSession object throughout the getSession() method of
the HttpServletRequest object.
• On client's first request, the Web Container generates a
unique session ID and gives it back to the client with
response. This is a temporary session created by web
container.
• The client sends back the session ID with each request.
Making it easier for the web container to identify where
the request is coming from.
• The Web Container uses this ID,finds the matching session
with the ID and associates the session with request .
How HttpSession works
Servlet: HttpSession Interface
Session Management
Session Management is a mechanism used by
the Web container to store session information
for a particular user. There are four different
techniques used by Servlet application for session
management.
They are as follows-
• Cookies
• Hidden form field
• URL Rewriting
• HttpSession
1.Cookies
Cookies are small pieces of information that are sent
in response from the web server to the
client. Cookies are the simplest technique used for
storing client state.
Cookies are stored on client's computer. They have a
lifespan and are destroyed by the client browser at
the end of that lifespan.
Using Cookies for storing client state has one
shortcoming though, if the client has turned of
Cookie saving settings in his browser then, client
state can never be saved because the browser will
not allow the application to store cookies.
Advantages and Disadvantages of Cookies
Advantages
• Simplest technique of maintaining the state.
• Cookies are maintained at client side.
• Cookie information can be extracted from the
client request using getCookies() of
HttpSevletRequest
Disadvantages
• It will not work if cookie is disabled from
the browser.
• Only textual information can be set in
Cookie object.
2.URL Rewriting for Session Management
If the client has disabled cookies in the browser then session
management using cookie wont work. In that case URL
Rewriting can be used as aback up. URL rewriting will always
work.
In URL rewriting, a token(parameter) is added at the end of
the URL. The token consist of name/value pair separated by
an equal(=) sign.
For Example:
Advantages:
• It will always work whether cookie is disabled
or not (browser independent).
• Extra form submission is not required on each
pages.
Disadvantages:
• It will work only with links.
• It can send Only textual information.
Advantages and Disadvantages of URL Rewriting
3.Hidden Form Field for Session Management
Hidden form field can also be used to store session
information for a particular client. In case of hidden
form field a hidden field is used to store client
state. In this case user information is stored in
hidden field value and retrieved 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
Advantages:
• It will always work whether cookie is disabled or
not.
• Inserting a simple HTML Input field of type hidden
is required. Hence, its easier to implement.
Disadvantages:
• It is maintained at server side.
• Extra form submission is required on each pages.
• Only textual information can be used.
Advantages and Disadvantages of Hidden Form Field
4. HTTP Session Management
In this case, container creates a session id for each user.
The container uses this id to identify the particular user.An
object of HttpSession can be used to perform two tasks:
• bind objects
• view and manipulate information about a session, such
as the session identifier, creation time, and last
accessed time.
Advantages:
• It stores user states and data to all over the application.
• Stores every user data separately.
• Session is secure and transparent from user because
session object is stored on the server
Disadvantages:
• Performance overhead in case of large number of user,
because of session data stored in server memory.
Advantages and Disadvantages of HTTP Session
Example for demonstrating usage of
HttpSession
Following pages are used for Demonstrating
HTTP Session
• Index.html
• Validate.java
• Welcome.java
• web.xml
Index.html
<form method="post" action="Validate">
User: <input type="text" name="user" /> <br/>
Password: <input type="text" name="pass" ><br/>
<input type="submit" value="submit">
</form>
Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("user");
String pass = request.getParameter("pass");
if(pass.equals("1234"))
{
HttpSession session = request.getSession(); //creating a session
session.setAttribute("user", name);
response.sendRedirect("Welcome");
} } }
Welcome.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Welcome extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String user = (String)session.getAttribute("user");
out.println("Hello "+user);
} }
web.xml
<web-app..>
<servlet>
<servlet-name>Validate</servlet-name>
<servlet-class>Validate</servlet-class>
</servlet>
<servlet>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Thank You
Ad

More Related Content

What's hot (20)

Servlets
ServletsServlets
Servlets
Sasidhar Kothuru
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
ejlp12
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
kamal kotecha
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
Peter R. Egli
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
Prem Sanil
 
ASP.NET Basics
ASP.NET Basics ASP.NET Basics
ASP.NET Basics
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
Return on Intelligence
 
Lecture 3: Servlets - Session Management
Lecture 3:  Servlets - Session ManagementLecture 3:  Servlets - Session Management
Lecture 3: Servlets - Session Management
Fahad Golra
 
Tomcat
TomcatTomcat
Tomcat
Venkat Pinagadi
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
Venkateswara Rao N
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Jsp tag library
Jsp tag libraryJsp tag library
Jsp tag library
sandeep54552
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
Halil Burak Cetinkaya
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
Santosh Kumar Kar
 

Similar to Enterprise java unit-2_chapter-3 (20)

C# cookieless session id and application state
C# cookieless session id and application stateC# cookieless session id and application state
C# cookieless session id and application state
Malav Patel
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
Jayaprasanna4
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
JainamParikh3
 
Session and state management
Session and state managementSession and state management
Session and state management
Paneliya Prince
 
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptxIMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
yvtinsane
 
State Management.pptx
State Management.pptxState Management.pptx
State Management.pptx
DrMonikaPatel2
 
SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6
Ben Abdallah Helmi
 
Session tracking In Java
Session tracking In JavaSession tracking In Java
Session tracking In Java
honeyvachharajani
 
It and ej
It and ejIt and ej
It and ej
Harihar Kalia
 
State management
State managementState management
State management
Lalit Kale
 
Sessions&cookies
Sessions&cookiesSessions&cookies
Sessions&cookies
Tirthika Bandi
 
UNIT - 5.pptx Servlets And Database Connectivity
UNIT - 5.pptx  Servlets And Database ConnectivityUNIT - 5.pptx  Servlets And Database Connectivity
UNIT - 5.pptx Servlets And Database Connectivity
bmit1
 
State management in ASP.net
State  management in ASP.netState  management in ASP.net
State management in ASP.net
baabtra.com - No. 1 supplier of quality freshers
 
Session,Cookies and Authentication
Session,Cookies and AuthenticationSession,Cookies and Authentication
Session,Cookies and Authentication
Knoldus Inc.
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other Techniques
PawanMM
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
pkaviya
 
Session and Cookies.pdf
Session and Cookies.pdfSession and Cookies.pdf
Session and Cookies.pdf
HamnaGhani1
 
State management in ASP .NET
State  management in ASP .NETState  management in ASP .NET
State management in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Session and cookies,get and post
Session and cookies,get and postSession and cookies,get and post
Session and cookies,get and post
baabtra.com - No. 1 supplier of quality freshers
 
C# cookieless session id and application state
C# cookieless session id and application stateC# cookieless session id and application state
C# cookieless session id and application state
Malav Patel
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
Jayaprasanna4
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
JainamParikh3
 
Session and state management
Session and state managementSession and state management
Session and state management
Paneliya Prince
 
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptxIMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
yvtinsane
 
SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6SCWCD : Session management : CHAP : 6
SCWCD : Session management : CHAP : 6
Ben Abdallah Helmi
 
State management
State managementState management
State management
Lalit Kale
 
UNIT - 5.pptx Servlets And Database Connectivity
UNIT - 5.pptx  Servlets And Database ConnectivityUNIT - 5.pptx  Servlets And Database Connectivity
UNIT - 5.pptx Servlets And Database Connectivity
bmit1
 
Session,Cookies and Authentication
Session,Cookies and AuthenticationSession,Cookies and Authentication
Session,Cookies and Authentication
Knoldus Inc.
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other Techniques
PawanMM
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
pkaviya
 
Session and Cookies.pdf
Session and Cookies.pdfSession and Cookies.pdf
Session and Cookies.pdf
HamnaGhani1
 
Ad

More from sandeep54552 (20)

Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptx
sandeep54552
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
sandeep54552
 
Dijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptxDijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptx
sandeep54552
 
DFS_New.pptx
DFS_New.pptxDFS_New.pptx
DFS_New.pptx
sandeep54552
 
Agents_AI.ppt
Agents_AI.pptAgents_AI.ppt
Agents_AI.ppt
sandeep54552
 
YCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.pptYCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.ppt
sandeep54552
 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptx
sandeep54552
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptx
sandeep54552
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
sandeep54552
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
sandeep54552
 
Quick_sort1.pptx
Quick_sort1.pptxQuick_sort1.pptx
Quick_sort1.pptx
sandeep54552
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
sandeep54552
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
sandeep54552
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
sandeep54552
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
sandeep54552
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++
sandeep54552
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
sandeep54552
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
sandeep54552
 
Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptx
sandeep54552
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
sandeep54552
 
Dijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptxDijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptx
sandeep54552
 
YCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.pptYCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.ppt
sandeep54552
 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptx
sandeep54552
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptx
sandeep54552
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
sandeep54552
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
sandeep54552
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
sandeep54552
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++
sandeep54552
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
sandeep54552
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
sandeep54552
 
Ad

Recently uploaded (20)

New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 

Enterprise java unit-2_chapter-3

  • 2. Session in Servlets HTTP is a stateless protocol. All requests and responses are independent. But sometimes you need to keep track of client's activity across multiple requests. For eg. When a User logs into your website, not matter on which web page he visits after logging in, his credentials will be with the server, until he logs out. So this is managed by a session. The basic concept behind session is, whenever a user starts using our application, we can save a unique identification information about him, in an object which is available throughout the application, until its destroyed. So wherever the user goes, we will always have his information and we can always manage which user is doing what. Whenever a user wants to exit from your application, destroy the object with his information.
  • 3. How Session Works Session is used to store everything that we can get from the client from all the requests the client makes
  • 4. Lifecycle of HttpSession? HttpSession object is used to store entire session with a specific client. We can store, retrieve and remove attribute from HttpSession object. Any servlet can have access to HttpSession object throughout the getSession() method of the HttpServletRequest object. • On client's first request, the Web Container generates a unique session ID and gives it back to the client with response. This is a temporary session created by web container. • The client sends back the session ID with each request. Making it easier for the web container to identify where the request is coming from. • The Web Container uses this ID,finds the matching session with the ID and associates the session with request .
  • 7. Session Management Session Management is a mechanism used by the Web container to store session information for a particular user. There are four different techniques used by Servlet application for session management. They are as follows- • Cookies • Hidden form field • URL Rewriting • HttpSession
  • 8. 1.Cookies Cookies are small pieces of information that are sent in response from the web server to the client. Cookies are the simplest technique used for storing client state. Cookies are stored on client's computer. They have a lifespan and are destroyed by the client browser at the end of that lifespan. Using Cookies for storing client state has one shortcoming though, if the client has turned of Cookie saving settings in his browser then, client state can never be saved because the browser will not allow the application to store cookies.
  • 9. Advantages and Disadvantages of Cookies Advantages • Simplest technique of maintaining the state. • Cookies are maintained at client side. • Cookie information can be extracted from the client request using getCookies() of HttpSevletRequest Disadvantages • It will not work if cookie is disabled from the browser. • Only textual information can be set in Cookie object.
  • 10. 2.URL Rewriting for Session Management If the client has disabled cookies in the browser then session management using cookie wont work. In that case URL Rewriting can be used as aback up. URL rewriting will always work. In URL rewriting, a token(parameter) is added at the end of the URL. The token consist of name/value pair separated by an equal(=) sign. For Example:
  • 11. Advantages: • It will always work whether cookie is disabled or not (browser independent). • Extra form submission is not required on each pages. Disadvantages: • It will work only with links. • It can send Only textual information. Advantages and Disadvantages of URL Rewriting
  • 12. 3.Hidden Form Field for Session Management Hidden form field can also be used to store session information for a particular client. In case of hidden form field a hidden field is used to store client state. In this case user information is stored in hidden field value and retrieved 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
  • 13. Advantages: • It will always work whether cookie is disabled or not. • Inserting a simple HTML Input field of type hidden is required. Hence, its easier to implement. Disadvantages: • It is maintained at server side. • Extra form submission is required on each pages. • Only textual information can be used. Advantages and Disadvantages of Hidden Form Field
  • 14. 4. HTTP Session Management In this case, container creates a session id for each user. The container uses this id to identify the particular user.An object of HttpSession can be used to perform two tasks: • bind objects • view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.
  • 15. Advantages: • It stores user states and data to all over the application. • Stores every user data separately. • Session is secure and transparent from user because session object is stored on the server Disadvantages: • Performance overhead in case of large number of user, because of session data stored in server memory. Advantages and Disadvantages of HTTP Session
  • 16. Example for demonstrating usage of HttpSession Following pages are used for Demonstrating HTTP Session • Index.html • Validate.java • Welcome.java • web.xml
  • 17. Index.html <form method="post" action="Validate"> User: <input type="text" name="user" /> <br/> Password: <input type="text" name="pass" ><br/> <input type="submit" value="submit"> </form>
  • 18. Validate.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Validate extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String name = request.getParameter("user"); String pass = request.getParameter("pass"); if(pass.equals("1234")) { HttpSession session = request.getSession(); //creating a session session.setAttribute("user", name); response.sendRedirect("Welcome"); } } }
  • 19. Welcome.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Welcome extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); String user = (String)session.getAttribute("user"); out.println("Hello "+user); } }