VisualBuilder
  Home > Jsp > Tutorials > Session Tracking in JSP - JSP Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
JSP Tutorial Home
1 . Introduction to JSP
2 . Why use JSP?
3 . JSP compared to ASP,ASP.NET and Servlets
4 . JSP architecture
5 . Setting up the JSP environment
6 . Creating your first JSP page
7 . Using JSP tags
8 . Declaration tag
9 . Expression tag
10 . Directive tag
11 . Scriptlet tag
12 . Action tag
13 . Creating your second JSP page
14 . Implicit Objects
15 . Creating a Form
16 . Processing a Form
17 . Creating a Form (more elements)
18 . Processing a Form (more elements)
19 . Processing a Form (more elements)
20 . Getting Client Info
21 . Review and next steps
22 . Beans scopes in JSP
23 . JSP Bean scope examples
24 . Uploading Application in JSP.
25 . Modularization in JSP
26 . Exception Handling in JSP-1
27 . Exception Handling in JSP-2
28 . Session Tracking in JSP
29 . Security in JSP -2
30 . Security in JSP -1
31 . Internationalization in JSP application
32 . Introduction To Expression Language.
33 . Introduction to Tag libraries
34 . Introduction To Core Taglib -1
35 . Introduction To Core Taglib -2
36 . Function Taglib
37 . Database Handling in JSP.
38 . Creating User Defined Custom Tag -1
39 . Creating User Defined Custom Tag -2
40 . Best Practices in JSP
 
 
JSP Home
JSP Members (18705)
JSP Member Articles ( 11 )
JSP Discussion (71)
JSP Q & A ( 81 )
- JSP Ask Question
- JSP Questions
- JSP Unanswered Questions
JSP Resources
JSP Source Code (172)
JSP Articles (34)
JSP Blogs (330)
JSP Jobs (0)
JSP Components (10)
JSP Books (372)
JSP Websites (13)
JSP News (12)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Jsp Tutorial
 Session Tracking in JSP
  << Prev: Exception Handling in JSP-2 Next: Security in JSP -2 >>

In any web application, a user moves from one page to another. So, it is necessary to track the user's data and objects throughout the application. As we know that the session keeps the data particular to a user, so sessions are used to hold the user's data. For sessions JSP provide an implicit object "session", which can be use to save the data specific to the particular user into the session scope.


There are mainly 4 ways for session Tracking as follows:

1. Cookies:-
You can use HTTP cookies to store information about a session. The cookies are small text files stored in the client side. This is an excellent most widely used approach. However, even though servlets have a high-level and easy-to-use interface to cookies, there are still a number of relatively tedious details that need to be handled, while working with cookies.


2. URL Rewriting:
You can append some extra data on the end of each URL that identifies the session, and the server can associate that session identifier with data it has stored about that session. This is also an excellent solution, and even has the advantage that it works with browsers that don't support cookies or where the user has disabled cookies. However, it has most of the same problems as cookies, namely that the server-side program has a lot of straightforward but tedious processing to do.



3. Hidden form fields:
HTML forms have an entry that looks like the following: <INPUT TYPE="HIDDEN" NAME="session" VALUE="...">. This means that, when the form is submitted, the specified name and value are included in the GET or POST data. This can be used to store information about the session. However, it has the major disadvantage that it only works if every page is dynamically generated, since the whole point is that each session has a unique identifier.


4. Using session object in the JSP.


The implicit object represents the session. The information can be stored in the session for the current session by setAttribute() and then retrieve by getAttribute() method, whenever needed. The session objects stores at the server so it is advisable not to put heavy information in the session object.

To understand a session let us take a very simple example of getting the name from the user and saving it in the session, we will retrieve this name from session on next page and display on the page. For this, we will need 2 JSP pages.


 


Example:- 


 


Sessiontracking1.jsp



<% String action= request.getParameter("action");
if(action != null && action.equals("sub    mit")){
    String username=request.getParameter("    username");
    String password=request.getParameter("password");
    if(username != null && password != null && username.equals("visualbuilder") && pa    ssword.equals("test"))
    {    
        session.setAttribute("username",username);
        response.sendRedirect("sessiontracking2.jsp");
    }else{
        out.println("<h3>Wrong password!!!!</h3>");
    }
    }else{
%>

<html>
<form
action="sessiontracking1.jsp" method="post">
<input
type="hidden" name="action" value="submit">
   
Enter the user name :- <input type="text" name="username" ><br>
   
Enter the password :- <input type="password" name="password" ><br>
    <input
type="submit" name="submit" value="submit">
</form>
</html>

<%}%>


 


Sessiontracking2.jsp


    <h3>Welcome <%= session.getAttribute("username")%> To the Site</h3>


 


Output:-


 



  << Prev: Exception Handling in JSP-2 Next: Security in JSP -2 >>
Jsp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising