tomcat-web.xml配置

web.xml配置

什么是Web.xml

Web.xml是Web应用部署的重要的描述条件,它支持的元素及属性来自于Servlet规范定义。在tomcat中,Web应用的部署描述信息文件包括tomcat/conf/web.xml以及Web应用的WEB-INF/web.xml下的文件。

ServletContext初始化参数

由于该对象比较常用,这里不做多余的描述。开发的都应该了解该对象。

<context-param>
    <param-name>name</param-name>
    <param-value>value</param-value>
    <description>descript</description>
</context-param>

会话配置

<session-config>
    <!--session会话超时时间,单位为分钟-->
    <session-timeout>30</session-timeout>
</session-config>

Servlet声明及映射

Servlet的声明和映射包含了<servlet>和<servlet-mapping>两部分。

<servlet>
    <servlet-name>myServlet</servlet-name>
    <!--拦截后的处理类-->
    <servlet-class>com.rabbit.tomcat.Hello</servlet-class>
    <!--初始化参数-->
    <init-param>
        <param-name>name</param-name>
        <param-value>value</param-value>
    </init-param>
    <!--大于0表示项目启动的时候初始化-->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <!--可以配置多个拦截路径-->
    <url-pattern>/*</url-pattern>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

应用声明周期监听器

监听器用于监听应用的请求,如果配置多个监听器,那么请求会依次经过监听器,多个监听器就形成了监听器链。

<listener>
    <!--监听器出例类,必须实现javax.servlet.ServletContextListener接口-->
    <listener-class>com.rabbit.tomcat.Hello</listener-class>
</listener>

Filter定义及映射

Filter用于配置web应用过滤器,用于过滤资源请求及响应。

<filter>
    <filter-name>myFilter</filter-name>
    <!--必须实现javax.servlet.Filter接口-->
    <filter-class>com.rabbit.tomcat.Hello</filter-class>
    <!--初始化参数-->
    <init-param>
        <param-name>name</param-name>
        <param-value>value</param-value>
    </init-param>
</filter>

MIME类型映射

MIME:多用途互联网邮件扩展类型,用于设定某类型的扩展名文件将采用何种应用程序打开,当我们通过请求访问该扩展名的资源文件时,浏览器将自动使用指定的应用程序打开返回的资源文件。

<mime-mapping>
    <extension>doc</extension>
    <mime-type>application/msword</mime-type>
</mime-mapping>

注意,这些类型的值都是固定的,工具会带有提示。Tomcat/conf/web.xml文件已经为我们定义了对应的类型,一般不需要额外配置。

欢迎文件列表

当请求地址为web应用的根目录,服务器会尝试在请求地址后面加上欢迎文件并进行请求定向。首先会查找index.html如果没有则查找index.jsp以此类推。

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

错误页面

错误页面可以根据响应状态或者响应异常类配置。

<error-page>
    <error-code>404</error-code>
    <location>/404.html</location>
</error-page>
<error-page>
    <exception-type>java.lang.RuntimeException</exception-type>
    <location>/error.jsp</location>
</error-page>

猜你喜欢

转载自blog.csdn.net/sinat_32366329/article/details/80370812