servlet的xml配置详解

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
 
    <servlet>
        <!--自定义,一般为类名-->
        <servlet-name>servletDemo1</servlet-name>
        <!--一定是package + .类名-->
        <servlet-class>day08_servlet.ServletDemo1</servlet-class>
    </servlet>
    <!--给Servlet提供(映射)一个可供客户端访问的URI-->
    <servlet-mapping>
        <!--和servlet中的name必须相同-->
        <servlet-name>servletDemo1</servlet-name>
        <!-- servlet的映射路径 -->
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
   
</web-app>

这里要注意的是servlet-mapping的url-pattern中的这个映射路径 / 一定不能忘记!!!

配置完成后,启动tomcat服务器,然后在浏览器中输入http://localhost:8080/servlet即可。

与其战胜敌人一万次,不如战胜自己一次。
原文地址:https://www.cnblogs.com/hyjh/p/11837711.html