关闭

javaEE - session简单应用-在线人数统计

标签: session应用class
108人阅读 评论(0) 收藏 举报
分类:

LoginAction

@WebServlet("/LoginAction")
public class LoginAction extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public LoginAction() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession ss = request.getSession() ;
        ss.setAttribute("name", "testname");
    }



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

LoginOutAction

@WebServlet("/LoginOutAction")
public class LoginOutAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginOutAction() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.getWriter().write("loginout success");
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

SessionListener

@WebListener
public class SessionListener implements HttpSessionListener{
    private static int tongji ;
    public  SessionListener() {
    }

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        tongji++;
        System.out.println(event.getSession().getId()+" sessionCreated");
    }


    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        tongji--;
        System.out.println(event.getSession().getId()+" sessionDestory");
    }

    public int tongji(){
        return tongji;
    }

}

TongjiAction

@WebServlet("/Tongji")
public class TongjiAction extends HttpServlet {
    private static final long serialVersionUID = 1L;
     private  static SessionListener sessionListener = new SessionListener();

    public TongjiAction() {


    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("tongji", sessionListener.tongji());
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7893次
    • 积分:421
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    我的微信公众号:IT互汇
      我的微信公众号

      为你推荐最新的博文
      更有惊喜等着你
    文章分类