Tracking user actions with Sessions - Overview

Chad Darby
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 482,277 students

Learn more from the full course

JSP, Servlets and JDBC for Beginners: Build a Database App

JSP: Covers JSP 2.3 and Servlets 3.1 - Most Popular JSP/Servlet course

08:39:14 of on-demand video • Updated July 2021

  • Build a fully functioning JSP and Servlets web application from scratch
  • Set up your JSP/Servlet development environment with Tomcat and Eclipse
  • Explore JSP scripting elements: Expressions, Scriptlets and Declarations
  • Read HTML form data with JSP and Servlets
  • Manage application state with Sessions to track unique user actions
  • Leverage Cookies to personalize a web site for a specific user
  • Integrate the JSP Standard Tag Library (JSTL) to minimize scriptlet code
  • Build a Multi-Lingual app using Internationalization and JSTL
  • Build an MVC application with JSP and Servlets
  • Generate HTML Tables with JSP and Servlets
  • Add database support with JDBC: query, insert, update and delete
  • Build a complete database web app with JDBC
English Instructor: Hello, in this video I'm gonna show you how to use session tracking with JSP. Now, we'll cover the following topics. First off, I'll give you a session tracking demo and then we'll look at the session coding steps and then finally, we'll pull it altogether with a full JSP page example. Now, for this video I'll actually break it up into two separate videos, I'll cover just the slides in part one and then I'll get into the coding portion in part two. All right, so what exactly is the JSP session object? Well, the session object is created once for a browser session and it's unique for a given user. So, you commonly use this to keep track of a user's actions. So, if you can think of like an online banking application or a shopping cart application, or maybe like an online exam application, for every user that's accessing the web application, you need to keep track of those unique actions and those actions specific to that given user. That's the idea of a session, so I always say a session, think of it as a shopping cart for a unique user. Now, I'm gonna actually show you a demo of using JSP sessions, a very simple demo where we'll manage a to-do list. A user will have a list of items stored in the session and then each user will have their own list for tracking and I'll show it to you in a nice little demo coming up here in a second. All right, so here's our demo, so I'm in my web browser. Up top you can add new items to your list, so think of it as like your to-do list for today and you can just enter some items, so here I'll say I need to go buy groceries and I'll hit Submit. That will add it to my list, so right now that's in my session. Next one here, go to the gym and then I can just add some additional items here, mail letters and so on. You get the idea. So, basically here this browser here actually has a unique session associated with it in the server's memory. Now, let me bring in another browser here just to simulate a different user, so let me just kind of resize the window here, so I have Firefox on the left and I'll bring in a Chrome browser on the right. All right, so here's my Chrome browser, so Firefox on the left, Chrome on the right. I go to that same URL, I'll increase the font here for a second and now in the Chrome browser on the right I'll add some items also, so again, remember, each browser user has a unique session, so here the first item I'll enter go running, hit Submit and then another one here, repair bike and so on and I can keep adding items. So, the key here is that each browser has its own unique session object in the background, so it's almost like a shopping cart, each user has their own shopping cart and the system can keep track of it, so I can go back over here on the left-hand side in Firefox, since I'm recording all these Java videos, I need to go off and record more videos, so anyway, the basic idea here is that each browser has a unique session object on the Tomcat server in memory and you can keep track of each user's individual actions, so on the next slide here I'll show you kind of how it works as far the big picture and then we'll get into some of the coding portion of it. All right, so here's the big picture. So, you can have multiple users of your application. They're all accessing maybe one JSP or whatever, that JSP can make use of a session object, so each user of the system will have their own session object. Now, a couple things here, the session object is kept in memory, so it's not stored on a database or it's not stored on the file system, it's only in your server's memory like in the Tomcat server memory and each user has their own session ID, so the Tomcat server will actually assign a session ID to the user and Tomcat has its own algorithm for assigning but the key thing is that you the developer you don't have to worry about the session ID generation, you also don't have to worry about passing a session ID back and forth, the Tomcat server and the browser will handle that for you automatically, so in this slide here, we have three users, Charles, Donald and Dawn and they each have their own session object in the Tomcat server memory. All right, so let's look at some of the coding mechanics here like how do we add data to the session object? Well, the method signature is session.setAttribute, so you make use of the built-in session object and you call setAttribute and you give the name and the value, so the name is basically just a label and then the value is any object that you want to place in the session. So, here's a coding example, in this example I want to place an ArrayList into the session, so here I create the list upfront and then I say session.setAttribute, I give the name of myToDoList, so that's the name, and you can use any name you want, comma items and that's that ArrayList object that I have. Now you can place any type of data here. You can place ArrayList, your own custom employee objects, strings, any type of object you can place inside of the JSP session object. All right, now the next thing here is how do you retrieve data from the session object? So, the method signature is session.getAttribute and you give the name of the item that you're retrieving and it's going to return you a plain object, so here's a coding example of how to do this. So, here I'll start on the right-hand side, I'll say session.getAttribute, I give myToDoList, that's the same name I used in the previous slide. It's gonna return me an object. Now what I wanna do is down cast it to the appropriate type of list<String> and then I can assign it on the left-hand side, so a variable my stuff, so you basically say session.getAttribute and you use the name of the object that you want to reference and it will return it back to you. And again, remember, you'll have to down cast it to the appropriate type if you want to use that later on in your page. Okay, great, now there's also some other useful methods that you can use on a JSP session object. This is not the full list. You can go to the full Java doc to get all the details but for right now you check this first method here is isNew. You can find out if it's a new true or false. You can also get the session ID just by calling getId and this is useful if you wanted to maybe log some information in your server logs, or maybe log something in the database. You can also call invalidate, so if you want to get rid of a user session object like say for example, a user logs out, then you can call session.invalidate and that will basically clear up that user's session and then finally, you may wonder well, how long does a session stay around in memory? Well, basically it stays around for a certain idle time period or an inactive interval. So, by default, a Tomcat server, the inactive interval is set for 30 minutes but it varies by server, so you need to check your server documentation for that but you can explicitly set the inactive interval, so like if you're doing like an online banking, most banks are very aggressive and they say only five minutes, so you can simply setMaxInactiveInterval and you pass in the value. The one thing to be aware of here is that this method takes the value in milliseconds, so you have to give the appropriate number of milliseconds there for your inactive interval. And that's it. All right, so I kind of covered a high level here as far as the mechanics we're using JSP session objects, so in the next video we'll get our hands dirty with all the full coding on the JSP session exam. I'll see you then.