JSP Implicit object "session"


 

JSP Implicit object "session"

In this Section , we will discuss about JSP implicit object "session" with an example.

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 rewriting.

Methods of session object:
  • setAttribute(String, object)
  • getAttribute(String name)
  • getAttributeNames
  • isNew()
  • getCreationTime
  • getId
  • invalidate()
  • getLastAccessedTime
  • getMaxInactiveInterval
  • removeAttribute(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 "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.

Example : Here is a set of pages that put a user's name in the session, and display it elsewhere.

Displayname.html

<HTML>
<BODY>
<FORM METHOD=POST ACTION="session.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

session.jsp

<%@page language="java" %>

<%

String name = request.getParameter( "username" );

session.setAttribute( "theName", name );

%>

<HTML>

<HEAD><TITLE>CLICK TO CONTINUE</TITLE></HEAD>

<BODY>

<A HREF="sessionresult.jsp">Click to Continue</A>

</BODY>

</HTML>

sessionresult.jsp

<%@page language="java" session="true" %>

<HTML>

<HEAD><TITLE>DISPALYING YOUR NAME</TITLE></HEAD>

<BODY>

<font color=red size="10">Hello, <%= session.getAttribute( "theName" ) %></font>

</BODY>

</HTML>

OUTPUT

After submitting value(output)--->

Download Source Code

0

Ads