IDEA中的JSP无法跳转到Servlet问题

折腾了一天,终于弄好,结果很扎心。

建立一个空白的JavaWeb项目后,建立在src中建立一个Servlet。

IDEA默认生成代码↓

@WebServlet(name = "Servlet")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

Jsp中的内容之后一个跳转的submit

<html>
  <head>
    <title>标题</title>
  </head>
  <body>
    <form action="Servlet">
      <input type="submit" value="123">
    </form>
  </body>
</html>

然后无论怎么调,都是一如既往的404。

后来在Eclipse中建了一个项目,发现Servlet中有猫腻。

除了默认生成的代码有多有少之外,最重要的是注解不同。
下面是Eclipse中的代码

/**
 * Servlet implementation class WoshishachazhengServlet
 */
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Servlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

区别点在于:

Eclipse的注解为:
@WebServlet("/Servlet")
IDEA的注解为:
@WebServlet(name = "Servlet")

然后把IDEA中的注解改成Eclipse中的形式,操作成功,bug解决。

你可能感兴趣的:(IDEA中的JSP无法跳转到Servlet问题)