web.xml

简述

  一般的web工程中都会用到web.xml,web.xml主要用来配置,可以方便的开发web工程。web.xml主要用来配置Filter、Listener、Servlet等。但是要说明的是web.xml并不是必须的,一个web工程可以没有web.xml文件。

Tomcat容器的加载过程

  WEB容器的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet。这些元素可以配置在文件中的任意位置

写法

初始配置

  什么都没有的web.xml,注意metadata-complete为true则不会扫描WebServlet注解,所以为flase就要设为false或不写

<?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"
         metadata-complete="false">
    
</web-app>

欢迎页面

  下面的例子指定了2个欢迎页面,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index1.jsp</welcome-file>
</welcome-file-list>

指定Servlet的URL

  经过这样设置之后,你就可以通过浏览器访问url-pattern设定的URL访问你的servlet了

<servlet>
    <servlet-name>自定义名字</servlet-name>
    <servlet-class>全限定类名</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>自定义名字(要和上面一样)</servlet-name>
    <url-pattern>/你要设置的URL</url-pattern>
</servlet-mapping>

 

 

posted @ 2021-06-22 09:35  艾尔夏尔-Layton  阅读(32)  评论(0编辑  收藏  举报