![](/img/trans.png)
[英]Nothing happens when I try to apply an ActionListener to a JButton to change the content of the JLabel
[英]When I change servlet content in doPost, nothing happens
這是我的HTML代碼的一部分:
<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>");
// }
問題是,當我運行welcome.html並按下登錄按鈕時,仍舊的代碼可以正常工作。 我的意思是我已經評論了這一部分:
/*if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
RequestDispatcher dispatch = request.getRequestDispatcher("welcome.jsp");
dispatch.forward(request, response);
} else{ */
但是仍然,當我按下按鈕時,此注釋塊將執行...因此我無法在此處進行任何更改..任何人都可以解釋如何重新啟動Servlet類? 還是什么問題? 謝謝你
我做了項目->清潔,它的工作:)
您的應用程序可能會緩存在tomcat目錄下的tomcat \\ work \\ Catalina \\ localhost下。 嘗試從此目錄中刪除您的應用程序,然后重新部署您的應用程序,或者簡單地重新啟動tomcat。
如果上面的方法沒有幫助,那么肯定會在Eclipse WAR的創建或部署方面出現故障。構建您的WAR,並確保它包含更新的類文件。 關鍵是要檢查部署WAR的Tomcat目錄中的文件日期。 即使您部署了一個全新的WAR,並刪除了所有文件夾,也可能發生這種情況,因為那里仍然有較舊的緩存文件,這可能是因為Eclipse認為它們沒有任何變化可以節省編譯時間。 確保Tomcat中的war或webapp文件夾包含最新的類文件,然后您就可以了。
祝好運!
Giorgi,應通過“構建”>“清理”輕松解決此問題。 如果仍然頑固,請注意,在Eclipse中,將生成的類寫入Java Build Path實用程序中指定的目錄中:
您可以手動刪除此處創建的類文件。 但是,該目錄將在Eclipse軟件包瀏覽器中隱藏。 您可以直接在文件系統中進行操作,而無需更改默認的Eclipse視圖過濾器。
然后重建。 它應該可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.