解决方案 »

  1.   

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
        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_3_0.xsd">
      <servlet>
        
        <servlet-name>First</servlet-name>
        <servlet-class>web.First</servlet-class>
      </servlet>  <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/servlet/First</url-pattern>
      </servlet-mapping></web-app>
      

  2.   

    package web;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;public class First extends HttpServlet { /**
     * Constructor of the object.
     */
    public First() {
    super();
    } /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    } /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the GET method");
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the POST method");
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
    } /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
    // Put your code here
    }}
      

  3.   

    工程  右键  export war 包 到 tomcat 的webapps 下面 ,tomcat 启动成功 
    http://localhost:8080/webDemo/servlet/First  红色部分根据实际情况更改
      

  4.   

    Tomcat启动了吗?启动了以后浏览器输入-----localhost:8080/你的项目名/First
    你的端口号是多少就把8080改成多少,如果你的 Web Context-root设置的是/,那就把上面的“你的项目名去掉”
      

  5.   

    把访问路径贴出来看看,是不是没加servlet
      

  6.   

    右键没有export war 这个选项
      

  7.   

    我访问我这个工程的index.jsp就能成功,但是访问Servlet就是无论如何都是无法成功的
      

  8.   


    代码没有问题,运行结果
    This is class web.First, using the GET method
    检查下自己的运行环境
      

  9.   

    把你的项目打成包,然后放tomact下
      

  10.   

    你要打成war包,放到tomcat的webapp目录下
      

  11.   

    项目右键export打成war包,放到tomcat的webapp目录下
      

  12.   

    你没有发布成功,看看你MYECLIPSE对应的TOMCAT目录下SERVLET的CLASS文件是否生成了,在MYECLIPSE里面重新发布一下