Guest User

Untitled

a guest
Jan 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. public class TestServlet extends HttpServlet {
  2.  
  3. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  4. doPost(request,response);
  5. }
  6.  
  7. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  8. response.setContentType("text/html");
  9. PrintWriter out = response.getWriter();
  10. HttpSession session = request.getSession();
  11. Dog d = new Dog();
  12. d.setName("Peter");
  13. session.setAttribute("test", d);
  14. /*Dog d1 = new Dog();
  15. d1.setName("Adam");
  16. */
  17. d.setName("Adam");
  18. session.setAttribute("test",d);
  19. }
  20.  
  21. }
  22.  
  23. public class MyAttributeListener implements HttpSessionAttributeListener {
  24. @Override
  25. public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
  26. System.out.println("Attribute Added");
  27. String attributeName = httpSessionBindingEvent.getName();
  28. Dog attributeValue = (Dog) httpSessionBindingEvent.getValue();
  29. System.out.println("Attribute Added:" + attributeName + ":" + attributeValue.getName());
  30.  
  31. }
  32.  
  33. @Override
  34. public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
  35. String attributeName = httpSessionBindingEvent.getName();
  36. String attributeValue = (String) httpSessionBindingEvent.getValue();
  37. System.out.println("Attribute removed:" + attributeName + ":" + attributeValue);
  38.  
  39.  
  40. }
  41.  
  42. @Override
  43. public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
  44. String attributeName = httpSessionBindingEvent.getName();
  45. Dog attributeValue = (Dog) httpSessionBindingEvent.getValue();
  46. System.out.println("Attribute replaced:" + attributeName + ":" + attributeValue.getName());
  47.  
  48. }
  49. }
  50.  
  51. public class Dog {
  52. private String name ;
  53.  
  54. public String getName() {
  55. return name;
  56. }
  57.  
  58. public void setName(String name) {
  59. this.name = name;
  60. }
  61. }
  62.  
  63. d.setName("Adam")
Add Comment
Please, Sign In to add comment