整个上午我都遇到了这个问题,我似乎无法理解为什么。
这是我的servlet。很简单:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class hello
*/
@WebServlet("/hello")
public class hello extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public hello() {
super();
// 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().write("test");
}
/**
* @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);
}
}
当我启动tomcat服务器并转到http://localhost:8080/testh/hello时,收到错误消息: HTTP状态404 - / testh / hello
为什么呢?服务器已启动,我在项目" testh"下运行了这个特定的servlet / hello。我不明白。请帮忙。
答案 0 :(得分:0)
尝试拨打http://localhost:8080/hello
因为你所有的项目都在根
之下/
然后你有映射/hello
此外,如果您确切需要http://localhost:8080/testh/hello
,则应使用路径 / testh 部署应用,然后HTTP调用将路由到 / testh / hello ,因为您的servlet路径是 / hello +应用路径 / testh