Académique Documents
Professionnel Documents
Culture Documents
:
SESSION
:
MAIN PURPOSE
BUILTIN METHODS
3
2. OUTPUT
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.
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”.