gpt4 book ai didi

java - session 用户计数器不工作

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:24 25 4
gpt4 key购买 nike

晚上好。我正在尝试向我的网络应用程序添加一个功能,以跟踪有多少用户正在使用它。我尝试实现它,但到目前为止,还没有结果。打印机不断打印出“0”,尽管它应该是“1 个用户”。我对 Java 和 servlet 很陌生。感谢所有帮助。

这是我的 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>

最佳答案

我在您的 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.我不明白维护 session 的目的,因为您正在同一页面中创建和销毁 session 。您的 ServletProjekt 类中有以下几行,

    HttpSession session = request.getSession();

因此,您已经为用户创建了 session ,并且调用了sessionCreated(),因此用户增量为1

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

那么你已经使 session 失效了 session.invalidate();

现在,当 invalidate() 上调用 sessionDestroyed() 时,用户将变为 0

然后您使用了 SessionCounterListener 类来打印 Activity 用户,在本例中将为 0

printWriter.write("Active users:" + scl.getTotalUsers() + "<br>");

第 1 步更改后,您确实需要创建 SessionCounterListener 类对象,因此您可以通过调用 getTotalUsers() 来计算总用户数。

SessionCounterListener.getTotalUsers();

关于java - session 用户计数器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929580/

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