2015-06-16 114 views
3

我是servlet編程的新手。Java - Servlet 404錯誤

以下是我的樣本servlet代碼

Sample.Java

public class Sample extends HttpServlet { 
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
     res.setContentType("text/html"); 
     PrintWriter pw=res.getWriter(); 

     String name=req.getParameter("name");//will return value 
     pw.println("Welcome "+name); 
     pw.close(); 
    } 
} 

Web.xml中

<web-app> 
<servlet> 
<servlet-name>sampleservlet</servlet-name> 
<servlet-class>Sample</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>sampleservlet</servlet-name> 
<url-pattern>/Sample</url-pattern> 
</servlet-mapping> 
</web-app> 

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form action="Sample" method="get"> 
Enter your name<input type="text" name="name"><br> 
<input type="submit" value="login"> 
</form> 
</body> 
</html> 

我的問題

當我在我的項目上右鍵單擊,然後單擊Run as -> Run on server,我選擇服務器,它要求我要選擇一個服務器,然後我選擇要運行的服務器。在Eclipse窗口打開一個瀏覽器窗口,具有地址URL爲http://localhost:8080/Sample/

enter image description here

當我點擊login它給了我這樣的錯誤,

HTTP狀態404 - /採樣/歡迎


狀態報告

消息 /樣品/歡迎

描述所請求的資源不可用。


的Apache Tomcat/7.0.62

截圖

enter image description here

爲什麼會出現這樣的錯誤?

其他細節

服務器:Apache-Tomcat的7.0。62 IDE:Eclipse的Java EE的開普勒

步驟我試圖解決這個問題

我嘗試了這些,

  1. Getting HTTP Status 404 error when trying to run servlet

結果

我沒有請參閱中的.class文件文件夾。

  • Tomcat giving 404 error with servlets
  • 結果 在此路徑 「tomcat7 \工作\卡塔利娜\本地主機\應用程式」 時,work文件夾是空的。

    請讓我知道,如果我需要發佈任何更多的細節。從IDE

    目錄結構

    enter image description here

    +0

    這將是有益的,看看項目結構/樹。也許從IDE的屏幕截圖 – Stan

    +0

    您是否在'web.xml'文件中輸入了一個條目? –

    +0

    @Stan我添加了項目結構的截圖。 –

    回答

    2

    你的代碼在tomcat 7和eclipse中對我有用。 也許您的index.html與您發佈的index.html不一樣,因爲它是針對Sample/welcome而不是Sample/Sample。

    提示:

    • 確保您的index.html發佈的版本是一樣的,你在你的代碼有一個。修改內容並確保其正確發佈。
    • 直接在您的瀏覽器中加載http://localhost:8080/Sample/Sample?name=Random以查看servlet正在運行。
    • 雙擊服務器上的Tomcat7。轉到您的服務器路徑。例如C:\ Java \ workspace.metadata.plugins \ org.eclipse.wst.server.core \ tmp2 \ wtpwebapps \ Sample \ WEB-INF \ classes(您的Sample類應該在那裏),仔細檢查您是否擁有這些文件夾的權限。
    • 將您的項目導出爲WAR並直接將其部署到您的tomcat而不是日食。
    • 清理/構建您的項目
    • 在Servers/server.xml下的Package Explorer中,您應該有一個聲明的上下文,例如<Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host> 如果你不仔細檢查這些文件夾中的權限。再次從頭開始添加您的tomcat7服務器。

    enter image description here

    +0

    我改變了'

    '到'',我得到了上述相同的錯誤。請讓我知道如果我做錯了什麼。 –

    +0

    我試過了你的第二個技巧。我仍然收到同樣的錯誤。什麼可能是錯的? –

    +0

    @Codester使用 請輸入您的名字
    反而讓我知道如果您看到「Please,Enter ...「我認爲服務器沒有正確刷新您的更改。 – PbxMan

    0

    試試這個,你不需要編寫任何servlet的XML文件;)

    只需添加註釋@WebServlet(「/ urlpattern「),並在JSP中調用這個url模式<form action="urlpattern">

    只有在eclipse上工作。

    @WebServlet("/Sample") 
    
    public class Sample extends HttpServlet { 
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
        res.setContentType("text/html"); 
        PrintWriter pw=res.getWriter(); 
    
        String name=req.getParameter("name");//will return value 
        pw.println("Welcome "+name); 
        pw.close(); 
    }} 
    

    希望它有幫助。

    +0