1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import javax.servlet.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class C_TestListener implements ServletContextListener,HttpSessionListener
{
String Av_Test;
/*This method is invoked when the Web Application has been removed
and is no longer able to accept requests
*/
public void contextDestroyed(ServletContextEvent event)
{
//Output a simple message to the server's console
System.out.println("The Simple Web App. Has Been Removed");
}
//This method is invoked when the Web Application
//is ready to service requests
public void contextInitialized(ServletContextEvent event)
{
event.getServletContext().setAttribute("N_User",event.getServletContext().getInitParameter("Nom_Utilisateur"));
Av_Test=(String)event.getServletContext().getAttribute("N_User");
//Output a simple message to the server's console
System.out.println("The Simple Web App. Is Ready");
}
public void sessionCreated(HttpSessionEvent sessionEvent)
{
// Get the session that was created
HttpSession session = sessionEvent.getSession();
// Store something in the session, and log a message
try {
session.setAttribute("NUser",Av_Test);
System.out.println("Le nom d'utilisateur est : " + session.getAttribute("NUser"));
} catch (Exception e) {
System.out.println("Error in setting session attribute: " +
e.getMessage());
}
}
public void sessionDestroyed(HttpSessionEvent sessionEvent)
{
// Get the session that was invalidated
HttpSession session = sessionEvent.getSession();
// Log a message
System.out.println("Session invalidated: "+session);
}
} |