eclipse创建servlet

1、创建类:com.itheima.servlet        HelloServlet

 

编辑HelloServlet.java类

 

编辑完成,新增5个方法。

A、配置文件实现servlet接口。

将5个方法中的service方法移到最前面(当客户端访问,后台会打印HelloServlet…)。

 

B、配置servlet,告诉服务器servlet服务。

配置文件web.xml。

<!-- 向tomcat反映,这里的servlet,名称为HelloServlet,具体路径是com.itheima.servlet.HelloServlet  -->

  <servlet>

    <servlet-name>HelloServlet</servlet-name>

    <servlet-class>com.itheima.servlet.HelloServlet</servlet-class>

  </servlet>

<!-- 注册servlet映射。servlet-name跟上面具体servlet服务对应,url-pattern:客户端访问路径 -->

  <servlet-mapping>

    <servlet-name>HelloServlet</servlet-name>

    <url-pattern>/abc</url-pattern>

  </servlet-mapping>

 启动报错,由于url-pattern配置不正确。

 

严重: A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloServlet]]

     at java.util.concurrent.FutureTask.report(Unknown Source)

访问测试。

 

2、继承测试

创建类,继承父类的service方法。

public class HelloServlet02 extends HttpServlet{

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("HelloServlet...");

    }  

}

 

配置web.xml文件。

 

测试其他两种方法。

需要注释service方法。

 

猜你喜欢

转载自www.cnblogs.com/sunnyyangwang/p/9427012.html