我正在尝试在我的网络应用程序中实现 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>
我是一名优秀的程序员,十分优秀!