2014-02-24 91 views
0

我有一个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 
+0

只是检查这里显而易见的是,你复制类文件后重新启动tomcat吗?还检查使用netstat绑定到8080的pid实际上是你的tomcat的进程(不是其他进程)(发生在我之前) – gerrytan

+0

是的。我在每次更新时都重新启动了它,但是后来我注意到,当“web.xml”发生更改时,它被配置为重新加载。当我保存了无意义的更新并且日志再次出现时,这一点得到了证实。 – bitcycle

+0

我*想*您只需要刷新打印作家,因为它没有设置为自动刷新。打印完成后输出“out.flush()”。 302表示找到了servlet。该文档还提到了这一点:http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/ServletResponse.html#getWriter() –

回答

1

由于你的servlet是在Web应用程序hello你需要调用本地主机:8080 /打招呼/你好

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

查看roguewave.com/portals/0/products/hydraexpress/docs/3.5.0/html/...的第4.3.3节,指出只有在所有其他匹配失败的情况下才会使用默认的servlet。所以实际上,我的URL应该是你的。

而不是卷曲,请尝试使用浏览器。

+0

我不认为这是正确答案,servlet hello被映射到/(参见web.xml) – gerrytan

+0

@scotth你的情况如果映射为'/hello',但是OP发布的代码是'/',那么这将是正确的。这是servlet的映射,而不是上下文路径。 Tomcat暗示上下文路径为'/ hello',因为该应用程序部署在名为'hello'的文件夹中。所以正确的网址是http:// localhost:8080/hello/ – gerrytan

+0

@gerrytan查看我更新的答案 –

0

我知道你做对了一种方法。但是,你只是尝试这种方法。同时你的状态如下:

  1. 以'/'开头并以'/ *'结尾的字符串用于路径映射。
  2. 以'*'开始的字符串。前缀被用作扩展映射。
  3. 只包含'/'字符的字符串表示应用程序的“默认”servlet。在这种情况下,servlet路径是请求URI减去上下文路径,路径信息为空。
  4. 所有其他字符串仅用于精确匹配。

你只是尝试这种方法。

<servlet-class>package.name of the servlet controller</servlet-class>