gpt4 book ai didi

java - HttpAttributeListener 不起作用?

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

我正在尝试在我的网络应用程序中实现 HttpAttributeListener。我尝试了所有方法并查看了我的代码十几次,但它不起作用。 MyAttribute 中的 sysout 应该执行,但它没有执行。我的代码有问题,但我没有看到它。请帮忙。

这是我的 HttpAttributeListenerClass:

public class MyAttribute implements HttpSessionAttributeListener{

@Override
public void attributeAdded(HttpSessionBindingEvent event) {
String attributeName = event.getName();
Object attributeValue = event.getValue();
System.out.println("##########################################");
System.out.println("Attribute added : " + attributeName + " : " + attributeValue);
System.out.println("##########################################");
}

@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
String attributeName = event.getName();
Object attributeValue = event.getValue();
System.out.println("##########################################");
System.out.println("Attribute removed : " + attributeName + " : " + attributeValue);
System.out.println("##########################################");
}

@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
String attributeName = event.getName();
Object attributeValue = event.getValue();
System.out.println("##########################################");
System.out.println("Attribute replaced : " + attributeName + " : " + attributeValue);
System.out.println("##########################################");
}
}

这是我的 servlet:

@WebServlet("/ServletProjekt")
public class ServletProjekt extends HttpServlet {
private static final long serialVersionUID = 1L;


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

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

HttpSession session = request.getSession();
session.setAttribute("test", "test");
}

这是我的 web.xml

<listener>
<listener-class>
Test.HttpSessionListenerTest
</listener-class>
</listener>

最佳答案

我认为web.xml中提到的监听器类不正确。它应该是MyAttribute而不是HttpSessionListenerTest

<listener>
<listener-class>
MyAttribute //provide full package name like (com.XXX.MyAttribute)
</listener-class>
</listener>

关于java - HttpAttributeListener 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981709/

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