简体   繁体   English

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

[英]When I change servlet content in doPost, nothing happens

Here is piece from my HTML code: 这是我的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>

and here is servletContextListener: 这是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();
}

} }

and here is my servlet doPost : 这是我的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>"); 
//  }

Problem is that, when i run welcome.html and push login button, still old code does its work. 问题是,当我运行welcome.html并按下登录按钮时,仍旧的代码可以正常工作。 I'mean i've commented this part: 我的意思是我已经评论了这一部分:

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

but still, when i push button, this, commented block executes... so i can't change anything there.. anybody can explain how can i restart my servlet class? 但是仍然,当我按下按钮时,此注释块将执行...因此我无法在此处进行任何更改..任何人都可以解释如何重新启动Servlet类? or what's the problem? 还是什么问题? thank you in advace 谢谢你


i did Project->clean and it worked :) 我做了项目->清洁,它的工作:)

Your application may be cached under the tomcat directory tomcat\\work\\Catalina\\localhost. 您的应用程序可能会缓存在tomcat目录下的tomcat \\ work \\ Catalina \\ localhost下。 Try deleting your application from this directory and redeploy your application or simple restart tomcat. 尝试从此目录中删除您的应用程序,然后重新部署您的应用程序,或者简单地重新启动tomcat。

If above does not help then there is surely some glitch with the eclipse WAR creation or deployment.Build your WAR, and make sure it contains the updated class files. 如果上面的方法没有帮助,那么肯定会在Eclipse WAR的创建或部署方面出现故障。构建您的WAR,并确保它包含更新的类文件。 The key is, to check the file dates in the Tomcat directory of where you deployed the WAR. 关键是要检查部署WAR的Tomcat目录中的文件日期。 It may happen even though you are deploying an entirely new cleaned WAR, and deleting all folders, there were still older cached files in there, probably because Eclipse keeps them to save compile time, thinking they don't have any changes. 即使您部署了一个全新的WAR,并删除了所有文件夹,也可能发生这种情况,因为那里仍然有较旧的缓存文件,这可能是因为Eclipse认为它们没有任何变化可以节省编译时间。 Make sure the war or webapp folder in your Tomcat contains the latest class files and then you should be good. 确保Tomcat中的war或webapp文件夹包含最新的类文件,然后您就可以了。

Good Luck! 祝好运!

Giorgi, this should be easily addressed by Build > Clean. Giorgi,应通过“构建”>“清理”轻松解决此问题。 If it continues to be stubborn, take note that in Eclipse, the generated classes are written into the directory specified in the Java Build Path utility: 如果仍然顽固,请注意,在Eclipse中,将生成的类写入Java Build Path实用程序中指定的目录中:

在此处输入图片说明

You can manually delete the class files created here. 您可以手动删除此处创建的类文件。 The directory will be hidden in Eclipse package explorer though. 但是,该目录将在Eclipse软件包浏览器中隐藏。 Instead of changing the default Eclipse view filters, you can do it directly in the file system. 您可以直接在文件系统中进行操作,而无需更改默认的Eclipse视图过滤器。

Then rebuild. 然后重建。 It should fix your problem. 它应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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