gpt4 book ai didi

java - HttpSessionListener 测试,出现奇怪的错误

转载 作者:行者123 更新时间:2023-12-01 12:01:29 25 4
gpt4 key购买 nike

更新。我使用了其他的方法,即拥有一个私有(private)静态 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();

}
}

最佳答案

请查看本教程here .

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

private static int totalActiveSessions;

希望有帮助。

关于java - HttpSessionListener 测试,出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com