session implicit Object in jsp

Introduction of session implicit Object -: Session object is an instance of javax.servlet.http.HttpSession object. This object is used to set, get, remove and get session information from the session scope.

In Java Server Page request and response these objects send some information from JS Engine to the server from Web Browser and also get that information from server to web browser. But this information is done only by single JSP Page.

In JSP Session, when some information is sent to the server from a web browser, then the information can be obtained from any page during that session. The session object is a very important object for JSP.

Methods of session implicit Object

There are several methods available in session implicit object in jsp. The list has been given here

  • setAttribute(String name, Object object)
  • getAttribute(String name)
  • getAttributeNames
  • removeAttribute(String name)
  • isNew()
  • getCreationTime()
  • getId()
  • invalidate()
  • getLastAccessedTime()
  • setMaxInactiveInterval(int seconds)
  • getMaxInactiveInterval()

setAttribute (String name, Object object)-: setAttribute () method is used to set the attribute and its value on a session. For example

<% 
     session.setAttribute("seessionAttr", "session is set");
 %>

getAttribute (string name)-: getAttribute () method is used to get the value of the attribute with the help of the name of the session. If the attribute is set, the value of the attribute can be obtained from any page during that session. If the given attribute name is not valid then ‘null’ returns. For example

<% 
    out.print(session.getAttribute("seessionAttr"));
 %>

getAttributeNames-: getAttributeNames () returns all attribute names set during the session to Enumeration.For example

<%@page import="java.util.*" %> 
  <% 
     Enumeration e=session.getAttributeNames(); 
     while(e.hasMoreElements()){        
    Object obj=e.nextElement();
      String name=(String)obj;
      out.println("Attribute Name : " + name + " ");
       }
 %>

removeAttribute (String name)-: removeAttribute () method is used to remove the set attribute from the attribute name.For example

<% 
   session.removeAttribute("sessionAttr"); 
%>

isNew ()-: isNew () returns boolean value by checking whether this method is session new or not. If the session is new then true returns and if not it is false return. If the cookie is disabled or blocked on the web browser then it returns ‘true’ and if the cookie is enabled or allowed then ‘false’ returns.for example

<% 
   out.print(session.isNew());
 %>

getCreationTime ()-:getCreationTime () method returns the time of created session in milliseconds. This method returns milliseconds from January 1, 1970 00:00:00 until session creation time. For example

<% 
    out.print(session.getCreationTime()); 
%>

getId ()-: getId () This method returns the id of the created session. For example

<%
   out.print(session.getId());
 %>

invalidate ()-: this method is very important of Session Object. Invalid () method inactive all the objects set on the session. login and logout are a recent example of invalidate () method.

getLastAccessedTime ()-: getLastAccessedTime () method returns the last accessing time of the session in milliseconds. This method returns milliseconds from January 1, 1970 00:00:00 until the last accessing time of the session.

<% 
  out.print(session.getLastAccessedTime());
 %>

setMaxInactiveInterval (int seconds)-: setMaxInactiveInterval () method keeps all the objects of the session active for a given time. After that they become inactive. Seconds are given on this method.

getMaxInactiveInterval ()-: getMaxInactiveInterval () method returns the set Inactive Internal time in seconds.

 More about jsp implicit object

 application implicit Object in jsp 

 config implicit Object in jsp 

 out implicit Object in jsp 

 response implicit Object in jsp 

 implicit objects in jsp 

 Directives in jsp 

 Java server Pages Tags 

 life cycle of jsp 

 What is java server pages 

More about programming language 

Leave a Comment