Jakarta ee 在外部java项目中使用@WebServlet注释

Jakarta ee 在外部java项目中使用@WebServlet注释,jakarta-ee,servlet-3.1,Jakarta Ee,Servlet 3.1,我试图只使用本机java,而不使用spring、struts等框架 我正在使用servlet和JSP构建一个web应用程序 我在project explorer中的项目布局(在Eclipse中): MyApp(java项目), MyAppWEB(动态web项目), MyAppWEBEAR(ear项目) Servlet位于MyApp项目中。 Jsp在MyAppWEB项目中 这两个项目都添加到EAR中。 MyApp已作为部署程序集中的jar添加到MyAppWEB的清单中 现在的问题是: 当我在ser

我试图只使用本机java,而不使用spring、struts等框架

我正在使用servlet和JSP构建一个web应用程序

我在project explorer中的项目布局(在Eclipse中): MyApp(java项目), MyAppWEB(动态web项目), MyAppWEBEAR(ear项目)

Servlet位于MyApp项目中。 Jsp在MyAppWEB项目中

这两个项目都添加到EAR中。 MyApp已作为部署程序集中的jar添加到MyAppWEB的清单中

现在的问题是: 当我在servlet类名上方使用@WebServlet(“/hi”)时,我得到错误404

但是,如果我在web.xml中定义servlet,那么一切都可以正常工作(没有注释)

同样,如果将MyApp项目导出为jar并将其放入WEB-INF/lib文件夹中 如果不使用web.xml,那么它也可以正常工作

所以问题是,是否可以在java项目中使用此注释(不需要手动导出到jar并放入WEB-INF/lib)

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyAppWEB</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<!--   <servlet> -->
<!--     <description></description> -->
<!--     <display-name>HelloWorldServlet</display-name> -->
<!--     <servlet-name>HelloWorldServlet</servlet-name> -->
<!--     <servlet-class>com.MyCom.MyApp.Servlets.HelloWorldServlet</servlet-class> -->
<!--   </servlet> -->
<!--   <servlet-mapping> -->
<!--     <servlet-name>HelloWorldServlet</servlet-name> -->
<!--     <url-pattern>/Hello</url-pattern> -->
<!--   </servlet-mapping> -->
</web-app>
HelloWorld.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MyApp</title>
</head>
<body>
    <h1>Hello World !!</h1>
</body>
</html>

MyApp
你好,世界!!
好的,我想好了。 似乎还需要将MyApp.jar添加到MyAppWEB项目的部署程序集中,而不仅仅是在清单条目中。这反过来会在服务器上部署时自动将MyApp.jar放在WEB-INF/lib中。 现在注释工作得很好

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MyApp</title>
</head>
<body>
    <h1>Hello World !!</h1>
</body>
</html>