tomcat - Web.xml 配置替换为 LoadOnStartup 的注释

标签 tomcat annotations servlet-3.0

规范 Tomcat 8.0.20,操作系统:win 7,Java:1.8

1) Servlet StartServletInit 扩展了 HttpServlet

2) StartServletInit 有只有一种方法 "public void init(ServletConfig config)”,它读取类路径中的“属性文件”并打印 在控制台上控制注入(inject)的键/值对。

3) web.xml 有标题项如下

version="3.1" 
  **metadata-complete="false"**  
  xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

3) Web.xml 在启动时加载为

<servlet>
<servlet-name>StartServletInit</servlet-name>
<servlet-class>org.web.init.StartServletInit</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

O/p:完美执行并在控制台上打印。 ===> :)

问题

注释 [注释了 web.xml 的 loadOnStartup 并注释了代码] "@WebServlet(name = "StartServletInit",loadOnStartup = 1)

O/p:不 - 将键/值打印到控制台。 ===> :(

最佳答案

我认为您不能为同一个 servlet 混合使用 web.xml 配置和注释。您可以使用 web.xml 或注释,但不能同时使用。

尝试从 web.xml 中完全删除 servlet 定义和 servlet 映射,并将 servlet 映射的 url-pattern 的值作为“值”属性放在注释中。使用注释时,servlet 名称并不是很有用。

例子:

@WebServlet(value="/your_url_pattern", loadOnStartup=1)

关于tomcat - Web.xml 配置替换为 LoadOnStartup 的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265903/

相关文章:

jsp - 如何更改 "Tomcat Web Application Manager"管理页面 http ://localhost:8080/manager/html 上的默认 &lt;title&gt;

shell - Tomcat servlet 引擎未运行,但 pid 文件存在。这条消息是什么意思?如果我得到它,我需要恢复 Tomcat 吗?

java - 运行时类路径中是否需要注解类文件?

java - 从 Java 代码启动和停止 Tomcat

spring - 服务器到客户端 SSL 加密,不带 SSL 身份验证 - Tomcat 和 Spring

java - 我可以跳过 HttpServletRequest.login() 并将经过身份验证的用户放入 session 中吗?

jsp - 如何在 servlet 中获取 JSP 请求字符串?

java - 如何使用 servlet 3.0 java 配置指定 mime 映射?

java - 实现 Map.entrySet 时出现 'mismatching null constraints' 错误(Java8、Eclipse)

java - 如何使用注释在当前函数之前运行函数?