Tomcat中apache-tomcat-8.5.60\conf\web.xml文件的配置说明

本文介绍了Apache Tomcat 8.5.60中`conf/web.xml`的配置,包括默认的servlet配置,用于处理默认请求和JSP请求,以及默认超时设置和欢迎页面配置。同时,探讨了项目中自定义的web.xml配置,如通过`<init-param>`设置初始化参数,使用`<error-page>`定义错误页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默认web.xml配置

默认的配置中,有2个servlet配置。defaultjsp两个servlet。

default处理默认请求,拦截<url-pattern>/</url-pattern>请求。

jsp处理JSP请求,拦截<url-pattern>*.jsp</url-pattern><url-pattern>*.jspx</url-pattern>请求。

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.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-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- The mappings for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

默认的session超时配置是30分钟

  <!-- ==================== Default Session Configuration ================= -->
  <!-- You can set the default session timeout (in minutes) for all newly   -->
  <!-- created sessions by modifying the value below.                       -->

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

提供了默认的欢迎页配置

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

项目中的web.xml配置

<context-param>标签

    <context-param>
        <param-name>test</param-name>
        <param-value>123456</param-value>
    </context-param>

配置初始化参数,通过servlet获取并进行逻辑操作。

public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Enumeration<String> parameterNames = req.getServletContext().getInitParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            String value = req.getServletContext().getInitParameter(key);
            System.out.println(key + ":" + value);
        }
    }
}

<session-config>标签

    <session-config>
        <session-timeout>40</session-timeout><!--session有效期-->
        <cookie-config>
            <name>JSESSIONID</name><!--Cookie携带的key-->
            <domain>www.gosuncn.fun</domain>
            <path>/</path>
            <comment>Session Cookie</comment>
            <http-only>true</http-only><!--只能通过http请求携带,不能js脚本携带-->
            <secure>false</secure><!--只能通过https协议请求访问-->
            <max-age>3600</max-age><!--cookie有效期-->
        </cookie-config>
        <tracking-mode>COOKIE</tracking-mode><!--跟踪模式。除了COOKIE还有URL和SSL方式-->
    </session-config>

<error-page>标签配置错误页

    <error-page>
        <location>web.xml</location>
        <error-code>500</error-code>
        <exception-type>xxx</exception-type>
    </error-page>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值