BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Session Implicit Object in JSP with examples

Last Updated: July 27, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

In this tutorial, you will learn session implicit object in JSP. JSP session implicit object is the most frequently used implicit object in JSP. The main use of session is to gain access to all the user’s data till the user session is active.

Methods of session Implicit Object

  1. setAttribute(String, object): This method is used to save an object in session by assigning a unique string to the object. Later, the object can be accessed from the session by using the same String till the session is active. setAttribute and getAttribute are the two most frequently used methods while dealing with session in JSP.
  2. getAttribute(String name): The object stored by setAttribute method is fetched from session using getAttribute method. For example if we need to access userid on every JSP page till the session is active then we should store the user-id in session using setAttribute method and can be accessed using getAttribute method whenever needed.
  3. removeAttribute(String name): The objects which are stored in session can be removed from session using this method. Pass the unique string identifier as removeAttribute’s method.
  4. getAttributeNames: It returns all the objects stored in session. Basically, it results in an enumeration of objects.
  5. getCreationTime(): This method returns the session creation time, the time when session got initiated (became active).
  6. getId(): Servlet container assigns a unique string identifier to session while creation of it. getId method returns that unique string identifier.
  7. isNew(): Used to check whether the session is new. It returns Boolean value (true or false). Mostly used to track whether the cookies are enabled on client side. If cookies are disabled the session.isNew() method would always return true.
  8. invalidate(): It kills a session and breaks the association of session with all the stored objects.
  9. getMaxInactiveInterval: Returns session’s maximum inactivate time interval in seconds.
  10. getLastAccessedTime: Generally used to know the last accessed time of a session.

JSP session Implicit Object Example

The following html page would display a text box along with a submit button. The submit action transfers the control to another JSP page session.jsp.

index.html

<html> 
<head>
<title>Welcome Page: Enter your name</title>
</head>
<body> 
<form action="session.jsp"> 
<input type="text" name="inputname"> 
<input type="submit" value="click here!!"><br/> 
</form> 
</body> 
</html>

The session.jsp page displays the name which user has entered in the index page and it stores the the same variable in the session object so that it can be fetched on any page until the session remains active.

session.jsp

<html> 
<head>
<title>Passing the input value to a session variable</title>
</head>
<body> 
<% 
String uname=request.getParameter("inputname"); 
out.print("Welcome "+ uname);
session.setAttribute("sessname",uname); 
%> 
<a href="output.jsp">Check Output Page Here </a>
</body> 
</html>

In this page we are fetching the value of name variable, which has been stored in the session using setAttribute() method of session implicit object. The user is still active so the user data stored in the session can be accessed. Here, we accessed the user name using getAttribute() method.

output.jsp

<html> 
<head>
<title>Output page: Fetching the value from session</title>
</head>
<body> 
<% 
String name=(String)session.getAttribute("sessname"); 
out.print("Hello User: You have entered the name: "+name); 
%> 
</body> 
</html>

Output Screens
Session Implicit Object in JSP
Session Implicit Object in JSP
Session Implicit Object in JSP

❮ JSP applicationJSP out ❯

Top Related Articles:

  1. Java Server Pages (JSP) Life Cycle
  2. Out Implicit Object in JSP with examples
  3. JSP Scriptlet Tag (Scripting elements)
  4. Config Implicit Object in JSP with examples
  5. Request Implicit Object in JSP with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Balan V says

    May 8, 2015 at 7:53 AM

    The latest version of Safari 5.1 and higher, the session value is always null. However, the same Safari browser is working fine in ‘Stand alone Server’. The site working all other browsers including IE. I have already reported the bugs to Apple. Any clue?

    Reply
  2. nupur koul says

    August 6, 2015 at 4:00 AM

    Can you suggest me how to fetch how many session are active & inactive,fetch all the names according that.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap