In this Section , we will discuss about JSP implicit object "session" with an example.
JSP IMPLICIT OBJECT "SESSION"
In this Section , we will discuss about JSP implicit object "session" with an
example. 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
Methods of session object:
- getAttribute(String name)
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
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.
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.
Example : Here is a set of pages that put a user's name in
the session, and display it elsewhere.
<FORM METHOD=POST ACTION="session.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
String name = request.getParameter(
"theName", name );
After submitting value(output)--->