Java (current. Jakarta) Server Pages is a framework used for web application development.

This post will discuss:

  • Session Object


Session


Sessions are non-persistent objects that exist only when a browser session is active. They are implicit and thus do not need to be constructed unlike cookies. They can inherit values from the client if specified to do so as part of their attributes.


Cookies and Sessions have similar behavior, with one key difference. Sessions are non-persistent and implicit, meaning that they automatically are created and then destroyed with every new session of a browser.


Sessions do not need to be constructed. Set the session attributes:

session.setAttribute("bc","123");


Session values can be grabbed. They are by default type object:

String session = (String)session.getAttribute("abc");


Other session methods can be used for misc. Functionality. The below page redirects if the session is null and kicks out logged in users by parsing for an existing attribute name.

    String userId = (String)session.getAttribute("s_id");
    Enumeration<String> attNames = session.getAttributeNames();
    boolean get = attNames.hasMoreElements();
	if(userId!=null){
	}else if(get){
		response.sendRedirect("session_login_ok.jsp");
	}


Methods for session object:

setAttribute():
Sets session attributes.
getAttribute():
Gets attributes.
getId():
Gets the session’s unique id.
getCreationTime(): vGets the time at which the session was created.
getLastAccessedTime():
Gets the last access time for the session.
set/getMaxInactiveInterval():
Sets/gets the lifetime of the session once it is inactive.
removeAttribute():
Removes specified attribute.
invalidate():
Invalidates, “deletes” the session.


-gonkgonk


<
Previous Post
Java Server Pages - Response Object, Cookie Object
>
Next Post
Java Server Pages - Application Object and Error Pages