Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
- doPost(request,response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- HttpSession session = request.getSession();
- Dog d = new Dog();
- d.setName("Peter");
- session.setAttribute("test", d);
- /*Dog d1 = new Dog();
- d1.setName("Adam");
- */
- d.setName("Adam");
- session.setAttribute("test",d);
- }
- }
- public class MyAttributeListener implements HttpSessionAttributeListener {
- @Override
- public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
- System.out.println("Attribute Added");
- String attributeName = httpSessionBindingEvent.getName();
- Dog attributeValue = (Dog) httpSessionBindingEvent.getValue();
- System.out.println("Attribute Added:" + attributeName + ":" + attributeValue.getName());
- }
- @Override
- public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
- String attributeName = httpSessionBindingEvent.getName();
- String attributeValue = (String) httpSessionBindingEvent.getValue();
- System.out.println("Attribute removed:" + attributeName + ":" + attributeValue);
- }
- @Override
- public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
- String attributeName = httpSessionBindingEvent.getName();
- Dog attributeValue = (Dog) httpSessionBindingEvent.getValue();
- System.out.println("Attribute replaced:" + attributeName + ":" + attributeValue.getName());
- }
- }
- public class Dog {
- private String name ;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- d.setName("Adam")
Add Comment
Please, Sign In to add comment