What's wrong with my listener in my web.xml?

假装没事ソ 提交于 2019-12-24 11:13:05

问题


We're using JBoss 4.22 with portlets. When the listener tag is not in the web.xml the portlet loads, but the listener sessionDestroyed() is never called (obviously). When added the portlet isn't loaded, and there aren't any exceptions or log messages. Are there any gotchas I should be aware of?

   <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">


           <!-- Listeners used by application -->
        <listener>
            <listener-class>listenerpackage.MyClassThatImplementsHttpSessionListener</listener-class>
        </listener>
    ...Other tags...
    </web-app>

In reply to the comments I have been looking through the code to find trouble spots. My Listener's constructor does have some possible trouble. Normally (not as listener) an instance would be made by the relevant portlet's (the one that isn't loaded when the listener is added to the web.xml) constructor. Can I still expect this to be the case or does the container create a instance itself?


回答1:


The problem is that listeners declared in the deployment document are instantiated when that document is loaded. My class was designed to be instantiated by the portlet so needed info wasn't available.

A solution is to use an HttpSessionBindingListener and adding the class as an attribute to the session. Then the unbound() is called when the session invalidates. :)




回答2:


Is your listener node in the right order in your web.xml? Maybe it's being ignored due to not following the DTD? For instance, servlets, servlet-mappings, session-configs are all supposed to be before the listener.



来源:https://stackoverflow.com/questions/1439743/whats-wrong-with-my-listener-in-my-web-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!