2013-05-04 69 views
2

這是從我的HTML代碼塊:當我的doPost改變servlet的內容,沒有任何反應

<form action="LoginServlet" method="post"> 
    Username: <input type="text" name="username"><br> 
    Password: <input type="password" name="password"> 
    <input type="submit" value="Log In"> 
</form> 

這裏是的ServletContextListener:

public class DataListener implements ServletContextListener { 
private AccountManager accs; 
ServletContext context; 
/** 
* Default constructor. 
*/ 
public DataListener() { 
    // TODO Auto-generated constructor stub 
} 

/** 
* @see ServletContextListener#contextInitialized(ServletContextEvent) 
*/ 
public void contextInitialized(ServletContextEvent e) { 
    accs = new AccountManager(); 
    context = e.getServletContext(); 
    context.setAttribute("accounts", accs); 
} 

/** 
* @see ServletContextListener#contextDestroyed(ServletContextEvent) 
*/ 
public void contextDestroyed(ServletContextEvent e) { 
    context = e.getServletContext(); 
} 

}

,這裏是我的servlet doPost:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    //ServletContext context = getServletContext(); 
    //AccountManager manager = (AccountManager) context.getAttribute("accounts"); 


    /*if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){ 
     RequestDispatcher dispatch = request.getRequestDispatcher("welcome.jsp"); 
     dispatch.forward(request, response); 
    } else{ */ 
     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 
     out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>"); 
     out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" 
         + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); 
     out.println("<html xmlns='http://www.w3.org/1999/xhtml'>"); 
     out.println("<head>"); 
     out.println("<title>Information Incorrect</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.print("<h1>Please Try Again </h1>"); 
     out.print("<br />"); 
     out.print("Either Your username or password is incorrect. Please try again."); 
     out.print("<br />"); 
     out.print("<br />"); 
     request.getRequestDispatcher("/LoginForm.html").include(request, response); 
     out.println("</body>"); 
     out.println("</html>"); 
// } 

Probl時間就是這樣,當我運行welcome.html並按下登錄按鈕時,仍舊舊的代碼執行其工作。 I'mean我已經評論說這部分:

/*if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){ 
    RequestDispatcher dispatch = request.getRequestDispatcher("welcome.jsp"); 
    dispatch.forward(request, response); 
} else{ */ 

但儘管如此,當我按下按鈕,這個評論塊執行... ,所以我不能有任何改變..任何人都可以解釋我如何能重新啓動我的servlet類?或者是什麼問題? 謝謝你在advace


我所做的項目 - >乾淨,它的工作:)

+0

我正在Eclipse Juno和Tomcat 7.0上工作 – 2013-05-04 12:48:15

回答

0

Giorgi,這應該可以通過Build> Clean輕鬆解決。如果繼續執迷不悟,請注意在Eclipse中,生成的類被寫入在指定的Java構建路徑實用程序的目錄:

enter image description here

您可以手動刪除此創建的類文件。該目錄將隱藏在Eclipse包資源管理器中。您可以直接在文件系統中執行,而不是更改默認的Eclipse視圖過濾器。

然後重建。它應該解決你的問題。

0

您的應用程序可以在tomcat的目錄下的tomcat \工作\卡塔利娜\本地主機緩存。 嘗試從此目錄中刪除您的應用程序並重新部署您的應用程序或簡單重新啓動tomcat。

如果上面沒有幫助,那麼eclipse WAR創建或部署肯定會有一些小故障。建立你的WAR,並確保它包含更新的類文件。關鍵是要檢查部署WAR的Tomcat目錄中的文件日期。即使您正在部署全新的已清理的WAR,並刪除所有文件夾,也可能發生這種情況,但仍然存在較舊的緩存文件,這可能是因爲Eclipse會保留它們以節省編譯時間,並認爲它們沒有任何更改。確保Tomcat中的war或webapp文件夾包含最新的類文件,然後你應該很好。

祝你好運!

+0

tomcat/work目錄是空的......當我在eclipse中重新啓動tomcat時,它並沒有幫助。如果我要更改html文件,然後重新啓動tomcat,它會顯示我更改了html,但是當我在servlet中更改doPost方法時,沒有任何反應 – 2013-05-04 13:05:37

+0

已更新分辨率,請嘗試關注它並讓我知道以防萬一它有幫助! – 2013-05-04 14:25:06

+0

我已經以不同的方式做到了。我剛剛清理了項目,它幫助。謝謝你:) – 2013-05-04 15:54:48

相關問題