diff options
Diffstat (limited to 'test/java/session_listeners')
-rw-r--r-- | test/java/session_listeners/app.java | 80 | ||||
-rw-r--r-- | test/java/session_listeners/web.xml | 14 |
2 files changed, 94 insertions, 0 deletions
diff --git a/test/java/session_listeners/app.java b/test/java/session_listeners/app.java new file mode 100644 index 00000000..603cc932 --- /dev/null +++ b/test/java/session_listeners/app.java @@ -0,0 +1,80 @@ + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionAttributeListener; +import javax.servlet.http.HttpSessionBindingEvent; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionIdListener; +import javax.servlet.http.HttpSessionListener; + +@WebServlet(urlPatterns = "/") +public class app extends HttpServlet implements + HttpSessionListener, + HttpSessionIdListener, + HttpSessionAttributeListener +{ + private static String session_created = ""; + private static String session_destroyed = ""; + private static String session_id_changed = ""; + private static String attribute_added = ""; + private static String attribute_removed = ""; + private static String attribute_replaced = ""; + + @Override + public void sessionCreated(HttpSessionEvent se) + { + session_created += se.getSession().getId(); + } + + @Override + public void sessionDestroyed(HttpSessionEvent se) + { + session_destroyed += se.getSession().getId(); + } + + @Override + public void sessionIdChanged(HttpSessionEvent event, String oldId) + { + session_id_changed += " " + oldId + "->" + event.getSession().getId(); + } + + @Override + public void attributeAdded(HttpSessionBindingEvent event) + { + attribute_added += event.getName() + "=" + event.getValue(); + } + + @Override + public void attributeRemoved(HttpSessionBindingEvent event) + { + attribute_removed += event.getName() + "=" + event.getValue(); + } + + @Override + public void attributeReplaced(HttpSessionBindingEvent event) + { + attribute_replaced += event.getName() + "=" + event.getValue(); + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException + { + HttpSession s = request.getSession(); + s.setAttribute("var1", request.getParameter("var1")); + + response.addHeader("X-Session-Id", s.getId()); + response.addHeader("X-Session-Created", session_created); + response.addHeader("X-Session-Destroyed", session_destroyed); + response.addHeader("X-Attr-Added", attribute_added); + response.addHeader("X-Attr-Removed", attribute_removed); + response.addHeader("X-Attr-Replaced", attribute_replaced); + } +} diff --git a/test/java/session_listeners/web.xml b/test/java/session_listeners/web.xml new file mode 100644 index 00000000..aedfe175 --- /dev/null +++ b/test/java/session_listeners/web.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8" ?> + +<web-app 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" + version="3.0"> + <listener> + <listener-class>app</listener-class> + </listener> + <listener> + <listener-class>app</listener-class> + </listener> +</web-app> + |