如何在web.xml中确定侦听器的顺序 [英] How to determine the order of listeners in web.xml

查看:71
本文介绍了如何在web.xml中确定侦听器的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java webapp中有一堆servlet上下文监听器,每个都收集一些有关环境的信息。

I got a bunch of servlet context listeners in my Java webapp, each of them gathering some information about the environment.

其中一些取决于收集的信息另一个听众。但我无法确定监听器的注册和调用顺序,因此我必须重复代码。

Some of them depend on information which is gathered by another listener. But I can't determine the order in which the listeners are registered and called, so I have to duplicate code.

我理解听众是按照他们的顺序注册的在web.xml中订购,但这对我来说听起来有些含糊,过于含糊,无法依赖它。

I understand that the listeners are registered in the order their order in web.xml but this sounds a bit vague to me, too vague to rely on it.

您是否有提示如何解决我的问题?

Do you have a hint how I can solve my problem?

推荐答案

所有servlet容器和Java EE容器都严格执行规范的这一部分。您可以依赖于按照您在web.xml中指定的顺序调用侦听器这一事实。

All servlet containers and Java EE containers implement this part of the spec strictly. You can rely on the fact that the listeners are called in the order you specified in web.xml.

您可以拥有一个应用程序级别数据结构(HashMap)每个过滤器/侦听器在遇到请求中的数据时进行更新。这将让每个Listener只更新必要的内容。您可以将公共代码放在基本侦听器中,这样就不会出现代码重复。

You can have a Application LEVEL Data structure(HashMap) that will be updated by each Filter/Listener as it encounters the data from the requests. This will let each Listener update only what is essential. You can put the common code in a base Listener so that there is no code duplication.

这篇关于如何在web.xml中确定侦听器的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆