web.xml配置详解

##1.常用的示例web.xml中的节点注释说明

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xmlns="http://java.sun.com/xml/ns/javaee"
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
         <!-- 应用名称-->
 	<display-name>jeesns</display-name>
        <!-- 应用范围的参数初始化-->
 	<context-param>
 		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mybatis.xml</param-value>
 	</context-param>
	<context-param>
		<param-name>defaultHtmlEscape</param-name>
		<param-value>true</param-value>
	</context-param>
       <!-- 过滤器-->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
       <!-- 过滤映射-->
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
		<filter-name>XssSqlFilter</filter-name>
		<filter-class>com.lxinet.jeesns.core.filter.XssFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>XssSqlFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
	</filter-mapping>
      <!--监听器-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
 	<!--servlet配置-->
 	<servlet>
 		<servlet-name>springmvc</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映射-->
 	<servlet-mapping>
 		<servlet-name>springmvc</servlet-name>
 		<url-pattern>/</url-pattern>
 	</servlet-mapping>
 	<!--会话配置-->
 	<session-config>
 		<session-timeout>14400</session-timeout>
 	</session-config>
        <!--servlet映射-->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/index</url-pattern>
	</servlet-mapping>
       <!--错误页面配置-->
	<error-page>
		<error-code>404</error-code>
		<location>/404.html</location>
	</error-page>
	<error-page>
		<error-code>400</error-code>
		<location>/error.html</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/error.html</location>
	</error-page>
       <!--   欢迎页面设置-->
	<welcome-file-list>
		<welcome-file>index</welcome-file>
 	</welcome-file-list>
</web-app>

##2. 更完整的节点说明 1.Web应用图标:指出IDE和GUI工具用来表示web应用的大图标和小图标

<icon>
        <small-icon>/images/app_small.gif</small-icon>
        <large-icon>/images/app_large.gif</large-icon>
</icon>

2.Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称

<display-name>Tomcat Example</display-name>

3.Web 应用描述:给出于此相关的说明性文本

<disciption>Tomcat Example servlets and JSP pages.</disciption>

4.上下文参数:声明应用范围内的初始化参数

<context-param>
      <param-name>参数名</para-name>
      <param-value>参数值</param-value>
      <description>参数描述</description>
 </context-param>

在servlet里面可以通过 getServletContext().getInitParameter(“context/param”)得到 5.过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联

<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
</filter>    

6.监听器配置

<listener>
      <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

7.Servlet配置

<servlet>
    <servlet-name>servlet名称</servlet-name>
    <servlet-class>server类全路径 </servlet-class>
    <init-param>
        <param-name>参数名</param-name>
        <param-value>参数值</param-value>
    </init-param>
    <run-as>
        <description>匿名访问的安全角色</description>
        <role-name>tomcat</role-name>
    </run-as>
    <load-on-startup>指定web应用启动时,装载servlet的次序</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>servlet名称</servlet-name>
    <url-pattern>映射路径</url-pattern>
</servlet-mapping>

8.会话超时配置(单位为分钟)

<session-config>
    <session-timeout>120</session-timeout>
</session-config>

9.MIME类型配置

<mime-mapping>
    <extension>htm</extension>
    <mime-type>text/html</mime-type>
</mime-mapping>

10.指定欢迎文件页配置

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

11.配置错误页面  (1).通过错误码来配置error-page

 <!--配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp-->
<error-page>
    <error-code>404</error-code>
    <location>/NotFound.jsp</location>
</error-page>

(2).通过异常的类型配置error-page

<!--配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp-->
<error-page>
    <exception-type>java.lang.NullException</exception-type>
    <location>/error.jsp</location>
</error-page>

12.TLD配置

<taglib>
    <tablib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
    <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>

</taglib>

如果开发工具一直在报错,应该把<taglib> 放到 <jsp-config>中

<jsp-config>
    <taglib>
        <tablib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
        <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
    </taglib>
</jsp-config>

13.资源管理对象配置

<resource-env-ref>
    <resource-env-ref-name>jms/StockQUeue</resource-env-ref-name>
</resource-env-ref>

14.资源工厂配置

<resource-ref>
    <res-ref-name>mail/Session</res-ref-name>
    <res-type>javax.mail.Session</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

配置数据库连接池可在此配置

<resource-ref>
    <res-ref-name>jdbc/sample_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

15.安全限制配置

<security-constraint>
      <display-name>Example Security Constraint</display-name>
      <web-resource-collection>
          <web-resource-name>Protected Area</web-resource-name>
          <url-pattern>/jsp/security/protected/*</url-pattern>
          <http-method>DELETE</http-method>
          <http-method>GET</http-method>
          <http-method>POST</http-method>
          <http-method>PUT</http-method>
     </web-resource-collection>
     <auth-constraint>
         <role-name>tomcat</role-name>
         <role-name>role1</role-name>
     </auth-constraint>
</security-constraint>

16.登陆验证配置

<login-config>
     <auth-method>FORM</auth-method>
     <realm-name>Example-Based Authentiation Area</realm-name>
     <form-login-config>
         <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
         <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
     </form-login-config>
 </login-config>

17.安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。 分别地声明角色可使高级IDE处理安全信息更为容易。

<security-role>
     <role-name>tomcat</role-name>
 </security-role>

18.Web环境参数:env-entry元素声明Web应用的环境项

<env-entry>
     <env-entry-name>minExemptions</env-entry-name>
     <env-entry-value>1</env-entry-value>
     <env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>

19.EJB 声明

<ejb-ref>
     <description>Example EJB reference</decription>
     <ejb-ref-name>ejb/Account</ejb-ref-name>
     <ejb-ref-type>Entity</ejb-ref-type>
     <home>com.mycompany.mypackage.AccountHome</home>
     <remote>com.mycompany.mypackage.Account</remote>
 </ejb-ref>

20.本地EJB声明


 <ejb-local-ref>
     <description>Example Loacal EJB reference</decription>
     <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
     <ejb-ref-type>Session</ejb-ref-type>
     <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
     <local>com.mycompany.mypackage.ProcessOrder</local>
 </ejb-local-ref>

转载于:https://my.oschina.net/jackyli515/blog/880148

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值