HOME
Java Server Pages
JSP
Introduction JSP
Syntax JSP Expression
JSP Declaration
JSP Comments
JSP Directives
JSP Actions
JavaBeans
JSP Sessions
JSP JDBC
JSP And HTML Forms
JSP With XML
JSP Exam
|
JSP Sessions
So far we are well familiar with expression, declaration, directive and beans. This chapter will cover JSP sessions, error handling and database connectivity. JSP provides various implicit objects to handle these functionalists in a JSP page. For example, the request object handles requests coming to a JSP page
A site may contain many JSP pages. Once a user logs into a site, his information is shared by all the resources on the site.
This is made possible thought the use of sessions in the JSP pages. An implicit object called session is provided to handle sessions in a JSP
page
A session can be defines as a series of related interaction between a single client and the server. Once a session object is created on the server, a unique identifier called session id is associated with it, this is then given to the client for communicating with the server. Since the session is generated by the server it is different form server to server, if a user is accessing a site that ahs more than one servlet or JSP page, the same session id is shared amongst all these resources.
Session object
This object has session scope and is an instance of javax.servlet.hhtp.HttpSession. it represents the session created for the requesting client and is only
valid Http requests. Sessions are created automatically, so this variable exists if there was no incoming session reference.
A Simple example for JSP session
Login.html |
<html>
<head> <title> log on screen </title> </head>
<body>
<form method="post" action="session.jsp">
UserName : <input type="text" name="uname">
<br> <br>
Password : <input type="text" name="pass">
<input type="submit" value="Login">
</form>
</body>
</html>
|
session.jsp
<html>
<head> <title> Sessions </title> </head>
<body bgcolor="#ddeedd">
<%! String name="" , pass="";
HttpSession session;
%>
<%
name=request.getParameter("uname");
pass=request.getParameter("pass");
session.putValue("ID", pass);
session.putValue("NAME", uname);
out.println(name + " and " + pass + " are stored in objects");
%>
</body>
</html>
|
On logging in . you can get the password that user has entered in this session using the following code
<html>
<head><title> Retriving Session Objects </title> </head>
<body bgcolor="#34aabb">
<%
String pass= (String) session.getValue("ID");
String name= (String) session.getValue("NAME");
out.println("Retriving Sessions");
out.println("Name = " + name + "<br> Password = " + pass);
%>
</body>
</html>
|
|
Inform Friend About This Site |