2015-12-10 147 views
0

在SO上有這樣的問題,但沒有一個解決了我的問題,我想不出任何可能導致此錯誤的問題。Tomcat servlet - 狀態404請求的資源不可用

我試圖寫一個簡單的servlet,只輸出「Hello World」:

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 

public class PrintServlet extends HttpServlet { 


    public void doGet(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException { 
     PrintWriter out = response.getWriter(); 
     out.println("Hello World"); 
    } 
} 

我所做的就是編譯java文件,編譯後的文件移動到/usr/local/apache-tomcat-7.0.65/webapps/examples/WEB-INF/classes,再加入這些到網上位於/usr/local/apache-tomcat-7.0.65/webapps/examples/WEB-INF .xml文件:

<servlet> 
    <serlet-name>PrintServlet</servlet-name> 
    <servlet-class>PrintServlet</servlet-name> 
</servlet> 

<servlet-mappinng> 
<servlet-name>PrintServlet</servlet-name> 
<url-pattern>/servlet/PrintServlet</url-pattern> 
</servlet-mapping> 

並試圖訪問使用http://localhost:8080/servlet/PrintServlet該servlet。

我收到錯誤消息:狀態404請求的資源不可用。

我沒有看到可能出現錯誤的地方,我只是簡單地跟隨教程。有人可以幫我嗎?

編輯:在catalina.out的

日誌文件(通過調用尾-f catalina.out的得到了這個,不知道怎麼回事打開此文件)。

Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/examples has finished in 130 ms 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/host-manager 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/host-manager has finished in 80 ms 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/manager 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/manager has finished in 96 ms 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.65/webapps/ROOT 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.HostConfig deployDirectory 
INFO: Deployment of web application directory /usr/local/apache-tomcat-7.0.65/webapps/ROOT has finished in 62 ms 
Dec 10, 2015 4:46:02 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 
Dec 10, 2015 4:46:02 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["ajp-bio-8009"] 
Dec 10, 2015 4:46:02 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 918 ms 

啓動消息調用./startup.sh時:

Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.65 
Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.65 
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.65/temp 
Using JRE_HOME:  /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home 
Using CLASSPATH:  /usr/local/apache-tomcat-7.0.65/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.65/bin/tomcat-juli.jar 
Tomcat started. 
+0

- >'http:// localhost:8080/examples/servlet/PrintServlet' – Mil4n

+0

不幸的是,我得到了同樣的錯誤。 – eager2learn

+0

嘗試使用示例/ servlet/PrintServlet –

回答

2

在web.xml你說你有這樣的:

<servlet> 
    <serlet-name>PrintServlet</servlet-name> 
    <servlet-class>PrintServlet</servlet-name> 
</servlet> 

<的servlet類> PrintServlet </servlet-name>區別?

它應該是這樣的:

<servlet> 
    <serlet-name>test.PrintServlet</servlet-name> 
    <servlet-class>PrintServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

解決這個問題,重新啓動和報告,如果它工作;-)

如果不工作:

我想你做的映射更簡單:

<servlet-mappinng> 
    <servlet-name>PrintServlet</servlet-name> 
    <url-pattern>/print</url-pattern> 
</servlet-mapping> 

應該解析爲http://localhost:8080/examples/print

而一些調試輸出添加到您的servlet:

package test; 

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 

public class PrintServlet extends HttpServlet { 

    static { 
     System.out.println("STACKOVERFLOW:> I've been loaded!"); 
    } 

    public void doGet(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException { 
     PrintWriter out = response.getWriter(); 
     out.println("Hello World"); 
    } 
} 

重新編譯,放在例子/ WEB-INF/classes下,重新啓動Tomcat並觀察行出現。或者日誌顯示錯誤消息?

+0

這是一個人眼! :)或好的IDE – Mil4n

+0

感謝您的幫助,但不幸的是,即使糾正後,我仍然得到相同的錯誤信息。也許問題出在指定的url-pattern中。在瀏覽器中編寫/servlet/PrintServlet然後調用「localhost:8080/examples/servlet/PrintServlet」是否正確? – eager2learn

+0

看到我的編輯 - 希望這有助於 – Jan

相關問題