如何确保servlet没有被加载?
我的 web.xml 中有 servlet,但我不希望我的应用程序加载它,人们会认为如果我们不希望加载该 servlet,那么将它放在那里的目的是什么,实际上我需要它在 web.xml 中,因为我正在部署应用程序的两个实例,在一个实例上我需要拥有该 servlet,而在另一个实例上我不想拥有它,并且我只使用一个 web.xml
,不知道如何做到这一点。
这是我的 web.xml
:
<servlet>
<servlet-name>StartServlet</servlet-name>
<servlet-class>com.web.startServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
我无法输入 -ve
值,因为容器会随机调用此 servlet,请在此纠正我并建议正确的方法。
I have servlet in my web.xml but i don't want my application to load it, one would think that if we don't want that servlet to load then what is the purpose of putting it there, actually I need to have it in web.xml because am deploying two instances of application and on one instance I need to have that servlet and on another I do not want to have it and I'm using only one web.xml
, am not sure how this can be done.
Here is my web.xml
:
<servlet>
<servlet-name>StartServlet</servlet-name>
<servlet-class>com.web.startServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
I cannot put -ve
value because then container would invoke this servlet randomly, kindly correct me here and advise of an proper way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 web.xml 中的
enabled
元素禁用 Servlet(这意味着无法通过定义的 url 模式映射访问它)。Servlet 3.0 规范章节8.2.3 从 web.xml、web-fragment.xml 和注释组装描述符 说:
enabled
元素在 XML 架构 此处,并且可以作为
元素的子元素找到。You can disable the Servlet (which means that it will not be reachable through defined url-pattern mapping) by using
enabled
element in web.xml.Servlets 3.0 specification in Chapter 8.2.3 Assembling the descriptor from web.xml, web-fragment.xml and annotations says:
The
enabled
element is defined in XML Schema here and can be found as a child element of the<servlet>
element.
允许您配置延迟加载。默认情况下,仅当访问 servlet 时(通过其 url 模式)才加载 servlet。您可以将其设置为在启动时加载。<load-on-startup>
allows you to configure lazy-loading. By default the servlet is loaded only when it is accessed (by its url-pattern). You can set it to be loaded on startup instead.也就是说,如果您将不应该加载的 servlet 设置为仅根据请求加载,然后使用负载平衡器来确保无论命中该服务器的任何 URL 都会命中另一台服务器,那么您可能会很高兴。
博佐+1。他的回答是一个很好的起点。
That said, if you set the servlet that isn't supposed to load to only load on request, and then use a load balancer to ensure that whatever URL would hit that server hits the other one instead, you'd probably be good to go.
+1 to Bozho. His answer is a great place to start.