2011-05-31 76 views
0

我有tomcat 7.0.14,jdk1.6和Eclipse Indigo for Java EE Web應用程序開發人員。 我是JSP新手,所以嘗試過Eclipse,因爲我認爲它使編碼變得更容易。我緊跟着this video tutorialEclipse Indigo中的Servlet JSP連接問題

我在我的項目prison- login.java,home.html和修改過的web.xml下創建了2個文件。 當我點擊登錄提交按鈕(這是第1頁)頁面,它僅顯示這樣的錯誤:

File not found 
      Firefox can't find the file at /D:/eclipse/Workspace/Prisonhome/WebContent/WEB-INF/Log?user=sa&pass=sa&action=That's+me. 

我不知道這是否是不足以拯救項目工作區。

視頻和我的Eclipse文件中給出的文件位置之間存在若干差異。例如:我的Eclipse中的web.xml位於servers-> apache-tomcat目錄中,而不是Web內容 - > WEB_INF-> lib-> web.xml。是不是重要?

我的代碼: 的index.html

<html> 
<head> 
    <title> Welcome :) </title> 
</head> 
<body> 
<form action="Log" method="get"> 
      <B> 
      <br><br><br><br><br><br><br><br><br><br><br><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;Username: <input type=text name="user"><br><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;Password: <input type=text name="pass"><br><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit name=action value="That's me" style="background-color:666666; color:ffffff; border-color:ee0088"> 
      </B></font> 
     </form> 
    </body> 
</html> 

Login.java:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String uname=request.getParameter("user"); 
    String passwd=request.getParameter("pass"); 
    response.getWriter().println("<html><head>This is the response!</head>"); 
    response.getWriter().println("<body>Response!!</body></html>"); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    doGet(request, response); 
} 

的web.xml:

<servlet> 
    <servlet-name>Log</servlet-name> 
    <servlet-class>org.prisonhome.packs.Login</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Log</servlet-name> 
    <url-pattern>/Log/*</url-pattern> 
</servlet-mapping> 
+0

請注意,如果答案適合您,您應該將其標記爲已接受。 – Bozho 2011-06-01 15:55:00

回答

0

Firefox can't find the file at /D:/eclipse/Workspace/Prisonhome/WebContent/WEB-INF/Log?user=sa&pass=sa&action=That's+me.

您需要通過真實的網址而不是本地磁盤文件系統路徑打開頁面。如果Tomcat運行在localhost 8080端口和web應用程序上下文名稱爲Prisonhome那麼你需要通過

http://localhost:8080/Prisonhome/index.html

調用它作爲教程,我建議你閱讀Coreservlets.com教程。您可以在我們的Servlets wiki page的底部找到鏈接列表。


無關到具體的問題,使用GET登錄是不是真的安全,因爲每個人都可以看到URL的密碼。 HTML <font>標記自1998年起已棄用,請改爲使用CSS。使用過多的<br>&nbsp;標籤也是不必要的,請改用CSS位置/邊距/填充。

+0

非常感謝您的先生!但似乎我的tomcat沒有正常啓動。只要我正確設置了tomcat,我就會遵循你的建議。假設我重新安裝tomcat,那麼我需要進行更改以覆蓋以前的tomcat指針? – sap 2011-06-01 15:51:14

+0

沒什麼,基本上。也就是說,當你通過下載和解壓zip來安裝它。只要刪除提取的文件夾應該是足夠的。您只需確保先關閉它。 – BalusC 2011-06-01 15:52:42

+0

現在我的tomcat工作正常。我用一個帶有html和jsp文件的簡單項目對它進行了測試。當我運行每個文件時,我在工作空間中得到正確的輸出。但是當我重新配置服務器以包含我的項目並單擊run main。它只顯示「請求的資源(/Prisonhome/main.jsp)不可用」。 HTTP狀態404.有什麼可能丟失? – sap 2011-06-02 10:18:17

0

您需要訪問tomcat的文件 - 即http://localhost:8080/Prisonhome/index.html。不要從文件瀏覽器啓動它。

爲了做到這一點,您需要部署應用程序,並且運行服務器(tomcat)。

+0

非常感謝您的先生! – sap 2011-06-01 15:51:22