原文地址:https://blog.csdn.net/zwl18210851801/article/details/78425057
1.web应用名称设置
<display-name>应用名称</display-name>
2.web应用描述
<description>应用描述</description>
3.配置监听器
listener作用是注册一个监听器类,可以自己建立一个监听器类,可以对某些事件进行监听。
<!-- 日志监听 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 配置日志文件地址-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/META-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>120000</param-value>
</context-param>
log4jRefreshInterval 该设置作用为容器会每120秒自动扫描log4j的配置文件,改变日志级别时不需要重启服务器。
<!-- 自动装载ApplicationContext的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring 提供了ServletContentListener的一个实现类ContextLoaderListener监听器,在启动tomcat容器时,该类的作用是自动装载ApplicationContext的配置信息,如果没有设置ContextLoaderListener的初始参数会使用默认参数web-inf路径下的application,xml文件,如果需要读取多个配置文件或修改默认路径,可以利用context-param设置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 解决可能出现的内存泄漏问题-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
4.设置应用的环境参数,该参数可以在jsp中通过方法获取,也可以在java代码中获取,放在监听器后面
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
在jsp中获取可以用{initparam.contextConfigLocation}获取对应的value值
在代码中可以通过string param=getservletContent().getParamter(‘contextConfigLocation’);获取
5.设置web应用的过滤器
字符集过滤器,用于处理项目中的乱码问题
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
encoding 作用为设置解析请求的编码方式为utf-8
forceEncoding=true 的意思是无论客户端请求是否包含编码必须使用过滤器里的编码方式解析请求
filter-mapping 设置的是该过滤器过滤的url范围,即当符合情况的url需要经过该过滤器处理
浏览器form表单只支持get和post请求,而delete,put等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,似的支持get,post,put,delete,请求,该过滤器为HiddenHttpMethodFilter。HiddenHttpMethodFilter必须放在DispatcherServlet之前。
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6.servlet配置
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置缺省Servlet
如果某个Servlet的映射路径仅仅为一个正斜杠(/),那么这个Servlet就成为当前Web应用程序的缺省Servlet。
凡是在web.xml文件中找不到匹配的元素的URL,它们的访问请求都将交给缺省Servlet处理,也就是说,缺省Servlet用于处理所有其他Servlet都不处理的访问请求。
7.进行session失效时间设定,单位为分钟
<session-config>
<session-timeout>30</session-timeout>
</session-config>
8.设置项目欢迎页
<!-- 配置在web.xml中的一个欢迎页,用于当用户在url中输入工程名称或者输入web容器url(如http://localhost:8080/或http://localhost:8080/test/)时直接跳转的页面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
9.配置错误页
当出现404错误时跳转错误页面
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
10.jsp配置
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
<jsp-property-group>元素主要子元素
<description>:设定的说明;
<display-name>:设定名称;
<url-pattern>:设定值所影响的范围,如:/CH2 或 /*.jsp;
<el-ignored>:若为true,表示不支持EL 语法;
<scripting-invalid>:若为true,表示不支持<% scripting %>语法;
<page-encoding>:设定JSP 网页的编码;
<include-prelude>:设置JSP 网页的抬头,扩展名为.jspf;
<include-coda>:设置JSP 网页的结尾,扩展名为.jspf。
trim-directive-whitespaces=true,作用是删除页面中的多余的空格
11.预防xss攻击
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>true</param-value>
</context-param>
12.验证码web配置
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.linkage.eduselfapp.core.kaptcha.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>black</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>5</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>25</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>90</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>45</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>23456789</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>??,Arial,Courier</param-value>
</init-param>
<init-param>
<param-name>kaptcha.obscurificator.impl</param-name>
<param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>
</init-param>
<init-param>
<param-name>kaptcha.noise.impl</param-name>
<param-value>com.google.code.kaptcha.impl.NoNoise</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
13.设置访问权限
<security-constraint>
<web-resource-collection>
<web-resource-name>web</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
的子元素 是可选的,如果没有 元素,这表示将禁止所有 HTTP 方法访问相应的资源。
子元素 需要和 相配合使用,但可以被单独使用。如果没有 子元素,这表明任何身份的用户都可以访问相应的资源。也就是说,如果 中没有 子元素的话,配置实际上是不起中用的。如果加入了 子元素,但是其内容为空,这表示所有身份的用户都被禁止访问相应的资源。
security-constriaint元素的用途是用来指示服务器使用何种验证方法了.此元素在web.xml中应该出现在login-config 的紧前面
login-config 为设置http规范,base64