Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Use Session to Track User in JSP 
 

ession tracking is a mechanism that is used to maintain state about a series of requests from the same user(that is, requests originating from the same browser) across some period of time.

 

Use Session to Track User in JSP

                         

This is detailed java code to track user in application by using session.

Session Tracking:  Session tracking is a mechanism that is used to maintain state about a series of requests from the same user(that is, requests originating from the same browser) across some period of time. A session id is a unique token number assigned to a specific user for the duration of that user's session.

Methods to work with session :

1. Cookies
2. URL Rewriting
3. Hidden Fields
4. Session API

This example explains creating session and using session information with the help of session API. JSP provides an implicit object called session which can be used to work with session. You can set and get attributes to the session with the help of setAttribute() and getAttribute() methods respectively. Other session information (session id, session creation time, session last accessed time etc.) can also found using appropriate methods. We have created session_track_jsp.jsp page which will help you to understand how to work with session.

session_track_jsp.jsp

<!--session declaration makes the session implicit 
variable available to a JSP page.-->
<%@page import = "java.util.*" session="true"%>
<HTML> 
    <HEAD>
        <TITLE>Using Sessions to Track Users 
        through JSP Code</TITLE>
    </HEAD> 
    <BODY bgcolor="#084B8A">
      <font size="+3" color="#E6E6E6"><br>track 
      user using session</font>
      <TABLE style="background-color: #CEF6F5;" border="1">
        <%
	/* here if counter has not been set before, 
        getAttribute will return a value of null. That means
        you can create the counter value, or increment it if 
        it already exists, and store the new value in the 
        session object*/
        Integer counter =  (Integer)session.getAttribute("counter");
        if (counter == null) {
            counter = 1;
        } 
		else {
            counter = counter+1;
        }
	/* setAttribute() binds counter value to this session, 
        using the specified name here "counte".*/
        session.setAttribute("counter", counter);
        %>
        <!-- use getId() method to get a unique session id -->
        <tr><th>Session ID</th><td><%=session.getId()%></td></tr>
	<!-- getCreationTime() method returns date and time 
        when session was created -->
        <tr><th>Session creation time</th><td><%=new 
        Date(session.getCreationTime())%></td></tr>
	<!--getlastAcccessTime() method returns date and time of 
        last access by this session id-->
        <tr><th>Last accessed time</th><td><%=new 
        Date(session.getLastAccessedTime())%></td></tr>
	<!-- this counter variable will print how much time user 
        visited in application -->
	<tr><th>visited</th><td><%=counter%> times</td></tr>
    </BODY> 
</HTML>

Save this code as a .jsp file named "session_track_jsp.jsp" in your application directory ('user' in our example) in Tomcat and run this jsp page with url http://localhost:8080/user/session_track_jsp.jsp in address bar of the browser.

When user refresh this page then page will reload and visited column shows that page is accessed one more time.

Download Source Code

                         

» View all related tutorials
Related Tags: java c string jsp ide class strings script object io objects method get char ip using sequence id declaration length

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.