我有一个HelloWorld servlet(读取,“我是一个java noob”)并且它在tomcat7中不起作用。有人能帮助我理解为什么它不起作用?注意,我正在使用tomcat7的开箱即用配置。我还确认ROOT
默认webapp工作正常,确认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:添加了tree
目录的/usr/local/tomcat/webapps
]
[vagrant@scep webapps]$ tree
.
└── hello
└── WEB-INF
├── classes
│ └── HelloWorld.class
└── web.xml
答案 0 :(得分:1)
由于您的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应该也是如此。
尝试使用浏览器,而不是卷曲。
答案 1 :(得分:0)
我知道你做对了一种方法。但是,你只是尝试这种方法。与此同时,您的工作条件将遵循以下类型:
你只是尝试这种方法。
<servlet-class>package.name of the servlet controller</servlet-class>