会话用户计数器无法正常工作

时间:2015-01-13 19:03:08

标签: java servlets

晚上好。 我正在尝试向我的网络应用添加一个功能,以跟踪有多少用户正在使用它。我尝试实现它,但到目前为止,没有结果。尽管打印机应该是" 1用户"但是打印机仍然打印出#34; 0"。我是Java和servlets的新手。所有帮助表示赞赏。

这是我的SessionCounter类:

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounterListener implements HttpSessionListener {

private int users = 0;

public int getTotalUsers() {
    return (users);
}

@Override
public void sessionCreated(HttpSessionEvent s) {

    if (users <= 0) {
        users++;
      System.out.println("Session created");
    }

  }

@Override
public void sessionDestroyed(HttpSessionEvent s) {

    if (users > 0) {
        users--;
        System.out.println("Session destroyed.");
    }

    }
  }

这是我的servlet中的代码,它应该在浏览器上显示用户数据:

public class ServletProjekt extends HttpServlet {
private static final long serialVersionUID = 1L;


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


    int requests = 0; 

    String firstName = request.getParameter("förnamn");
    String lastName = request.getParameter("efternamn");
    String address = request.getParameter("address");
    String email = request.getParameter("email");
    String howoften = request.getParameter("howoften");
    String newsabout = request.getParameter("news");
    String[] age = request.getParameterValues("age");

    requests++;
    request.getServletContext().log("Added a new request-log//.");

    // servern skapar objektet, inte vi. 
    HttpSession session = request.getSession();
    session.setAttribute("förnamn", firstName);
    session.setAttribute("efternamn", lastName);
    session.setAttribute("address", address);
    session.setAttribute("email", email);
    session.setAttribute("howoften", howoften);
    session.setAttribute("news", newsabout);
    session.setAttribute("age", age);

      session.invalidate();  //sessionDestroyed() is executed   


    Cookie firstname = new Cookie ("firstname", firstName);
    Cookie lastname = new Cookie ("lastname", lastName);
    Cookie Address = new Cookie ("Address", address);
    Cookie mail = new Cookie ("mail", email);
    Cookie HowOften = new Cookie ("HowOften?", howoften);
    Cookie news = new Cookie ("news", newsabout);

    firstname.setMaxAge(60 * 60 * 24);
    lastname.setMaxAge(60 * 60 * 24);
    Address.setMaxAge(60 * 60 * 24);
    mail.setMaxAge(60 * 60 * 24);
    HowOften.setMaxAge(60 * 60 * 24);
    news.setMaxAge(60 * 60 * 24);

    response.addCookie(firstname);
    response.addCookie(lastname);
    response.addCookie(Address);
    response.addCookie(mail);
    response.addCookie(HowOften);
    response.addCookie(news );


    SessionCounterListener scl = new SessionCounterListener ();


        response.setContentType("text/html");
        PrintWriter printWriter = response.getWriter();
        printWriter.write("<html>");
        printWriter.write("<h1> THANK YOU FOR YOUR INFORMATION</h1>");
        printWriter.write("<body>");
        printWriter.write("<p>Your name:</p>" + firstName);
        printWriter.write("<br>");
        printWriter.write("<p>Your last name:</p>" +lastName + "<br>");
        printWriter.write("<p>Your address:</p>" +address + "<br>");
        printWriter.write("<p>Your choice of often:</p>" +howoften + "<br>");
        printWriter.write("<p>Your choice of news:</p>" +newsabout + "<br>");
        printWriter.write("<p>Your email:</p>" +email + "<br>");
        printWriter.write("<p>Request number:</p>" +requests + "<br>");
        printWriter.write("Active users:" + scl.getTotalUsers() + "<br>");
        printWriter.write("");      



        for (String ages : age) {
            printWriter.write("<p>Hours:</p>" +ages + "<br>");
        }

        printWriter.write("<a href =\"GetAllCookies\">View All Cookies</a>");



        printWriter.write("</body>");
        printWriter.write("</html>");
        printWriter.close();

}

}

这是我的web.xml:

<web-app >
    <listener>
    <listener-class>SessionCounterListener</listener-class>
    </listener>
  </web-app>

1 个答案:

答案 0 :(得分:0)

我在ServletProjektSessionCounterListener课程中看到了以下问题。

1.更改您的SessionCounterListener类,如下所示(您正在创建此侦听器类的对象,该对象将为每个用户请求创建,因此每个用户都将拥有自己的用户属性,因此您永远不会得到总用户。)注意我已将user变量和getTotalUsers方法更改为static

  import javax.servlet.http.HttpSessionEvent;    
  import javax.servlet.http.HttpSessionListener;

  public class SessionCounterListener implements HttpSessionListener {
    private static int users = 0;
    public static int getTotalUsers() {
        return users;
    }
    @Override
    public void sessionCreated(HttpSessionEvent s) {
            users++;
            System.out.println("Session created");
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent s) {
            users--;
            System.out.println("Session destroyed.");
    }
}

2.我不理解维护会话的目的,因为您正在同一页面中创建和销毁会话。您的ServletProjekt类中有以下行,

    HttpSession session = request.getSession();

因此,您已为用户创建了会话,并且调用了sessionCreated(),因此用户会增加到 1

在此行之后,您将向会话范围添加一些属性。

然后您使会话无效     session.invalidate();

现在,用户将0 sessionDestroyed() invalidate上调用SessionCounterListener

然后您使用了printWriter.write("Active users:" + scl.getTotalUsers() + "<br>"); 类来打印活动用户,对于这种情况,该用户将 0

SessionCounterListener

在第1步更改后,您需要创建getTotalUsers()类对象,这样您就可以通过调用SessionCounterListener.getTotalUsers(); 来计算总用户数。

{{1}}