Thursday, April 12, 2012

Session Listeners in Apache Tomcat

To implement a SessionListener in java we implement the javax.servlet.http.HttpSessionListener interface.

Here is our implementation of the HttpSessionListener

CountSessionListener
package servletpackage;
import java.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CountSessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent event)
{
HttpSession session=event.getSession();
try
{

String id=session.getId();
java.util.Date d=new java.util.Date();
System.out.println("Session Created with ID=" + id  + " at  time " + d);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public void sessionDestroyed(HttpSessionEvent event)
{
HttpSession session=event.getSession();
try
{
String id=session.getId();
java.util.Date d=new java.util.Date();
System.out.println("Session Destroyed with ID=" + id  + " at  time " + d);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}

To redirect the Standard Output in Apache Tomcat we use the logging tab in the Tomcat Monitor.




To install this session listener the following entries are required in the web.xml file.

web.xml

<web-app>
<listener>
<listener-class>servletpackage.CountSessionListener</listener-class>
</listener>

<session-config>
<session-timeout>2</session-timeout>
</session-config>
</web-app>

The underlined portion is the session listener configuration. We have also set a session timeout of 2 minutes.

To test the listener create two jsp pages. hello.jsp, and bye.jsp. hello.jsp creates a session while bye.jsp destroys it.

Here are the two files:-

hello.jsp

<%
request.getSession(true);
%>

bye.jsp

<%
session.invalidate();
%>



Open the two files and refresh them alternately in the browser.

Here is the output in the out.txt file where the System.out is redirected.



2012-04-12 14:01:32 Commons Daemon procrun stdout initialized
Session Created with ID=EA0AEFF5335E7F8E9B59F99FBDA5157D at  time Thu Apr 12 14:01:44 IST 2012
Session Destroyed with ID=EA0AEFF5335E7F8E9B59F99FBDA5157D at  time Thu Apr 12 14:02:05 IST 2012
Session Created with ID=2BE4BD1A6EFAA45785F63EB5991B46D1 at  time Thu Apr 12 14:24:04 IST 2012
Session Destroyed with ID=2BE4BD1A6EFAA45785F63EB5991B46D1 at  time Thu Apr 12 14:24:04 IST 2012
Session Created with ID=2BFF301ED2C87299CB0E75F51323340D at  time Thu Apr 12 14:24:06 IST 2012

Exchanging the value of Variables

In this post we shall look at the various ways of interchanging the values of variables. We shall not be looking into methods involving loops, but only simple expression based methods.

Essentially, any binary operation which has an inverse can be used as a basis for the exchange. Thus, we shall use the assignment operator, addition, subtraction, multiplication and division. The last two will require the divisor to be non zero.


Here is the program which does this exchange.

Exchange.c

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a=15,b=10,t;
clrscr();
printf("Original values a=%f,b=%f",a,b);
printf("\nSwap using a temporary variable\n");
t=a;a=b;b=t;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a substraction\n");
a=a-b;b=a+b;a=b-a;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a addition\n");
a=a+b;b=a-b;a=a-b;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a multiplication\n");
a=a*b;b=a/b;a=a/b;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a division\n");
a=a/b;b=a*b;a=b/a;
printf("Swapped values a=%f,b=%f",a,b);
getch();
}
This is the output of the program

Original values a=15.000000,b=10.000000
Swap using a temporary variable
Swapped values a=10.000000,b=15.000000
Swap using a substraction
Swapped values a=15.000000,b=10.000000
Swap using a addition
Swapped values a=10.000000,b=15.000000
Swap using a multiplication
Swapped values a=15.000000,b=10.000000
Swap using a division
Swapped values a=10.000000,b=15.000000


Rotation of values
Rotation of values a<--b, b<-- c, c<-- a

This is accomplished through the following program code.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b=2,c=3,t;
clrscr();
printf("Original values a=%d,b=%d,c=%d\n",a,b,c);
t=a;
a=b;
b=c;
c=t;
printf("Rotated values a=%d,b=%d,c=%d\n",a,b,c);
getch();
}

Here is the output of this program

Original values a=1,b=2,c=3
Rotated values a=2,b=3,c=1


Exchanging two elements via a temporary variable is a special case of the general rotation problem. As an interesting aside we can see that for n elements to be rotated, n rotations would give back the original set.