2014-03-13 51 views
0

我想創建一個簡單的servlet例子,我使用java 1.7和tomcat服務器7.當我部署我的項目時,我得到一個HTTP 404錯誤。這裏是我的代碼:tomcat下的Servlet錯誤

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

部署時,您不會獲得404。當您嘗試向未處理的URI發送請求時,您會得到一個404。你嘗試了什麼URI? –

+0

感謝您的回答,URI是http:// localhost:8080/IMC/servlet/ca.uqac.Servlet.IMCServlet – user3382344

+0

現在您是否在部署描述符(web.xml)中看到了該路徑的任何處理程序? –

回答

0

變化

< servlet-class > 
     ca.uqac.Servlet 
< /servlet-class > 

< servlet-class > 
     ca.uqac.IMCServlet 
    < /servlet-class > 

IMCServlet類是在CA .uqac包

+0

謝謝,我試過了,但沒有奏效。 – user3382344