java - HttpAttributeListener 不起作用?

标签 java jsp servlets web

我正在尝试在我的网络应用程序中实现 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/

相关文章:

java - EL session 对象属性

java - Struts2 - OGNL 参数化名称

java - 单例和同步Servlet

java - 如何循环访问数据库并打印表中的所有项目

java - 如何仅删除文本中的标题

java - 有没有办法在java中截断字符串数组(不使用循环)并直接转换为整数?

不使用注释的 Java 代码到 XML/XSD

java - 使用 Lingpipe 进行词级语言模型

javascript - 单击后所有图像对象在一个 css 中更改

java - 使用 poi 转换 ms word 2007