Tomcat之Web 应用配置
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
web.xml
是
web
应用的描述文件 它支持的元素及属性来自于
Servlet
规范定义 。 在 Tomcat 中
Web
应用的描述信息包括
tomcat/conf/web.xml
中默认配置 以及
Web 应用 WEB-INF/web.xml
下的定制配置
1 .ServletContext 初始化参数
我们可以通过添加
ServletContext
初始化参数它配置了一个键值对这样我们可以在应用程序中使用 javax.servlet.ServletContext.getInitParameter()
方法获取参数。
<context‐param>
<param‐name>contextConfigLocation</param‐name>
<param‐value>classpath:applicationContext‐*.xml</param‐value>
<description>Spring Config File Location</description>
</context‐param>
2 .会话配置
用于配置
Web
应用会话包括 超时时间、
Cookie
配置以及会话追踪模式。它将覆盖 server.xml 和
context.xml
中的配置。
<session‐config>
<session‐timeout>30</session‐timeout>
<cookie‐config>
<name>JESSIONID</name>
<domain>www.itcast.cn</domain>
<path>/</path>
<comment>Session Cookie</comment>
<http‐only>true</http‐only>
<secure>false</secure>
<max‐age>3600</max‐age>
</cookie‐config>
<tracking‐mode>COOKIE</tracking‐mode>
</session‐config>
配置解析
1session‐timeout 会话超时时间单位分钟
2cookie‐config 用于配置会话追踪Cookie
nameCookie的名称
domainCookie的域名
pathCookie的路径
comment注释
http‐onlycookie只能通过HTTP方式进行访问JS无法读取或修改此项可以增
加网站访问的安全性。
secure此cookie只能通过HTTPS连接传递到服务器而HTTP 连接则不会传递该
信息。注意是从浏览器传递到服务器服务器端的Cookie对象不受此项影响。
max‐age以秒为单位表示cookie的生存期默认为‐1表示是会话Cookie浏览器
关闭时就会消失。
3 tracking‐mode 用于配置会话追踪模式Servlet3.0版本中支持的追踪模式
COOKIE、URL、SSL
A. COOKIE : 通过HTTP Cookie 追踪会话是最常用的会话追踪机制 而且
Servlet规范也要求所有的Servlet规范都需要支持Cookie追踪。
B. URL : URL重写是最基本的会话追踪机制。当客户端不支持Cookie时可以采
用URL重写的方式。当采用URL追踪模式时请求路径需要包含会话标识信息Servlet容器
会根据路径中的会话标识设置请求的会话信息。如
http//www.myserver.com/user/index.html;jessionid=1234567890。
C. SSL : 对于SSL请求 通过SSL会话标识确定请求会话标识。
3. Servlet配置
Servlet
的配置主要是两部分
servlet
和
servlet-mapping
<servlet>
<servlet‐name>myServlet</servlet‐name>
<servlet‐class>cn.itcast.web.MyServlet</servlet‐class>
<init‐param>
<param‐name>fileName</param‐name>
<param‐value>init.conf</param‐value>
</init‐param>
<load‐on‐startup>1</load‐on‐startup>
<enabled>true</enabled>
</servlet>
<servlet‐mapping>
<servlet‐name>myServlet</servlet‐name>
<url‐pattern>*.do</url‐pattern>
<url‐pattern>/myservet/*</url‐pattern>
</servlet‐mapping>
配置说明
1 servlet‐name : 指定 servlet 的名称 该属性在 web.xml 中唯一。2 servlet‐class : 用于指定 servlet 类名3 init‐param 用于指定 servlet 的初始化参数 在应用中可以通过HttpServlet.getInitParameter 获取。4 load‐on‐startup 用于控制在 Web 应用启动时 Servlet 的加载顺序。 值小于 0web 应用启动时不加载该 servlet, 第一次访问时加载。5 enabled true false 。 若为 false 表示 Servlet 不处理任何请求。6 url‐pattern 用于指定 URL 表达式一个 servlet‐mapping 可以同时配置多个 url‐pattern 。
Servlet
中文件上传配置
<servlet>
<servlet‐name>uploadServlet</servlet‐name>
<servlet‐class>cn.itcast.web.UploadServlet</servlet‐class>
<multipart‐config>
<location>C://path</location>
<max‐file‐size>10485760</max‐file‐size>
<max‐request‐size>10485760</max‐request‐size>
<file‐size‐threshold>0</file‐size‐threshold>
</multipart‐config>
</servlet>
配置说明
1 location 存放生成的文件地址。2 max‐file‐size 允许上传的文件最大值。 默认值为 ‐1 表示没有限制。3 max‐request‐size 针对该 multi/form‐data 请求的最大数量默认值为 ‐1 表示无限制。4 file‐size‐threshold 当数量量大于该值时 内容会被写入文件。
4 .Listener配置
Listener
用于监听
servlet
中的事件例如
context
、
request
、
session
对象的创建、修改、删除并触发响应事件。Listener
是观察者模式的实现在
servlet
中主要用于对context、
request
、
session
对象的生命周期进行监控。在
servlet2.5
规范中共定义了
8
中Listener。在启动时
ServletContextListener
的执行顺序与
web.xml
中的配置顺序一致 停止时执行顺序相反。
<listener>
<listener‐class>org.springframework.web.context.ContextLoaderListener</listener‐class>
</listener>
5.Filter配置
filter
用于配置
web
应用过滤器 用来过滤资源请求及响应。 经常用于认证、日志、加密、数据转换等操作 配置如下
<filter>
<filter‐name>myFilter</filter‐name>
<filter‐class>cn.itcast.web.MyFilter</filter‐class>
<async‐supported>true</async‐supported>
<init‐param>
<param‐name>language</param‐name>
<param‐value>CN</param‐value>
</init‐param>
</filter>
<filter‐mapping>
<filter‐name>myFilter</filter‐name>
<url‐pattern>/*</url‐pattern>
</filter‐mapping>
配置说明
1 filter‐name 用于指定过滤器名称在web.xml中过滤器名称必须唯一。
2 filter‐class 过滤器的全限定类名 该类必须实现Filter接口。
3 async‐supported 该过滤器是否支持异步
4 init‐param 用于配置Filter的初始化参数 可以配置多个 可以通过
FilterConfig.getInitParameter获取
5 url‐pattern 指定该过滤器需要拦截的URL。
6 .欢迎页面配置
welcome-file-list 用于指定web应用的欢迎文件列表。
<welcome‐file‐list>
<welcome‐file>index.html</welcome‐file>
<welcome‐file>index.htm</welcome‐file>
<welcome‐file>index.jsp</welcome‐file>
</welcome‐file‐list>
尝试请求的顺序从上到下。
7. 错误页面配置
error-page
用于配置
Web
应用访问异常时定向到的页面支持
HTTP
响应码和异常类两种
形式。
<error‐page>
<error‐code>404</error‐code>
<location>/404.html</location>
</error‐page>
<error‐page>
<error‐code>500</error‐code>
<location>/500.html</location>
</error‐page>
<error‐page>
<exception‐type>java.lang.Exception</exception‐type>
<location>/error.jsp</location>
</error‐page>