今天学习cookie的时候,建好了一个动态Web工程后,重启Tomcat突然启动不了。然后在网上寻找答案也没找到我的这种问题,后来只好直接寻找。最后发现了造成这个问题的原因,现将这个原因记录在这里,避免以后遇到相同的问题忘记。
在新版的Eclipse中,新建的servlet会在servlet中加入注解@WebServlet("/Servlet名称")如图所示:
1 @WebServlet("/LoginServlet") 2 public class LoginServlet extends HttpServlet { 3 4 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 5 6 String uname = request.getParameter("uname"); 7 String upwd = request.getParameter("upwd"); 8 9 response.setContentType("text/html;charset=utf-8"); 10 11 if("admin".equals(uname)&&"admin".equals(upwd)) {//正确 12 13 //请求转发方式(对象是request获取的) 14 request.getRequestDispatcher("login_success.html").forward(request, response); 15 16 }else { 17 response.getWriter().write("登录失败!"); 18 } 19 20 } 21 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 doGet(request, response); 24 } 25 26 }
在类定义上面自动添加了一个注解@WebServlet("/LoginServlet"),这相当于web.xml中这个Servlet的url-pattern,然后我又自己在web.xml中配置了url-pattern也为/LoginServlet,这就让Tomcat服务器认为有两个相同的url-patten,而这是不允许的,所以无法启动Tomcat服务器,只需要将其中一个改掉即可解决。