如何确保servlet没有被加载?

发布于 2024-12-21 01:33:34 字数 547 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧夏天 2024-12-28 01:33:34

您可以使用 web.xml 中的 enabled 元素禁用 Servlet(这意味着无法通过定义的 url 模式映射访问它)。

Servlet 3.0 规范章节8.2.3 从 web.xml、web-fragment.xml 和注释组装描述符 说:

(...)
3. 如果使用 web.xml 中引入的enabled元素禁用servlet
那么 servlet 将无法通过为 servlet 指定的 url 模式使用。 (...)

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:

(...)
3. If a servlet is disabled using the enabled element introduced in the web.xml
then the servlet will not be available at the url-pattern specified for the servlet. (...)

The enabled element is defined in XML Schema here and can be found as a child element of the <servlet> element.

溺深海 2024-12-28 01:33:34

允许您配置延迟加载。默认情况下,仅当访问 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.

空宴 2024-12-28 01:33:34

允许您配置延迟加载。默认情况下,仅当访问 servlet 时(通过其 url 模式)才加载 servlet。您可以将其设置为在启动时加载。

也就是说,如果您将不应该加载的 servlet 设置为仅根据请求加载,然后使用负载平衡器来确保无论命中该服务器的任何 URL 都会命中另一台服务器,那么您可能会很高兴。

博佐+1。他的回答是一个很好的起点。

<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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文