httpsessionlistenertest,出现一个奇怪的错误

sycxhyv7  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(366)

更新。我使用了另一种方法,它有一个私有的静态int total。我在sessioncreated()时增加总数,在sessiondestroyed()时减少sessioncounter。目标是让servlet打印出web应用上有多少活跃用户。代码不起作用,我得到的总数为0。
我遇到的另一个问题是,请求变量也不起作用,这是次要的,也不太重要。它负责在每次调用servlet时递增,并打印出您访问页面的次数。
这是我的httpsessionlistenertest类:

public class HttpSessionListenerTest implements HttpSessionListener {

private static int total = 0;

public HttpSessionListenerTest() {
    // TODO Auto-generated constructor stub
}

public static int getTotal(){
    return total;
  }

/**
 * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
 */
public void sessionCreated(HttpSessionEvent e)  { 
     // TODO Auto-generated method stub

             total++;

}

/**
 * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
 */
public void sessionDestroyed(HttpSessionEvent e)  { 
     // TODO Auto-generated method stub

        total--; 

}

这是我的主要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; 
    requests++;
    request.getServletContext().log("Added a new request-log//abdi.");

    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");

    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);

        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>You have visited this page this many times:</p>" +requests + "<br>");**       
        printWriter.write("");

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

      **printWriter.write("Active users:" + HttpSessionListenerTest.getTotal() + "<br>");**

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

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

}
}
4dc9hkyq

4dc9hkyq1#

请在这里查看本教程。
在本教程中,他试图计算用户数,为此他使用了一个静态变量,以便可以在所有用户之间共享,如下所示:

private static int totalActiveSessions;

希望有帮助。

9udxz4iz

9udxz4iz2#

getattribute(字符串名称)可以返回 null 如果没有指定这样的属性 name . 既然你想给一个 Integer 到原始的 int ,jvm无法转换 null 到一个基元类型,所以得到一个“nullpointerexception”。
一个简单的解决方案:

ServletContext ctx=getServletContext();  
Integer users =(Integer) ctx.getAttribute("totalusers");
if (users == null) {
   users = 0;
}

另外,你的手机也有问题 HttpSessionListenerTest . 没有活动会话计数的引用。过滤器在每个事件和之后都会被调用 total 不是一成不变的,恐怕永远都是 0 (需要确认)。因此,您可能会得到一个值,该值在会话中只有一个用户,而计数器仅依赖于该值 total . 下面是一个很好的解决方法:

@WebListener
public class HttpSessionListenerTest implements HttpSessionListener {

    /**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent e)  { 
         // TODO Auto-generated method stub

        SessionContext ctx = e.getSession().getServletContext();
        Integer totalUsers = cxt.getAttribute("totalusers");
        if (totalUsers == null) {
            totalUsers = 0;
        } else {
            totalUsers += 1;
        }

        ctx.setAttribute("totalusers", totalUsers);  
    }

    /**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent e)  { 
         // TODO Auto-generated method stub

        SessionContext ctx = e.getSession().getServletContext();
        Integer totalUsers = cxt.getAttribute("totalusers");
        if (totalUsers == null) {
            totalUsers = 0;
        } else {
            totalUsers -= 1;
        }

        ctx.setAttribute("totalusers", totalUsers); 
    }
}

另一种方法是有一个 private static int sessionCounter . 增加 sessionCountersessionCreated() 和减量 sessionCountersessionDestroyed() .

相关问题