標籤:extends prot 一個使用者 ide sys contex ima servlet 顯示
今天又重新學習了java web的基礎 監聽器技術,用sessionListener簡單的實現了網頁統計線上人數 (有一個bug就是當關閉網頁的時候 session不會關閉依舊存在,除非關閉伺服器),代碼如下(前端網頁代碼就不顯示了):
LoginServlet.java:
1 import javax.servlet.ServletException; 2 import javax.servlet.annotation.WebServlet; 3 import javax.servlet.http.HttpServlet; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpSession; 7 import java.io.IOException; 8 9 @WebServlet(name = "LoginServlet",urlPatterns = "/login")10 public class LoginServlet extends HttpServlet {11 @Override12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {13 String username=request.getParameter("username");14 HttpSession session=request.getSession();15 session.setAttribute("username",username);16 //重新定向到index.jsp17 response.sendRedirect("index.jsp");18 }19 20 @Override21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {22 23 }24 }
ExitServlet.java:
1 package servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 import java.io.IOException;10 11 @WebServlet(name = "ExitServlet",urlPatterns = "/exit")12 public class ExitServlet extends HttpServlet {13 @Override14 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {15 //從session中移除loginName參數16 response.getWriter().write("2222");17 HttpSession session=request.getSession();18 session.removeAttribute("username");19 //重新定向到login.jsp20 response.sendRedirect("login.jsp");21 }22 23 @Override24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {25 doPost(request, response);26 }27 }
OnlineListener.java:
1 package listener; 2 3 import bean.User; 4 5 import javax.servlet.ServletContextEvent; 6 import javax.servlet.ServletContextListener; 7 import javax.servlet.http.HttpSessionAttributeListener; 8 import javax.servlet.http.HttpSessionEvent; 9 import javax.servlet.http.HttpSessionListener;10 import javax.servlet.http.HttpSessionBindingEvent;11 12 @javax.servlet.annotation.WebListener()13 public class OnlineListener implements ServletContextListener,14 HttpSessionListener, HttpSessionAttributeListener {15 public OnlineListener() {16 }17 @Override18 public void contextInitialized(ServletContextEvent sce) {19 20 }21 @Override22 public void contextDestroyed(ServletContextEvent sce) {23 24 }25 @Override26 public void sessionCreated(HttpSessionEvent se) {27 System.out.println("會話已建立");28 }29 @Override30 public void sessionDestroyed(HttpSessionEvent se) {31 System.out.println("會話已銷毀");32 }33 34 @Override35 public void attributeAdded(HttpSessionBindingEvent sbe) {36 //向session範圍中添加一個使用者37 User.addUser(String.valueOf(sbe.getValue()));38 System.out.println("session增加屬性為"+sbe.getName()+"值為:"+sbe.getValue());39 }40 @Override41 public void attributeRemoved(HttpSessionBindingEvent sbe) {42 //將session會話中的使用者移除43 User.removeUser(String.valueOf(sbe.getValue()));44 System.out.println(sbe.getValue()+"屬性已被移除");45 }46 @Override47 public void attributeReplaced(HttpSessionBindingEvent sbe) {48 //擷取舊的屬性值49 String oldValue=String.valueOf(sbe.getValue());50 //擷取新的屬性值51 String newValue=String.valueOf(sbe.getSession().getAttribute(sbe.getName()));52 //移除舊的屬性,添加新的屬性53 User.removeUser(oldValue);54 User.addUser(newValue);55 }56 }
用兩個不同的瀏覽器運行結果如所示:
項目工程目錄如:
(堅持學習!努力加油!勉勵自己)
java web用監聽器listener簡單的實現線上統計人數