繁体   English   中英

当我在doPost中更改servlet内容时,什么也没发生

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM