星空网 > 软件开发 > Java

web.xml文件配置

在项目中,通常会遇到加载的优先级,过滤器配置,监听的配置等一些情况,经过这几天的工作经验,网上搜集的资料,总结一下,以便今后的学习。

web.

以下是在网上看到的一篇不错的文章拷贝过来。。。。。

web.

  1. <web-app>   
  2. <display-name></display-name>定义了WEB应用的名字   
  3. <description></description> 声明WEB应用的描述信息   
  4.   
  5. <context-param></context-param> context-param元素声明应用范围内的初始化参数。   
  6. <filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。   
  7. <filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。   
  8. <listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。   
  9.                      Listener元素指出事件监听程序类。   
  10. <servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。   
  11. <servlet-mapping></servlet-mapping> 服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。   
  12.               但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。   
  13.   
  14. <session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。   
  15.           可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。   
  16.   
  17. <mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。   
  18. <welcome-file-list></welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。   
  19. <error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。   
  20. <taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,   
  21.                   而不用编辑使用这些文件的JSP页面。   
  22. <resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。   
  23. <resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。   
  24. <security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用   
  25. <login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。   
  26. <security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素   
  27.                    的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。   
  28. <env-entry></env-entry>声明Web应用的环境项。   
  29. <ejb-ref></ejb-ref>声明一个EJB的主目录的引用。   
  30. < ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。   
  31. </web-app>   

相应元素配置 :

 

  1. 1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标   
  2. <icon>   
  3. <small-icon>/images/app_small.gif</small-icon>   
  4. <large-icon>/images/app_large.gif</large-icon>   
  5. </icon>   
  6. 2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称   
  7. <display-name>Tomcat Example</display-name>   
  8. 3、Web 应用描述: 给出于此相关的说明性文本   
  9. <disciption>Tomcat Example servlets and JSP pages.</disciption>   
  10. 4、上下文参数:声明应用范围内的初始化参数。   
  11.   <context-param>   
  12.     <param-name>ContextParameter</para-name>   
  13.     <param-value>test</param-value>   
  14.     <description>It is a test parameter.</description>   
  15.   </context-param>   
  16.   在servlet里面可以通过getServletContext().getInitParameter("context/param")得到   
  17.   
  18. 5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。   
  19.   <filter>   
  20.         <filter-name>setCharacterEncoding</filter-name>   
  21.         <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>   
  22.         <init-param>   
  23.             <param-name>encoding</param-name>   
  24.             <param-value>GB2312</param-value>   
  25.         </init-param>   
  26.   </filter>   
  27.   <filter-mapping>   
  28.         <filter-name>setCharacterEncoding</filter-name>   
  29.         <url-pattern>/*</url-pattern>   
  30.   </filter-mapping>   
  31. 6、**配置   
  32.   <listener>   
  33.       <listerner-class>listener.SessionListener</listener-class>   
  34.   </listener>   
  35. 7、Servlet配置   
  36.    基本配置   
  37.    <servlet>   
  38.       <servlet-name>snoop</servlet-name>   
  39.       <servlet-class>SnoopServlet</servlet-class>   
  40.    </servlet>   
  41.    <servlet-mapping>   
  42.       <servlet-name>snoop</servlet-name>   
  43.       <url-pattern>/snoop</url-pattern>   
  44.    </servlet-mapping>   
  45.    高级配置   
  46.    <servlet>   
  47.       <servlet-name>snoop</servlet-name>   
  48.       <servlet-class>SnoopServlet</servlet-class>   
  49.       <init-param>   
  50.          <param-name>foo</param-name>   
  51.          <param-value>bar</param-value>   
  52.       </init-param>   
  53.       <run-as>   
  54.          <description>Security role for anonymous access</description>   
  55.          <role-name>tomcat</role-name>   
  56.       </run-as>   
  57.    </servlet>   
  58.    <servlet-mapping>   
  59.       <servlet-name>snoop</servlet-name>   
  60.       <url-pattern>/snoop</url-pattern>   
  61.    </servlet-mapping>   
  62.    元素说明   
  63.      <servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:   
  64.      <servlet-name></servlet-name> 指定servlet的名称   
  65.      <servlet-class></servlet-class> 指定servlet的类名称   
  66.      <jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径   
  67.      <init-param></init-param> 用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数   
  68.      <load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。   
  69.                                  当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.   
  70.                                  当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它   
  71.      <servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素   
  72.        <servlet-name></servlet-name> 指定servlet的名称   
  73.        <url-pattern></url-pattern> 指定servlet所对应的URL   
  74. 8、会话超时配置(单位为分钟)   
  75.    <session-config>   
  76.       <session-timeout>120</session-timeout>   
  77.    </session-config>   
  78. 9、MIME类型配置   
  79.    <mime-mapping>   
  80.       <extension>htm</extension>   
  81.       <mime-type>text/html</mime-type>   
  82.    </mime-mapping>   
  83. 10、指定欢迎文件页配置   
  84.    <welcome-file-list>   
  85.       <welcome-file>index.jsp</welcome-file>   
  86.       <welcome-file>index.html</welcome-file>   
  87.       <welcome-file>index.htm</welcome-file>   
  88.    </welcome-file-list>   
  89. 11、配置错误页面   
  90.   一、 通过错误码来配置error-page   
  91.    <error-page>   
  92.       <error-code>404</error-code>   
  93.       <location>/NotFound.jsp</location>   
  94.    </error-page>   
  95.   上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。   
  96. 二、通过异常的类型配置error-page   
  97.    <error-page>   
  98.        <exception-type>java.lang.NullException</exception-type>   
  99.        <location>/error.jsp</location>   
  100.    </error-page>   
  101.   上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp   
  102. 12、TLD配置   
  103.    <taglib>   
  104.        <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   
  105.        <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>   
  106.    </taglib>   
  107.    如果MyEclipse一直在报错,应该把<taglib> 放到 <jsp-config>中   
  108.    <jsp-config>   
  109.       <taglib>   
  110.           <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   
  111.           <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>   
  112.       </taglib>   
  113.    </jsp-config>   
  114. 13、资源管理对象配置   
  115.    <resource-env-ref>   
  116.        <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>   
  117.    </resource-env-ref>   
  118. 14、资源工厂配置   
  119.    <resource-ref>   
  120.        <res-ref-name>mail/Session</res-ref-name>   
  121.        <res-type>javax.mail.Session</res-type>   
  122.        <res-auth>Container</res-auth>   
  123.    </resource-ref>   
  124.    配置数据库连接池就可在此配置:   
  125.    <resource-ref>   
  126.        <description>JNDI JDBC DataSource of shop</description>   
  127.        <res-ref-name>jdbc/sample_db</res-ref-name>   
  128.        <res-type>javax.sql.DataSource</res-type>   
  129.        <res-auth>Container</res-auth>   
  130.    </resource-ref>   
  131. 15、安全限制配置   
  132.    <security-constraint>   
  133.       <display-name>Example Security Constraint</display-name>   
  134.       <web-resource-collection>   
  135.          <web-resource-name>Protected Area</web-resource-name>   
  136.          <url-pattern>/jsp/security/protected/*</url-pattern>   
  137.          <http-method>DELETE</http-method>   
  138.          <http-method>GET</http-method>   
  139.          <http-method>POST</http-method>   
  140.          <http-method>PUT</http-method>   
  141.       </web-resource-collection>   
  142.       <auth-constraint>   
  143.         <role-name>tomcat</role-name>   
  144.         <role-name>role1</role-name>   
  145.       </auth-constraint>   
  146.    </security-constraint>   
  147. 16、登陆验证配置   
  148.    <login-config>   
  149.      <auth-method>FORM</auth-method>   
  150.      <realm-name>Example-Based Authentiation Area</realm-name>   
  151.      <form-login-config>   
  152.         <form-login-page>/jsp/security/protected/login.jsp</form-login-page>   
  153.         <form-error-page>/jsp/security/protected/error.jsp</form-error-page>   
  154.      </form-login-config>   
  155.    </login-config>   
  156. 17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。   
  157.     分别地声明角色可使高级IDE处理安全信息更为容易。   
  158.   <security-role>   
  159.      <role-name>tomcat</role-name>   
  160.   </security-role>   
  161. 18、Web环境参数:env-entry元素声明Web应用的环境项   
  162.   <env-entry>   
  163.      <env-entry-name>minExemptions</env-entry-name>   
  164.      <env-entry-value>1</env-entry-value>   
  165.      <env-entry-type>java.lang.Integer</env-entry-type>   
  166.   </env-entry>   
  167. 19、EJB 声明   
  168.   <ejb-ref>   
  169.      <description>Example EJB reference</decription>   
  170.      <ejb-ref-name>ejb/Account</ejb-ref-name>   
  171.      <ejb-ref-type>Entity</ejb-ref-type>   
  172.      <home>com.mycompany.mypackage.AccountHome</home>   
  173.      <remote>com.mycompany.mypackage.Account</remote>   
  174.   </ejb-ref>   
  175. 20、本地EJB声明   
  176.   <ejb-local-ref>   
  177.      <description>Example Loacal EJB reference</decription>   
  178.      <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>   
  179.      <ejb-ref-type>Session</ejb-ref-type>   
  180.      <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>   
  181.      <local>com.mycompany.mypackage.ProcessOrder</local>   
  182.   </ejb-local-ref>   
  183. 21、配置DWR   
  184.   <servlet>   
  185.       <servlet-name>dwr-invoker</servlet-name>   
  186.       <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>   
  187.   </servlet>   
  188.   <servlet-mapping>   
  189.       <servlet-name>dwr-invoker</servlet-name>   
  190.       <url-pattern>/dwr/*</url-pattern>   
  191.   </servlet-mapping>   
  192. 22、配置Struts   
  193.     <display-name>Struts Blank Application</display-name>   
  194.     <servlet>   
  195.         <servlet-name>action</servlet-name>   
  196.         <servlet-class>   
  197.             org.apache.struts.action.ActionServlet   
  198.         </servlet-class>   
  199.         <init-param>   
  200.             <param-name>detail</param-name>   
  201.             <param-value>2</param-value>   
  202.         </init-param>   
  203.         <init-param>   
  204.             <param-name>debug</param-name>   
  205.             <param-value>2</param-value>   
  206.         </init-param>   
  207.         <init-param>   
  208.             <param-name>config</param-name>   
  209.             <param-value>/WEB-INF/struts-config.</param-value>   
  210.         </init-param>   
  211.         <init-param>   
  212.             <param-name>application</param-name>   
  213.             <param-value>ApplicationResources</param-value>   
  214.         </init-param>   
  215.         <load-on-startup>2</load-on-startup>   
  216.     </servlet>   
  217.     <servlet-mapping>   
  218.         <servlet-name>action</servlet-name>   
  219.         <url-pattern>*.do</url-pattern>   
  220.     </servlet-mapping>   
  221.     <welcome-file-list>   
  222.         <welcome-file>index.jsp</welcome-file>   
  223.     </welcome-file-list>   
  224.   
  225.     <!-- Struts Tag Library Descriptors -->   
  226.     <taglib>   
  227.         <taglib-uri>struts-bean</taglib-uri>   
  228.         <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>   
  229.     </taglib>   
  230.     <taglib>   
  231.         <taglib-uri>struts-html</taglib-uri>   
  232.         <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>   
  233.     </taglib>   
  234.     <taglib>   
  235.     <taglib-uri>struts-nested</taglib-uri>   
  236.     <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>   
  237.     </taglib>   
  238.     <taglib>   
  239.         <taglib-uri>struts-logic</taglib-uri>   
  240.         <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>   
  241.     </taglib>   
  242.     <taglib>   
  243.         <taglib-uri>struts-tiles</taglib-uri>   
  244.         <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>   
  245.     </taglib>   
  246. 23、配置Spring(基本上都是在Struts中配置的)   
  247.   
  248.    <!-- 指定spring配置文件位置 -->   
  249.    <context-param>   
  250.       <param-name>contextConfigLocation</param-name>   
  251.       <param-value>   
  252.        <!--加载多个spring配置文件 -->   
  253.         /WEB-INF/applicationContext.
  254.       </param-value>   
  255.    </context-param>   
  256.   
  257.    <!-- 定义SPRING**,加载spring -->   
  258.   
  259.   <listener>   
  260.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  261.   </listener>   
  262.   
  263.   <listener>   
  264.      <listener-class>   
  265.        org.springframework.web.context.request.RequestContextListener   
  266.      </listener-class>   
  267.   </listener>   

 




原标题:web.xml文件配置

关键词:xml

xml
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流