示例

下面的例子说明了部署描述文件模式中列出的定义的用法。

一个简单的例子

CODE EXAMPLE 14-1 Basic Deployment Descriptor Example

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd”
  6. version=”2.5”>
  7. <display-name>A Simple Application</display-name>
  8. <context-param>
  9. <param-name>Webmaster</param-name>
  10. <param-value>webmaster@mycorp.com</param-value>
  11. </context-param>
  12. <servlet>
  13. <servlet-name>catalog</servlet-name>
  14. <servlet-class>com.mycorp.CatalogServlet
  15. </servlet-class>
  16. <init-param>
  17. <param-name>catalog</param-name>
  18. <param-value>Spring</param-value>
  19. </init-param>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>catalog</servlet-name>
  23. <url-pattern>/catalog/*</url-pattern>
  24. </servlet-mapping>
  25. <session-config>
  26. <session-timeout>30</session-timeout>
  27. </session-config>
  28. <mime-mapping>
  29. <extension>pdf</extension>
  30. <mime-type>application/pdf</mime-type>
  31. </mime-mapping>
  32. <welcome-file-list>
  33. <welcome-file>index.jsp</welcome-file>
  34. <welcome-file>index.html</welcome-file>
  35. <welcome-file>index.htm</welcome-file>
  36. </welcome-file-list>
  37. <error-page>
  38. <error-code>404</error-code>
  39. <location>/404.html</location>
  40. </error-page>
  41. </web-app>

安全的例子

CODE EXAMPLE 14-2 Deployment Descriptor Example Using Security

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
  6. version=”2.5”>
  7. <display-name>A Secure Application</display-name>
  8. <servlet>
  9. <servlet-name>catalog</servlet-name>
  10. <servlet-class>com.mycorp.CatalogServlet
  11. </servlet-class>
  12. <init-param>
  13. <param-name>catalog</param-name>
  14. <param-value>Spring</param-value>
  15. </init-param>
  16. <security-role-ref>
  17. <role-name>MGR</role-name>
  18. <!-- role name used in code -->
  19. <role-link>manager</role-link>
  20. </security-role-ref>
  21. </servlet>
  22. <security-role>
  23. <role-name>manager</role-name>
  24. </security-role>
  25. <servlet-mapping>
  26. <servlet-name>catalog</servlet-name>
  27. <url-pattern>/catalog/*</url-pattern>
  28. </servlet-mapping>
  29. <security-constraint>
  30. <web-resource-collection>
  31. <web-resource-name>SalesInfo
  32. </web-resource-name>
  33. <url-pattern>/salesinfo/*</url-pattern>
  34. <http-method>GET</http-method>
  35. <http-method>POST</http-method>
  36. </web-resource-collection>
  37. <auth-constraint>
  38. <role-name>manager</role-name>
  39. </auth-constraint>
  40. <user-data-constraint>
  41. <transport-guarantee>CONFIDENTIAL
  42. </transport-guarantee>
  43. </user-data-constraint>
  44. </security-constraint>
  45. </web-app>