我在 Eclipse 上创建了一个带有一个类的 Web 项目:“HelloWorld.java”,它应该有一个回答 GET 请求的方法。
package javaeetutorial.hello;
// imports
@Path("base")
public class HelloWorld extends HttpServlet {
public HelloWorld() {
}
@GET
@Produces("text/html")
public String getHtml() {
return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
}
}
然后,在 WebContent 文件夹中的 WEB-INF 目录中,我创建了一个包含以下内容的 web.xml 文件,以便将到 /hello url 的请求映射到我的 servlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
metadata-complete="true"
version="3.1">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>javaeetutorial.hello.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
我将项目导出到 .war 文件,然后使用 Glassfish 部署它,但是当我调用应该调用我的 Web 服务的 URL 时,它显示“请求的资源 () 不可用”。
我调用的 URL 是:http://localhost:8080/Calculator/hello/base
为什么我的网络服务没有被调用?