这是我的 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,它起作用了:)