我在tomcat的webapps目录下新建一个TestServlet目录,然后再建了WEB-INF目录和web.xml文件,web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
 
    <servlet>
       <servlet-name>yuzhengzhong</servlet-name>
       <servlet-class>TestServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
       <servlet-name>yuzhengzhong</servlet-name>
       <url-pattern>/testServlet</url-pattern>
    </servlet-mapping>
 
 </web-app>WEB-INF目录下有classes和lib目录,lib目录里面没东西,classes目录下放了TestServlet.class文件,TestServlet.java的内容如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>yuzhengzhong's first servlet</title>");
out.println("</head>");
out.println("<body>");
out.println(new Date());
out.println("<br>");
out.println("Welcome to my first servlet~");
out.println("</body>");
out.println("</html>");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}}启动tomcat,没有错误,正常运行,在浏览器中输入http://localhost:8080/TestServlet/testServlet,为什么会出现404错误呀,错误如下:
HTTP Status 404 - /TestServlet/testServlet--------------------------------------------------------------------------------type Status reportmessage /TestServlet/testServletdescription The requested resource (/TestServlet/testServlet) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.16

解决方案 »

  1.   

    补充下,输入http://localhost:8080能够显示tomcat的主页
      

  2.   

    java文件加上 public TestServlet() {
    super();
    } public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    } public void init() throws ServletException {
    // Put your code here
    }
      

  3.   

    是你的路径问题,我不知道楼主所在的工程名称是什么,正确访问应该是http://localhost:8080/工程名/testServlet.
      

  4.   

    404 路径出错了!有可能是配置文件中的url与class的路径出错!
    或者是4楼的那种方法输入http://localhost:8080/工程名/testServlet楼主可以试一下啊
      

  5.   

    试试http://localhost:8080/工程名/testServlet
    再看看你的servlet是不是放在缺省包下,
    <servlet-class>TestServlet</servlet-class>
      

  6.   

    LZ你这样写看看行吧,浏览器的是调用doGet方法
    public class TestServlet extends HttpServlet{    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
                   doPost(req,resp);
               }    @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
             resp.setContentType("text/html;charset=utf-8");
            PrintWriter out=resp.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<title>yuzhengzhong's first servlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println(new Date());
            out.println("<br>");
            out.println("Welcome to my first servlet~");
            out.println("</body>");
            out.println("</html>");    }}
      

  7.   

    默认情况下是调用doGet方法的,如果你设置了就可以调用doPost方法
      

  8.   

    TestServlet.java 在是否在SRC 目录下面  再仔细检查发布的 工程
      

  9.   

    又是一个痴情的人...
    看log啊,大哥们././//logs/localhost.*最新的那个文件.看里面出错没..
    对症下药,瞎猜写什么啊..还解决不了就把log 贴出来......
      

  10.   

    页面上的form里面配置method="post",调用doPost()方法.404很明显是路径问题,
      

  11.   

    http://localhost:8080/TestServlet/testServlet你的 TestServlet 是项目名 还是 Servlet 啊
      

  12.   

    楼主的问题在于,你在web.xml的配置与你请求的路径不符合.
    更改方案如下:在web.xml中将<url-pattern>/testServlet</url-pattern>
    更改为:<url-pattern>/testServlet</url-pattern>
    <url-pattern>/****/testServlet</url-pattern>***代表你的工程目录既WEB-INF的上 一级.
      

  13.   

    你的浏览器输入的http://localhost:8080/TestServlet/testServlet
    改为http://localhost:8080/testServlet/testServlet
      

  14.   

    是不是把你的项目放到F:\apache-tomcat-6.0.20\apache-tomcat-6.0.20\webapps下面?那你应该是输入http://localhost:8080/项目名称/testServlet
      

  15.   

    结贴了,我也不知道问题出在哪里,可是我用myeclipse部署后就可以了,自己部署它就是出错我晕~算了,以后还是用myeclipse部署了,不再自己直接部署了~