1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.context;
  17. import java.io.IOException;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServlet;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. /**
  23. * Bootstrap servlet to start up Spring's root WebApplicationContext.
  24. * Simply delegates to ContextLoader.
  25. *
  26. * <p>This servlet should have a lower load-on-startup value in web.xml
  27. * than any servlets that access the root web application context.
  28. *
  29. * <p><i>Note that this class has been deprecated for containers implementing
  30. * Servlet API 2.4 or higher in favour of ContextLoaderListener.</i><br>
  31. * According to Servlet 2.4, listeners must be initialized before load-on-startup
  32. * servlets. Many Servlet 2.3 containers already enforce this behaviour. If you
  33. * use such a container, this servlet can be replaced with ContextLoaderListener.
  34. * Else or if working with a Servlet 2.2 container, stick with this servlet.
  35. *
  36. * <p>Servlet 2.3 containers known to work with listeners are:
  37. * <ul>
  38. * <li>Apache Tomcat 4.x
  39. * <li>Jetty 4.x
  40. * <li>Resin 2.1.8+
  41. * <li>Orion 2.0.2+
  42. * </ul>
  43. * For working with any of them, ContextLoaderListener is recommended.
  44. *
  45. * <p>Servlet 2.3 containers known <i>not</i> to work with listeners are:
  46. * <ul>
  47. * <li>BEA WebLogic up to 8.1
  48. * <li>IBM WebSphere 5.x
  49. * <li>Oracle OC4J 9.0.3
  50. * </ul>
  51. * If you happen to work with such a server, this servlet has to be used.
  52. *
  53. * <p>So unfortunately, the only context initialization option that is compatible
  54. * with <i>all</i> Servlet 2.3 containers is this servlet.
  55. *
  56. * <p>Note that a startup failure of this servlet will not stop the rest of the
  57. * web application from starting, in contrast to a listener failure. This can
  58. * lead to peculiar side effects if other servlets get started that depend on
  59. * initialization of the root web application context.
  60. *
  61. * @author Juergen Hoeller
  62. * @author Darren Davison
  63. * @see ContextLoader
  64. * @see ContextLoaderListener
  65. * @see org.springframework.web.util.Log4jConfigServlet
  66. */
  67. public class ContextLoaderServlet extends HttpServlet {
  68. private ContextLoader contextLoader;
  69. /**
  70. * Initialize the root web application context.
  71. */
  72. public void init() throws ServletException {
  73. this.contextLoader = createContextLoader();
  74. this.contextLoader.initWebApplicationContext(getServletContext());
  75. }
  76. /**
  77. * Create the ContextLoader to use. Can be overridden in subclasses.
  78. * @return the new ContextLoader
  79. */
  80. protected ContextLoader createContextLoader() {
  81. return new ContextLoader();
  82. }
  83. /**
  84. * Close the root web application context.
  85. */
  86. public void destroy() {
  87. this.contextLoader.closeWebApplicationContext(getServletContext());
  88. }
  89. /**
  90. * This should never even be called since no mapping to this servlet should
  91. * ever be created in web.xml. That's why a correctly invoked Servlet 2.3
  92. * listener is much more appropriate for initialization work ;-)
  93. */
  94. public void doService(HttpServletRequest request, HttpServletResponse response) throws IOException {
  95. getServletContext().log("Attempt to call service method on ContextLoaderServlet as " +
  96. request.getRequestURI() + " was ignored");
  97. response.sendError(HttpServletResponse.SC_BAD_REQUEST);
  98. }
  99. public String getServletInfo() {
  100. return "ContextLoaderServlet for Servlet API 2.2/2.3 " +
  101. "(deprecated in favour of ContextLoaderListener for Servlet API 2.4)";
  102. }
  103. }