0x00 前言
在JSP中,我们可以使用web.xml来定义Servlet,web.xml文件称为部署描述文件(Deployment Descriptor,简称DD文件),该文件可以覆盖Servelt中的标识设置,所以总结一下该文件的标签元素以及作用(一个工程不是必须要有web.xml文件)。
0x01 web.xml文件加载过程
<context-param>-> <listener> -> <filter> -> <servlet>
同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用
0x02 web.xml文件元素
2.1 模式文件
web.xml的模式文件是由Sun公司定义的,每一个web.xml文件的根元素中都标明.xml使用哪个模式文件,其他元素放在中。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 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">
2.2 指定欢迎页面
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
这里指定两个欢迎页面,按顺序从第一个找起,如果第一个存在,访问第一个,否则,访问第二个。
2.3 给Servlet命名
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>demo.JspServlet</servlet-class>
</servlet>
将demo包下的JspServlet类命名为jsp.
2.4 给Servlet定制URL
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>hello.do</url-pattern>
</servlet-mapping>
通过在浏览器输入localhost:8080/hello.do
就能匹配到该名为jsp的Servlet(上例就可以通过jsp匹配到JspServlet)。
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
任何扩展名为.do(文件名和路径任意)的url请求都会匹配
localhost:8080/demo.do
localhost:8080/hello.do
2.5 定制初始化参数
可以定制servlet、JSP、Context的初始化参数,然后可以在servlet、JSP、Context中获取这些参数值
<servlet>
<servlet-name>name01</servlet-name>
<servlet-class>demo.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
- servlet 配置Servlet
- servlet-name 定义Servlet的名字
- servlet-class 指定实现这个servlet的类
- init-param 定义Servlet的初始化参数和参数值,可有多个init-param
- load-on-startup 指定当Web应用启动时,装载Servlet的次序
- 当值为正数或零时,Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet
经过上面的配置,在Servlet中就能通过调用getServletConfig().getInitParameter("param1")
获得参数名对应的值。
2.6 指定错误处理页面
“错误码”来指定错误处理页面
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
“异常类型”来指定错误处理页面
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>
2.7 配置session的有效时间
<session-config>
<session-timeout>30</session-timeout>
</session-config>
这里默认是30分钟
2.8 mime-type映射
<mime-mapping>
<extension>zip</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
避免在浏览器直接打开
2.9 设置监听器
<listener>
<listener-class>demo.demoLisenet</listener-class>
</listener>
2.10 设置过滤器
设置一个编码过滤器,过滤所有资源
<filter>
<filter-name>XCharaSetFilter</filter-name>
<filter-class>demo.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.11 定义了WEB应用的名字
<display-name>Test</display-name>
2.12 声明WEB应用的描述信息
<description>This is a test Page</description>
2.13 声明应用范围内的初始化参数
<context-param>
<param-name>web</param-name>
<param-value>log4</param-value>
</context-param>
取得值得方法:
ServletContext app =ServletContextEvent.getServletContext();
context-paramValue= app.getInitParameter("web");
- context-paramValue context-param的值
- web context-param的键,上面定义的
0x03 小结
XML文档的样式与内容分离,可以帮我们快速寻找并提取有用的数据信息,所以学一些关于XML的配置还有很有用的,这里的只是通过在网上收集,还有Tomcat中默认的web.xml文件中一些常见的元素,以后要用到时再查阅资料。
版权声明:本文为qq_39629343原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。