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.util;
  17. import java.io.IOException;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. /**
  22. * Bootstrap servlet for custom Log4J initialization in a web environment.
  23. * Simply delegates to Log4jWebConfigurer.
  24. *
  25. * <p>Note: This servlet should have a lower load-on-startup value in web.xml
  26. * than ContextLoaderServlet, when using custom Log4J initialization.
  27. *
  28. * <p><i>Note that this class has been deprecated for containers implementing
  29. * Servlet API 2.4 or higher in favour of Log4jConfigListener.</i><br>
  30. * According to Servlet 2.4, listeners must be initialized before load-on-startup
  31. * servlets. Many Servlet 2.3 containers already enforce this behaviour
  32. * (see ContextLoaderServlet javadoc for details). If you use such a container,
  33. * this servlet can be replaced with Log4jConfigListener. Else or if working
  34. * with a Servlet 2.2 container, stick with this servlet.
  35. *
  36. * @author Juergen Hoeller
  37. * @author Darren Davison
  38. * @since 12.08.2003
  39. * @see Log4jWebConfigurer
  40. * @see Log4jConfigListener
  41. * @see org.springframework.web.context.ContextLoaderServlet
  42. */
  43. public class Log4jConfigServlet extends HttpServlet {
  44. public void init() {
  45. Log4jWebConfigurer.initLogging(getServletContext());
  46. }
  47. public void destroy() {
  48. Log4jWebConfigurer.shutdownLogging(getServletContext());
  49. }
  50. /**
  51. * This should never even be called since no mapping to this servlet should
  52. * ever be created in web.xml. That's why a correctly invoked Servlet 2.3
  53. * listener is much more appropriate for initialization work ;-)
  54. */
  55. public void doService(HttpServletRequest request, HttpServletResponse response) throws IOException {
  56. getServletContext().log("Attempt to call service method on Log4jConfigServlet as " + request.getRequestURI() + " was ignored");
  57. response.sendError(HttpServletResponse.SC_BAD_REQUEST);
  58. }
  59. public String getServletInfo() {
  60. return "Log4jConfigServlet for Servlet API 2.2/2.3 (deprecated in favour of Log4jConfigListener for Servlet API 2.4)";
  61. }
  62. }