java 调用 servlet 时找不到 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5756677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:36:55  来源:igfitidea点击:

404 not found when calling servlet

javaservletsglassfish

提问by xdevel2000

I have a simple servlet say com.test.HelloWorld.class

我有一个简单的 servlet 说 com.test.HelloWorld.class

then I create these folders tree:

然后我创建这些文件夹树:

WEB-INF -->
            lib
            classes -->
                    com -->
                           test -->
                                HelloWorld.class
web.xml

where web.xml is:

其中 web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         version="3.0">
  <servlet>
    <servlet-name>helloworld</servlet-name>
    <servlet-class>
      com.test.HelloWorld
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloworld</servlet-name>
    <url-pattern>/servlets</url-pattern>
  </servlet-mapping>
</web-app>

after I create a .war called servlets.war and put it inside the autodeploy dir of the GlassFish server.

在我创建一个名为 servlets.war 的 .war 并将其放入 GlassFish 服务器的 autodeploy 目录之后。

After I point the url: localhost:8080/servlets/helloworld but I have a 404 error.

在我指向 url 之后:localhost:8080/servlets/helloworld 但我有一个 404 错误。

Why? What's worng?

为什么?穿什么?

Thanks.

谢谢。

回答by Jigar Joshi

Hit /yourApplicationContext/servletsas you specified that in url-mapping

/yourApplicationContext/servlets按您在 url-mapping 中指定的方式命中

for example : http://localhost:8080/myapp/servlets

例如: http://localhost:8080/myapp/servlets

回答by prat

In Netbeans services window you can right click the particular servlet and click Open in browser. It will run the servlet. You can find that particular URL in the address bar.

在 Netbeans 服务窗口中,您可以右键单击特定的 servlet,然后单击在浏览器中打开。它将运行 servlet。您可以在地址栏中找到该特定 URL。