对于下面提到的一组简单代码,我仍然会收到错误404。
HTTP状态404 - 未找到
输入状态报告
Message / DemoServlet
描述源服务器没有找到目标资源的当前表示,或者不愿意透露该目标资源是否存在。
Apache Tomcat / 9.0.0.M26
它曾经工作,但现在它没有。我已经尝试重新安装eclipse,它仍然没有帮助。我用的是MackBook Pro。 我的代码:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/DemoServlet" method="post">
Enter name: <input type="text" name="n"/><br>
<input type="submit" value="Validate" name="s"/>
</form>
</body>
</html>
的Servlet
package app.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DemoServlet")
public class DemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DemoServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("n");
if(name.equals("Rajiv")) {
out.println("Welcome Rajiv");
}else
out.println("Please try again.");
}
}