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

被折叠的 条评论
为什么被折叠?



