簡體   English   中英

Servlet無法運作

[英]Servlet isn't working

我有一個HelloWorld servlet(閱讀“我是Java菜鳥”),它在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:添加了/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。

嘗試使用瀏覽器而不是卷曲。

我知道您做對了一種方法。 但是,您只需嘗試此方法。 同時,您的工作狀況如下所示:

  1. 路徑映射使用以'/'字符開頭並以'/ *'后綴結尾的字符串。
  2. 以“ *”開頭的字符串。 前綴用作擴展名映射。
  3. 僅包含“ /”字符的字符串表示應用程序的“默認” servlet。 在這種情況下,Servlet路徑是請求URI減去上下文路徑,並且路徑信息為null。
  4. 所有其他字符串僅用於完全匹配。

您只需嘗試此方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM