且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Tomcat 上的 Servlet:未找到请求的资源

更新时间:2022-11-06 14:15:28

在我看来,您在 ROOT 下有 HelloWorld 文件夹,就像 webapps/ROOT/HelloWorld/ 中的 web.xmlwebapps/ROOT/HelloWorld/WEB-INF/web.xmlwebapps/ROOT/HelloWorld/WEB-INF/classes/... 中的类它不会像这样工作.

It sounds to me like you have the HelloWorld folder under ROOT, like webapps/ROOT/HelloWorld/ with a web.xml in webapps/ROOT/HelloWorld/WEB-INF/web.xml and the classes in webapps/ROOT/HelloWorld/WEB-INF/classes/... It won't work like that.

webapps 下的每个文件夹都是它自己的独立应用程序,只能有一个 web.xml 处于活动状态.所以 ROOT 是一个 webapp,它只会读取 webapps/ROOT/WEB-INF/web.xml 中的 web.xml 和 webapps/ROOT 中的类/WEB-INF/classes/...

Each folder directly under webapps is its own self-contained app that can only have one web.xml active. So ROOT is a webapp that will only read the web.xml found at webapps/ROOT/WEB-INF/web.xml with the classes in webapps/ROOT/WEB-INF/classes/...

如果你有另一个文件夹 webapps/HelloWorld 它会读取 webapps/HelloWorld/WEB-INF/web.xml 中的 web.xml 和 中的类>webapps/HelloWorld/WEB-INF/classes/...

If you had another folder webapps/HelloWorld it would read the web.xml in webapps/HelloWorld/WEB-INF/web.xml with the classes in webapps/HelloWorld/WEB-INF/classes/...

这是正常的做法.

但会话不会跨应用共享.这样做的话,root 将拥有自己的会话,而 HelloWorld 将拥有自己的会话.

But session is not shared across apps. So doing it that way, root will have its own session, and HelloWorld its own session.

因此,如果您确实需要将 HelloWorld 置于根目录下,那么您必须在 webapps/ROOT/WEB-INF/web.xml 中的 web.xml 中定义您的 servlet.xml 并将您的类文件直接放在根目录下的 WEB-INF 中: webapps/ROOT/WEB-INF/classes/... 然后你必须定义 url-pattern在 web.xml 中作为(至少理论上我认为这会起作用):

So, if you really need HelloWorld to be under root, then you'll have to define your servlet in the web.xml in webapps/ROOT/WEB-INF/web.xml and put your class files in the WEB-INF directly under root: webapps/ROOT/WEB-INF/classes/... Then you'd have to define the url-pattern in the web.xml as (at least theoretically I think this would work):

     <servlet-mapping>
         <servlet-name>HelloWorld</servlet-name>
         <url-pattern>/HelloWorld/controller.do</url-pattern>
     </servlet-mapping>