Here is a simple session tracking example in servlets using Tomcat, our pretty pet. We are familiar with using cookies in servlets because we've already done an example and you should note that there are some drawbacks of cookies. One of the major drawbacks that strikes to your mind is they may not stored. Yes
What if cookies are not stored by the browser?
A. Session tracking to the rescue!
A session refers to all the requests made by a user to a web application. This session is unique for every user and is stored in the server itself. For every session, the default lifetime is 20 minutes which can be changed.
An ID will be generated which is usually look like this 4BABD9D86DFF9061DDC3DA4C8A9E2498 (this is just an example) which is unique and random. This is usually stored in a cookie or in some invisible input field, however the session is maintained by the web server.
We need to track all the user's requests made for a particular application, this phenomenon is called as Session Tracking. All the regarded information i.e. the track of user's requests is stored in the web server itself.
That's it. In this way we can work out with session tracking in servlets. Simple is that. Also see Cookies example in servlets
What if cookies are not stored by the browser?
A. Session tracking to the rescue!
A session refers to all the requests made by a user to a web application. This session is unique for every user and is stored in the server itself. For every session, the default lifetime is 20 minutes which can be changed.
An ID will be generated which is usually look like this 4BABD9D86DFF9061DDC3DA4C8A9E2498 (this is just an example) which is unique and random. This is usually stored in a cookie or in some invisible input field, however the session is maintained by the web server.
We need to track all the user's requests made for a particular application, this phenomenon is called as Session Tracking. All the regarded information i.e. the track of user's requests is stored in the web server itself.
Folder structure
Project path: D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\sessiont
Project structure
sessiont |_ index.html |_ WEB-INF |_ web.xml |_ classes |_ SessionExample.java |_ SessionExample.class |_ PrintAttribute.java |_ PrintAttribute.class
HTML File(s)
index.html
<html>
<head>
<title>Get a Hai for me</title>
</head>
<body>
<form action="http://localhost:8080/sessiont/mt" method="get">
<input type="text" name="user"/>
<input type="submit" value="Get"/>
</form>
</body>
</html>
Deployment Descriptor - web.xml
<web-app>
<servlet>
<servlet-name>myser</servlet-name>
<servlet-class>SessionExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myser</servlet-name>
<url-pattern>/mt</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>myser1</servlet-name>
<servlet-class>PrintAttribute</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myser1</servlet-name>
<url-pattern>/rd</url-pattern>
</servlet-mapping>
</web-app>
Servlet Programs
SessionExample.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SessionExample extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse res)throws ServletException,IOException
{
String st=req.getParameter("user");
HttpSession ses=req.getSession();
ses.setAttribute("uname",st);
res.sendRedirect(res.encodeURL("/sessiont/rd"));
}
}
PrintAttribute.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class PrintAttribute extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
HttpSession ses=req.getSession();
String st=(String)ses.getAttribute("uname");
pw.println("<h1> Hai "+st+"</h1>");
}
}
Output
The following screenshots show you sample output of this application.
Explanation
index.html
The HTML file consists of two input fields, one of type text and the other one of type SUBMIT. The name of the text type input field is user. Whenever user hits enter or click on the submit button, the form is submitted to the web server to the url http://localhost:8080/sessiont/mt and then the class associated with this URL is SessionExample.java. This get's executed.
web.xml
There is nothing to say about
web.xml
, it is understood and it's obvious. /mt URL is associated with SessionExample
and /rd with PrintAttribute classes.SessionExample.java
HttpSession ses=req.getSession();
The getSession()
method in HttpServletRequest class get's the session associated with the request (if there is no session new one will be created). This method returns the javax.servlet.http.HttpSession object.ses.setAttribute("uname",st);
The setAttribute() method takes a key value pair with uname as key (here) and st is the value. These attributes are very important and they carry the information. Here st contains the data that user has typed in the text field (input field of type text) with name as user. This is obtained from getParameter("user"); (see above).res.sendRedirect(res.encodeURL("/sessiont/rd"));
The sendRedirect() method in HttpServletResponse takes a String which contains the URL to be redirected to. Here I made a method call to encodeURL() which is present in HttpServletResponse and takes String as parameter. This parameter is the URL to be encoded. By calling this method and sending the parameter you can observe the jsessionid in the URL (see it in the 2nd output image).PrintAttribute.java
HttpSession ses=req.getSession():
The same thing as explained above. But here, the Session
is already created and now it is sent.String st=(String)ses.getAttribute("uname");
Here, the session is already created and now we can call getAttribute("uname")
which returns the value of the uname.That's it. In this way we can work out with session tracking in servlets. Simple is that. Also see Cookies example in servlets