Checking Status of Session

Checking Status of Session


Posted in : Java Posted on : June 24, 2011 at 7:19 PM Comments : [ 0 ]

In this section we will discuss about checking the status of the session whether it is newly created or already existed.

Checking Status of Session

In this section we will discuss about checking the status of the session whether it is newly created or already existed.

In the example given below I used the isNew() method with HttpSession object. This method checks whether the session is created newly or it is  pre existing.

Example :

CheckSessionStatus.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckSessionStatus extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
HttpSession ssn = req.getSession();
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
if(ssn.isNew())
{
pw.println("A new session is created");
}
else
{
pw.println("The session is already existed");
}

}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SessionStatus</display-name>
<servlet>
<servlet-name>CheckSessionStatus</servlet-name>
<servlet-class>CheckSessionStatus</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckSessionStatus</servlet-name>
<url-pattern>/CheckSessionStatus</url-pattern>
</servlet-mapping>
</web-app>

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics