首先寫一個登錄界面----index.jspweb
<body> <form action="${pageContext.request.contextPath }/loginServlet" method="post"> 用戶名:<input name="uname"/><br/> 密碼:<input name="pwd" type="password"/><br/> <input type="submit" value="登陸"/> </form> </body>
點擊登錄,跳轉至----loginServletsession
@WebServlet(name="loginServlet",urlPatterns="/loginServlet") //Serclet3.0註解配置 public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("uname"); //獲取用戶名 String pwd = request.getParameter("pwd"); //獲取密碼 if("jack".equals(name) && "bdqn".equals(pwd)){ //判斷用戶名與密碼是否正確 request.getSession().setAttribute("name", name); //將name放到session做用域裏 request.getRequestDispatcher("online.jsp").forward(request, response); //跳轉至登陸成功界面 }else if ("tom".equals(name) && "123".equals(pwd)) { //第二個用戶 request.getSession().setAttribute("name", name); request.getRequestDispatcher("online.jsp").forward(request, response); }else { request.getRequestDispatcher("401.jsp").forward(request, response); //若是未成功登陸,跳轉至401界面,可返回從新登陸 } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
未成功登錄----401.jspapp
<body> 請先登陸,點擊返回<a href="${pageContext.request.contextPath }/index.jsp">登陸</a>頁面 </body>
成功登陸---online.jspjsp
<body> 登陸成功!當前在線人數:${applicationScope.num } <!-- 監聽器獲取在線人數 --> <a href="${pageContext.request.contextPath }/logoutServlet">退出</a> </body>
退出處理----LogoutServletide
@WebServlet(name="logoutServlet",urlPatterns="/logoutServlet") public class LogoutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().removeAttribute("name"); request.getRequestDispatcher("index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
設置監聽器----首先須要在web.xml裏配置post
<listener> <listener-class>cn.bdqn.listener.DemoListemer</listener-class> </listener>
監聽器類 DemoListemer 繼承 HttpSessionAttributeListener 接口url
public class DemoListemer implements HttpSessionAttributeListener{ @Override public void attributeAdded(HttpSessionBindingEvent event) { int num=0; //定義初始在線人數爲0 ServletContext servletContext = event.getSession().getServletContext();//使用session來判斷是否爲註冊用戶 Object object = servletContext.getAttribute("num"); //獲取人數num if(object==null){ num=1; //object爲null,說明是第一個登陸的用戶 }else { num=(Integer)object+1; //若是不爲null,就在原人數上+1; } servletContext.setAttribute("num", num); } @Override public void attributeRemoved(HttpSessionBindingEvent event) { ServletContext servletContext = event.getSession().getServletContext(); Object object = servletContext.getAttribute("num"); //獲取人數num int num=(Integer)object-1; //退出登陸後人數-1 servletContext.setAttribute("num", num); //變動人數後設置給num } @Override public void attributeReplaced(HttpSessionBindingEvent event) { // TODO Auto-generated method stub } }
登陸成功頁面圖spa