JSP

JSP session Object

JSP session Object is implicitly available to the developer on Java Server Pages. Session object is useful in maintaining objects and accessing it values in different JSPs. Session object has a scope of session.

Session Object represents the data associated with a user’s session. The request and response, are used to pass information from web browser to server and from server to web browser respectively. While the connection between client & the server is provided by “session” object. The main use of ‘session’ object is to maintain states during multiple page requests.

The main feature of session object is to navigate between multiple pages in a application where variables are stored for the entire user session. The session objects do not lose the variables and the value remains for the user?s session. The concept of maintenance of sessions can be performed by cookies or URL rewriting.

Methods of session object:

setAttribute(String, object) :

This method sets the value of object as string’s value and you can get this value in other JSP page which is connected to this page.

getAttribute(String name) :

This method is use to fetch the value of “String” which is set by you, in other JSP page, using “setAttribute()” method.

getAttributeNames() :

The “getAttributeNames” method of session object is used to retrieve all attribute names associated with the current session. The name of each object of the current session is returned. The value returned by this method is an enumeration of objects that contains all the unique names stored in the session object.

isNew():

The isNew() method of session object returns a true value if the session is new. If the session is not new, then a false value is returned. The session is marked as new if the server has created the session, but the client has not yet acknowledged the session. If a client has not yet chosen the session, i.e., the client switched off the cookie by choice, then the session is considered new. Then the isNew() method returns true value until the client joins the session. Thus, the isNew() method session object returns a Boolean value of true of false.

invalidate():

This method invalidates this session and unbinds any objects bound to it.
Check whether session object is null or not. If the session object null means, session is already expired or not yet initiated. Use invalidate() method on session object to invalidate a session if the session object is not null.

setMaxInactiveInterval(int interval):

Get max time in milliseconds of inactive interval. This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

getMaxInactiveInterval():

This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.

removeAttribute(String name):

Remove attribute of given name from session object.

public long getLastAccessedTime():

This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.

public String getId():

This method returns a string containing the unique identifier assigned to this session.

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago