gpt4 book ai didi

java - Servlet 不工作

转载 作者:行者123 更新时间:2023-12-01 13:21:38 25 4
gpt4 key购买 nike

我有一个 HelloWorld servlet(读作“我是一个 java 菜鸟”),但它在 tomcat7 中不起作用。有人可以帮助我理解为什么它不起作用吗?请注意,我使用的是 tomcat7 的开箱即用配置。我还确认了 ROOT 默认 Web 应用程序工作正常,确认 tomcat 可以启动、java 配置正确等。

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");

// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}

public void destroy()
{
// do nothing.
}
}

注意:我从 Hello World 教程中在线获取了此源代码

所以,我像这样编译它:

javac -cp/usr/local/tomcat/lib/servlet-api.jar ./HelloWorld.java

然后我将编译后的 HelloWorld.class 移动到 /usr/local/tomcat/webapps/hello/WEB-INF/classes。然后我创建了以下 web.xml 文件:

<web-app
version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemalocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

[编辑1:添加带有响应的curl命令]

[vagrant@scep webapps]$ curl localhost:8080/hello -v


* About to connect() to localhost port 8080 (#0)
* Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
> GET /hello HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.6.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: Apache-Coyote/1.1
< Location: http://localhost:8080/hello/
< Transfer-Encoding: chunked
< Date: Mon, 24 Feb 2014 04:31:07 GMT
<
* Connection #0 to host localhost left intact
* Closing connection #0

这是使用 / 进行的测试:

[vagrant@scep webapps]$ curl localhost:8080/hello/ -v
* About to connect() to localhost port 8080 (#0)
* Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
> GET /hello/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.6.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
< Content-Language: en
< Content-Length: 963
< Date: Mon, 24 Feb 2014 04:32:07 GMT
<
* Connection #0 to host localhost left intact
* Closing connection #0
<html><head><title>Apache Tomcat/7.0.52 - Error report</title><!-- a lot of stuff, truncated --></html>

[编辑1:添加/usr/local/tomcat/webapps目录的tree]

[vagrant@scep webapps]$ tree
.
└── hello
└── WEB-INF
├── classes
│   └── HelloWorld.class
└── web.xml

最佳答案

由于您的 servlet 位于 hello webApp 中,因此您需要调用 localhost:8080/hello/hello

URL 模式/具有特殊含义。它即表示“默认 Servlet”URL 模式。因此,与 web.xml 中任何其他更具体的 URL 模式不匹配的每个请求最终都会出现在该 servlet 中。

查看 roguewave.com/portals/0/products/Hydraexpress/docs/3.5.0/html/… 的第 4.3.3 节表明,只有在所有其他匹配失败时才会使用默认 servlet。所以实际上,我的 URL 应该如此,你的 URL 也应该如此。

尝试使用浏览器,而不是curl。

关于java - Servlet 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com