我正在尝试创建一个简单的servlet例子我正在使用java 1.7和tomcat服务器7.当我部署我的项目时,我收到HTTP 404错误。这是我的代码:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class IMCServlet
*/
public class IMCServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public IMCServlet() {
// 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.setContentType("text/html");
PrintWriter out =response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE> Calculer votre IMC</TITLE></HEAD>");
out.println("<BODY>");
out.println("<Text goes here...>");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
这是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app id="WebApp_ID">
<display-name>IMCServlet</display-name>
<servlet>
<servlet-name>IMCServlet</servlet-name>
<display-name>IMCServlet</display-name>
<description>Servlet qui stock le IMC dans une base de données</description>
<servlet-class>ca.uqac.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IMCServlet</servlet-name>
<url-pattern>/IMCServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Index.html</welcome-file>
<welcome-file>Index.htm</welcome-file>
<welcome-file>Index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
有人可以帮忙,谢谢。 毕竟我弄清楚了,我不得不改变这个
<servlet-class>ca.uqac.Servlet</servlet-class>
到
<servlet-class>ca.uqac.Servlet.IMCServlet</servlet-class>
答案 0 :(得分:0)
更改
< servlet-class >
ca.uqac.Servlet
< /servlet-class >
到
< servlet-class >
ca.uqac.IMCServlet
< /servlet-class >
希望IMCServlet
类在ca.uqac包中