其他分享
首页 > 其他分享> > 05-Web.xml解读

05-Web.xml解读

作者:互联网

Web.xml解读

<servlet>
<!--  起的别名  -->
    <servlet-name>HelloServlet</servlet-name>
<!--   servlet程序的全类名  -->
    <servlet-class>com.tian.servlet.HelloServlet</servlet-class>
</servlet>
    <servlet-mapping>
<!--    告诉服务器,我配置的地址是给哪个servlet程序使用    -->
        <servlet-name>HelloServlet</servlet-name>
<!--     访问地址   -->
        <url-pattern>/hello</url-pattern>
<!--   / 斜杠在服务器解析的时候 地址为 http://ip:port/工程路径
      /hello 表示地址为  http://ip:port/工程路径/hello
     -->
    </servlet-mapping>

Mapping问题

  1. 一个Servlet可以指定一个映射路径

     <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.tian.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/helloServlet</url-pattern>
        </servlet-mapping>
    
  2. 一个Servlet可以指定多个映射路径

     <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.tian.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/helloServlet</url-pattern>
        </servlet-mapping>
    
     <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.tian.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/helloServlet1</url-pattern>
        </servlet-mapping>
    
  3. 一个Servlet可以指定通用映射路径

     <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.tian.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
  4. 指定一些后缀或者前缀等等…

    //注意点,*前面不能加项目映射的路径 
    <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.tian.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    

优先级问题

<!-- 404 -->
<servlet>
        <servlet-name>ErrorServlet</servlet-name>
        <servlet-class>com.tian.servlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ErrorServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

标签:xml,Web,映射,05,com,路径,tian,servlet,HelloServlet
来源: https://blog.csdn.net/xixihaha_coder/article/details/119077658