我有一个HelloWorld servlet(阅读“我是java noob”),它不能在tomcat7中工作。有人能帮助我理解为什么它不起作用吗?请注意,我正在使用tomcat7的开箱即用配置。我还证实,ROOT
默认的Web应用程序正常工作,确认tomcat的启动可以,JAVA是正确的配置等Servlet不能正常工作
// 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命令与响应]
[[email protected] 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
这里与/
测试:
[[email protected] 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
]
[[email protected] webapps]$ tree
.
└── hello
└── WEB-INF
├── classes
│ └── HelloWorld.class
└── web.xml
只是检查这里显而易见的是,你复制类文件后重新启动tomcat吗?还检查使用netstat绑定到8080的pid实际上是你的tomcat的进程(不是其他进程)(发生在我之前) – gerrytan
是的。我在每次更新时都重新启动了它,但是后来我注意到,当“web.xml”发生更改时,它被配置为重新加载。当我保存了无意义的更新并且日志再次出现时,这一点得到了证实。 – bitcycle
我*想*您只需要刷新打印作家,因为它没有设置为自动刷新。打印完成后输出“out.flush()”。 302表示找到了servlet。该文档还提到了这一点:http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/ServletResponse.html#getWriter() –