zoukankan      html  css  js  c++  java
  • 使用监听器监听用户访问页面的次数

    /**

    创建SesstionListener类实现  HttpSessionListener  接口   写如下代码

    */

    public void sessionCreated(HttpSessionEvent arg0) {
      ServletContext application =arg0.getSession().getServletContext();
      int count=1;
      if(application.getAttribute("count")!=null){
      count=(Integer)application.getAttribute("count");           //获取application的值          

      count++;                                                                           这一段就是判断application的值  如果不为空就获取count并且+1  在存到application中
      application.setAttribute("count", count);
    }
      application.setAttribute("count", count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
      ServletContext application =arg0.getSession().getServletContext();
      Integer count=(Integer)application.getAttribute("count");
      count--;
      application.setAttribute("count", count);
    }

    /**

    配置Eeb.XML文件   如下

    */

    <listener>
      <listener-class>cn.yct.Listeren.SessionAttListener</listener-class>     //包名.类名
    </listener>
    <listener>
      <listener-class>cn.yct.Listeren.SesstionListener</listener-class>         //包名.类名

    </listener>

    /**

    创建  CountServlet   继承  HttpServlet  实现她的方法   写如下代码

    */

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

      response.setContentType("text/html;charset=utf-8");
      PrintWriter out = response.getWriter();
      HttpSession session=request.getSession();        //创建Sessions对象
      String name=request.getParameter("name");      //获取登录的用户名
      if(name!=null){
        request.getRequestDispatcher("/Login/success.jsp").forward(request, response);     //跳转到登录成功的页面
        session.setMaxInactiveInterval(10);       //设置session的失效
    }

    /**

    主页面   登录的页面

    */

    <form action="CountServlet">    
       <table>
        <tr>
          <td>用户名:</td>
          <td><input type="text" name="name"></td>
        </tr>
        <tr>
          <td><input type="submit" value="提交"></td>
        </tr>
      </table>
    </form>

  • 相关阅读:
    linux sort根据日期时间排序方法记录
    gitlab数据迁移与升级记录
    ubuntu加压7z分卷
    docker环境运行elasticsearch以及汉化运行kibana
    nginx 403错误 检查nginx.conf user有没有问题,最好是当前用户
    系统属性file.encoding在JVM启动后,再次设置无法对系统的默认编码造成影响 & sun.jnu.encoding
    IDEA快速选择提示代码的设置
    log4j2可以打印到控制台,但无法打印到文件
    IDEA快捷键
    Eclipse自定义快捷键
  • 原文地址:https://www.cnblogs.com/y-c-t/p/8313473.html
Copyright © 2011-2022 走看看