Unit-V
HttpSession and Cookies
Cookies
Cookies are small pieces of data stored on the client side (browser) and sent with every
request to the server.
Points:
Stored in the browser.
Can persist even after the browser is closed (based on expiration time).
Limited in size (~4KB).
Can be set from server-side (Java) or client-side (JavaScript).
Java (Servlets):
Create and send a cookie:
Cookie cookie = new Cookie("username", "john123");
cookie.setMaxAge(60 * 60); // 1 hour
response.addCookie(cookie);
Read cookies:
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
String value = cookie.getValue();
}
}
2. HttpSession
HttpSession is a server-side mechanism that stores user data on the server.
Points:
Server-side storage.
Unique JSESSIONID cookie is created and used to track sessions.
Can store any Java object as an attribute.
Automatically created when request.getSession() is called.
In Java (Servlets):
Create or retrieve a session:
HttpSession session = request.getSession();
By: Mr. Amrit Jaiswal 1
Store data:
session.setAttribute("username", "john123");
Retrieve data:
String username = (String) session.getAttribute("username");
Cookies vs. HttpSession
Feature Cookies HttpSession
Storage Client (browser) Server
Size Limit ~4KB Large (limited by server)
Lifetime Set via setMaxAge() Until session expires
Security Less secure (can be edited) More secure
Usage Good for simple data (like preferences) Good for login/session data
Introduction to EJB
Enterprise JavaBeans (EJB) is a server-side component architecture for modular
construction of enterprise applications.
Features:
Simplifies development of distributed, transactional, and secure business applications.
Supports remote access, persistence, concurrency, security, and transaction
management.
Deployed in an EJB container, which provides system-level services (e.g., transactions,
security, lifecycle).
Types of EJBs
EJBs are broadly divided into three main types:
1. Session Bean: Session bean contains business logic that can be invoked by local, remote
or webservice client. There are two types of session beans:
(i) Stateful Session bean :
Stateful session bean performs business task with the help of a state. Stateful
session bean can be used to access various method calls by storing the
information in an instance variable. Some of the applications require
information to be stored across separate method calls. In a shopping site, the
items chosen by a customer must be stored as data is an example of stateful
session bean.
(ii) Stateless Session bean :
Stateless session bean implement business logic without having a persistent
storage mechanism, such as a state or database and can used shared data.
Stateless session bean can be used in situations where information is not
required to use across call methods.
By: Mr. Amrit Jaiswal 2
1. Message Driven Bean: Like Session Bean, it contains the business logic but it is
invoked by passing message.
3. Entity Bean: It summarizes the state that can be remained in the database. It is
deprecated. Now, it is replaced with JPA (Java Persistent API). There are two types
of entity bean:
(i) Bean Managed Persistence :
In a bean managed persistence type of entity bean, the programmer has to write
the code for database calls. It persists across multiple sessions and multiple
clients.
(ii) Container Managed Persistence :
Container managed persistence are enterprise bean that persists across database.
In container managed persistence the container take care of database calls.
Advantages of Enterprise Java Beans
EJB repository yields system-level services to enterprise beans, the bean
developer can focus on solving business problems. Rather than the bean developer, the
EJB repository is responsible for system-level services such as transaction management
and security authorization.
The beans rather than the clients contain the application’s business logic, the client
developer can focus on the presentation of the client. The client developer does not have
to code the pattern that execute business rules or access databases. Due to this the clients
are thinner which is a benefit that is particularly important for clients that run on small
devices.
Enterprise Java Beans are portable elements, the application assembler can build new
applications from the beans that already exists.
Disadvantages of Enterprise Java Beans
Requires application server
Requires only java client. For other language client, you need to go for webservice.
Complex to understand and develop EJB applications.
By: Mr. Amrit Jaiswal 3