/*MySessionListener.java*/
package com.rajendra.servlets;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener
{
public MySessionListener() {
}
public void sessionCreated(HttpSessionEvent sessionEvent) {
// Get the session
HttpSession session = sessionEvent.getSession();
try {
System.out.println("Session created: "+session);
session.setAttribute("foo","bar");
} 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);
System.out.println("The name is: " + session.getAttribute("foo"));
}
}
/*ServletSessionListener.java*/
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServletSessionListener extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession session= request.getSession();
String str = (String)session.getAttribute("foo");
pw.println("The name is " + str);
}
}
package com.rajendra.servlets;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener
{
public MySessionListener() {
}
public void sessionCreated(HttpSessionEvent sessionEvent) {
// Get the session
HttpSession session = sessionEvent.getSession();
try {
System.out.println("Session created: "+session);
session.setAttribute("foo","bar");
} 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);
System.out.println("The name is: " + session.getAttribute("foo"));
}
}
/*ServletSessionListener.java*/
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServletSessionListener extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession session= request.getSession();
String str = (String)session.getAttribute("foo");
pw.println("The name is " + str);
}
}
- maintain the folder structure
- write the web.xml file
- \
No comments:
Post a Comment