简体   繁体   English

有没有办法将某些操作引导到我的所有servlet?

[英]Is there a way to bootstrap some actions to all my servlets?

I have a bunch of Servlets. 我有一堆Servlet。 I want all of them to instantiate a Logger, pretty much the exact same way. 我希望他们都实例化一个Logger,几乎完全一样。
Is there a proper (like a Main file) where I can bootsrap such code? 是否有适当的(如主文件)可以在其中引导此类代码?
Currently what I do is created a subclass of the Servlet class, which does that in the init function and all the other Servltes inherit from it. 当前,我所做的是创建Servlet类的子类,该子类在init函数中执行此操作,所有其他Servltes都从该继承。

I don't know if it's a good idea what you are trying for the logger (i think you will lose some logger capacibilities), but well, if you want an unique instance of something in a servlet you should declare it and init it in the static way: 我不知道您要为记录器做什么是一个好主意(我认为您会失去一些记录器功能),但是,如果您想要Servlet中某个东西的唯一实例,则应在其中声明并初始化它静态方式:

static final SomethingClear GLOBAL;
static{
    // do tons of things to init 
    GLOBAL = ClassWithStaticFunc.gimmeGlobalLoaded();
}

An why is better this way? 为什么这样更好? because there's another funny -or well, may be annoying :( - way to load a static (in the senses of uniqueness and that always will be an instance in memory) variable in a servlet: 因为还有另一种有趣的-或可能是令人讨厌的:(-在Servlet中加载静态变量(在唯一性的意义上,并且总是内存中的实例)的方式:

Servlets dont have nothing like 'main'. Servlet没有像“ main”这样的东西。 They are unique instances which live in a servlet container, are instanciated by it, and its methods (doGet, doPost, doPut, doDelete) are executed by the servlet container on demand giving them request and response objects. 它们是驻留在servlet容器中的唯一实例,由实例实例化,并且其方法(doGet,doPost,doPut,doDelete)由servlet容器按需执行,从而为它们提供请求和响应对象。 So, in memory, there will be allways one and only one instance of each servlet, so any variable outside doGet, doPut, doDelete and doPost will be unique in memory. 因此,在内存中,每个Servlet始终只有一个实例,因此,doGet,doPut,doDelete和doPost之外的任何变量在内存中都是唯一的。 This was done this way in the beginings of the java-time in order to save memory... 这是在java-time的初期以节省内存的方式...

And this is a funny way to merge sessions... :( 这是合并会话的一种有趣方式... :(

If you assing something to anything outside that methods in a servlet it will be assigned to all sessions of your application, and is not obvious and is not so easy to find... 如果您在Servlet中将该方法之外的任何事物都关联起来,它将被分配给您应用程序的所有会话,并且不那么明显,而且也不那么容易找到...

So its better to mark them as static and final because they'll be always created (you have no access to objects construction) and there will be only one instance. 因此最好将它们标记为static和final,因为它们将始终被创建(您无权访问对象构造)并且只有一个实例。 It's not nice, but servlets are not nice... 不好,但是servlet不好。

I once did a unique servlet whose invoke my custom 'Servlets' using reflection (one object per petition, giving the name of my 'servlet' in the web.xml as initParam and setting the servlet-name always to the same servlet that was the instanciator of mines) to solve session merges and worked great. 我曾经做过一个唯一的servlet,它使用反射(每个请求一个对象,在web.xml中将我的“ servlet”的名称指定为initParam)来调用我的自定义“ Servlet”,并将servlet名称始终设置为与该servlet相同的servlet。矿工顾问)解决会议合并问题,并取得了不错的成绩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM