Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Sessions

JSP Implicit Objects

JSP Container makes certain JAVA Objects available on


JSP. These objects are called Implicit objects.

Implicit objects need not to be declared, defined or


instantiated like other JAVA Objects.

One example is the session Object which is defined for


you and you can start using it in your jsp.
HttpSession
HttpSession objects live on the server;
they're just automatically associated with
the requester by a behind-the-scenes
mechanism like cookies or URL-rewriting.
These session objects have a builtin data
structure that let you store any number of
keys and associated value
Example
** In this example we will take 2 numbers
in the file session_001.jsp
** in session_002 we will add them to a
session and take a name
** In ShowNumbesSession.jsp
we will retrieve the numbers from the
session and the name from the parameters
and display them
<%--
Document : session_001
Created on : Aug 11, 2013, 4:17:32 PM
Author : ehab
--%>

<%@page contentType="text/html" pageEncoding="windows-1256"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<table>
<form name="finishStart" action="session_002.jsp" mode="Get">
<TR><TD> start</TD> <TD> <input name="start"> </TD> </TR>
<TR><TD> Finish</TD> <TD> <input name="finish"> </TD> </TR>
<TR> <TD colspan="2"> <input type="submit" value="show numbers"
style="width:250px;" > </TD> </TR>
</form>
</table>
</body>
</html>
<%--
Document : session_002
Created on : Aug 11, 2013, 4:20:57 PM
Author : ehab
--%>

<%@page contentType="text/html" pageEncoding="windows-1256"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<%
String s = request.getParameter("start");
String f = request.getParameter("finish");
session = request.getSession();
int si = Integer.parseInt(s); // convert string to int
int fi = Integer.parseInt(f);
session.setAttribute("start",si); // add attribute to the session
session.setAttribute("finish", fi);
%>
<form name="finishStart" action="showNumbersSession.jsp" mode="Get">
<TR><TD> Name</TD> <TD> <input name="name"> </TD> </TR>
<TR> <TD colspan="2"> <input type="submit" value="show numbers" style="width:250px;" > </TD>
</TR>
</form>
</body>
</html>
<%--
Document : showNumbersSession
Created on : Jul 26, 2013, 7:22:08 PM
Author : ehab
--%>

<%@page contentType="text/html" pageEncoding="windows-1256"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<%= request.getParameter("name") %>
<br>
<%
int s = (Integer) session.getAttribute("start"); // must cast to int
int f = (Integer) session.getAttribute("finish");
for(int i=s; i<=f; i++){
out.print(i+ " - ");
}
%>
</body>
</html>
Run session_001
Assignment # 2

Modify the mlutiplication table given in


assignment # 1 to take the start and end
parmateers and put them into a session
and take a title and display athe title above
the multiplication table. Must use
setAttribute nad get atttribute for the start
and finish..

You might also like