Vous êtes sur la page 1sur 8

SESSION MANAGMENT IN JSP

:
SESSION
:

 Session is a JSP implicit object


 A session object uses a key/value combination to store information.

MAIN PURPOSE

 It counts how many times a particular user visits a webpage.

GETTING INFORMATION FROM SESSION


To retrieve information from session,
session.getValue(“vp”);
where,
vp is a key of session object
 The return type of the getValue() is Object. So, we will need to type cast to get the required
value
 If there is not a session key with that name, a null value is returned
SETTING INFORMATION TO SEESION
To set a session key with a value,
session.putValue(“vp”,count);
STEPS:
1. Get the value of the session variable-vp
2. If the session variable (vp) is null, then set the session variable to 1 and welcome the
visitor
1
3. If the session variable is not null (after step 2), increment the session variable and
display the number of visits.

BUILTIN METHODS

1. Object getValue(String keyName)


 It is an instance method of session object
 It returns value(Object type) of given key
 Key can be value type (e.g: int,double,..) or reference type (e.g: String)
 Return type: Object
 Here Object is a generic class in java. It is reference type not value type
 In java, Object is a super class of all java sub classes
 Object is found in java.lang.Object (package)
2. void putValue(String KeyName, Object Value)
 It is based on key-value pair
 It stores the information based on key-value pair
 key never accept duplicates but value may accept duplicate values
 Return type: void

I. EXAMPLE OF SESSION COUNTING


:
:
1. SOURCE CODE
<html>
<body>
<%  opening JSP tag
2
// get the value of the session object-vp ( vp -> key of session object)
Integer count=(Integer)session.getValue("vp");
if(count==null)
{
// set the session variable to 1
count=new Integer(1);
// set the key & value of session object (implicit object)
session.putValue("vp", count);
// print 1st visited count to browser
out.println("Welcome, Visitor");
}
else
{
// increment the count the value
// here the count is actually an object of Integer class. so convert this to int type
int c=count.intValue();
// add the updated int value(count object) to Integer class's constructor
count=new Integer(c+1);
// set the key & value of session object (implicit object)
session.putValue("vp", count);
// print the no. of visited counts to browser
out.println("You have visited this page "+count+ " time(s)");
}
%>  closing JSP tag
</body>
</html>

3
2. OUTPUT

2.1 FORM LOADING (1st time page visit)

2.2 SESSION COUNTING („n‟ times visiting)

4
SEESION TRACKING

:
 It tracks the session between different JSP pages
:

BUILT-IN METHODS
:
1. setAttribute(String keyName, Object Value)
:
 It is used to store the information based on the key/value pair
 It binds an object to this session, using the name specified.
 If an object of the same name is already bound to the session, the object is replaced.
 Return type: void
2. getAttribute(String keyname)
 It is used to get the value from session.
 Returns the object bound with the specified name in this session, or null if no object is
bound under the name.
 Return type: Object.

II. EXAMPLE OF SESSION TRACKING: EXCHANGE DATA B/W DIFFERENT JSP PAGES

:
:
1. SOURCE CODE

input.jsp
<html>
<body>
<form action="save.jsp" method="post">
Enter ur name :<br> <input type="text" name="t1"><br>
<input type="submit" value="Submit">
5
</form>
</body>
</html>

save.jsp
<%
// get the client data using JSP request object
String name=request.getParameter("t1");
// store / save the form data into session variable : key / value pair
session.setAttribute("t1",name);
%>
<html>
<body>
<p><a href="display.jsp">Next page to view the session value</a></p>
</body>
</html>

display.jsp
<html><center>
<%
// just pass the form element name in the argument of getAttribute() of session // object
String str=(String)session.getAttribute("t1");
out.println("Name is : "+str);
%>
</center>
</html>

6
2. OUTPUT
2.1 INPUT FORM-USER REQUESTs via JSP

NOTE:
 The above JSP page takes the input from user.

2.2 PAGE 2: SAVING FORM DATA IN SESSION

7
NOTE:
 The above JSP page saves the form data (Ex. textbox data) into session object

2.3 SHOW THE PAGE 1‟s DATA in PAGE 3 via SESSION OBJECT

RESULT
 Now we can get the same value (entered in “input.jsp”) in “display.jsp”.

Vous aimerez peut-être aussi