Sessions in JSP
Sessions in JSP
In this article we will see about sessions in JSP. On a typical web site, a visitor might visit several pages and perform several interactions.
you are programming the site, it is very helpful to be able to associate some
data with each visitor. For this purpose, "session"s
can be used in JSP.
session is an object associated with a visitor. Data can be put in the
session and retrieved from it, much like a Hashtable. A different set of
data is kept for each visitor to the site.
is a set of pages that put a user's name in the session, and display it
elsewhere. Try out installing and using these.
First we have a form,
let us call it GetName.html
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
The target of the form is
"SaveName.jsp", which saves the user's name in the session. Note
the variable "session".
This is another variable that is normally made available in JSPs, just like out and request
variables. (In the @page directive, you can indicate that you do not need
sessions, in which case the "session" variable will not be made
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
The SaveName.jsp saves the user's name in the session, and puts a link to another page, NextPage.jsp.
shows how to retrieve the saved name.
Hello, <%= session.getAttribute( "theName" ) %>
If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of.
The session is kept around until a timeout period. Then it is assumed the user is no longer visiting the site, and the session is discarded
1. simply super post
View Tutorial By: vinodh at 2010-05-05 00:29:12
2. its not too good
i am looking for some higher level programing
View Tutorial By: bikram at 2009-11-12 21:27:21
3. This matter is really too good.
View Tutorial By: Ashwin perti at 2009-07-08 04:43:55
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
A JSP page that gets properties from a bean
The taglib, tag, include, attribute and the variable Directive in JSP
Scriptlets and Expressions in JSP