Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. @WebServlet("/HelloWorld")
  2. public class HelloWorld extends HttpServlet {
  3. private static final long serialVersionUID = 1L;
  4.  
  5.  
  6. @Override
  7. public void init(ServletConfig config) throws ServletException {
  8. super.init(config);
  9. getServletContext().setAttribute("applicationHits", new AtomicInteger(0));
  10. }
  11.  
  12.  
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14.  
  15. System.out.println("get");
  16.  
  17. ((AtomicInteger) request.getServletContext().getAttribute("applicationHits")).incrementAndGet();
  18. ((AtomicInteger) request.getSession(true).getAttribute("sessionHits")).incrementAndGet();
  19. request.setAttribute("requestHits", 0);
  20.  
  21. getServletContext().getRequestDispatcher("/view/HelloWorld.jsp").forward(request, response);
  22. }
  23.  
  24. }
  25.  
  26. @WebListener
  27. public class SessionListener implements HttpSessionListener {
  28.  
  29. public SessionListener() {
  30. }
  31.  
  32. public void sessionCreated(HttpSessionEvent arg0) {
  33. System.out.println("session listener");
  34. arg0.getSession().setAttribute("sessionHits", new AtomicInteger(0));
  35. }
  36.  
  37. public void sessionDestroyed(HttpSessionEvent arg0) {
  38. }
  39.  
  40. }
  41.  
  42. ((AtomicInteger) request.getSession(true).getAttribute("sessionHits")).incrementAndGet();
  43. request.setAttribute("requestHits", 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement