HttpSessionListener测试,得到一个奇怪的错误

时间:2015-01-15 09:10:48

标签: java jsp servlets web

更新。我用的是其他私有静态int总数。我在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();

}
}

2 个答案:

答案 0 :(得分:1)

请查看本教程here

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

private static int totalActiveSessions;

希望有助于。

答案 1 :(得分:0)

如果没有指定null的此类属性,

ServletContext.getAttribute(String name)可以返回name。由于您尝试将Integer自动装箱到原始int,因此JVM无法将null转换为基本类型,因此您将获得`NullPointerException

一个简单的解决方法:

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

此外,您的HttpSessionListenerTest存在问题。活动会话计数的引用。每个事件都会调用过滤器,因为total不是静态的,我担心它总是0(需要确认)。因此,您可能会得到一个只有1个用户在会话中的值,而您的计数器仅取决于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。在sessionCounter之后递增sessionCreated(),在sessionCounter之后递减sessionDestroyed()