2

这是我的 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 类?或者有什么问题?提前谢谢你


我做了Project->clean,它起作用了:)

4

2 回答 2

0

您的应用程序可能缓存在 tomcat 目录 tomcat\work\Catalina\localhost 下。尝试从此目录中删除您的应用程序并重新部署您的应用程序或简单地重新启动 tomcat。

如果上述方法没有帮助,那么 eclipse WAR 创建或部署肯定会出现一些故障。构建您的 WAR,并确保它包含更新的类文件。关键是,检查部署 WAR 的 Tomcat 目录中的文件日期。即使您正在部署一个全新的清理过的 WAR 并删除所有文件夹,但仍有较旧的缓存文件在其中,可能会发生这种情况,这可能是因为 Eclipse 保留它们以节省编译时间,认为它们没有任何更改。确保 Tomcat 中的 war 或 webapp 文件夹包含最新的类文件,然后就可以了。

祝你好运!

于 2013-05-04T12:54:36.897 回答
0

Giorgi,这应该可以通过 Build > Clean 轻松解决。如果它仍然顽固,请注意在 Eclipse 中,生成的类会写入 Java Build Path 实用程序中指定的目录中:

在此处输入图像描述

您可以手动删除此处创建的类文件。该目录将隐藏在 Eclipse 包资源管理器中。您可以直接在文件系统中进行更改,而不是更改默认的 Eclipse 视图过滤器。

然后重建。它应该可以解决您的问题。

于 2013-05-04T14:42:50.970 回答