KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > ContextLoaderServlet


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.web.context;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 /**
27  * Bootstrap servlet to start up Spring's root {@link WebApplicationContext}.
28  * Simply delegates to {@link ContextLoader}.
29  *
30  * <p>This servlet should have a lower <code>load-on-startup</code> value
31  * in <code>web.xml</code> than any servlets that access the root web
32  * application context.
33  *
34  * <p><i>Note that this class has been deprecated for containers implementing
35  * Servlet API 2.4 or higher, in favor of {@link ContextLoaderListener}.</i><br>
36  * According to Servlet 2.4, listeners must be initialized before load-on-startup
37  * servlets. Many Servlet 2.3 containers already enforce this behavior. If you
38  * use such a container, this servlet can be replaced with ContextLoaderListener.
39  * Else or if working with a Servlet 2.2 container, stick with this servlet.
40  *
41  * <p>Servlet 2.3 containers known to work with bootstrap listeners are:
42  * <ul>
43  * <li>Apache Tomcat 4.x+
44  * <li>Jetty 4.x+
45  * <li>Resin 2.1.8+
46  * <li>Orion 2.0.2+
47  * <li>BEA WebLogic 8.1 SP3
48  * </ul>
49  * For working with any of them, ContextLoaderListener is recommended.
50  *
51  * <p>Servlet 2.3 containers known <i>not</i> to work with bootstrap listeners are:
52  * <ul>
53  * <li>BEA WebLogic up to 8.1 SP2
54  * <li>IBM WebSphere 5.x
55  * <li>Oracle OC4J 9.0.3
56  * </ul>
57  * If you happen to work with such a server, this servlet has to be used.
58  *
59  * <p>So unfortunately, the only context initialization option that is compatible
60  * with <i>all</i> Servlet 2.3 containers is this servlet.
61  *
62  * <p>Note that a startup failure of this servlet will not stop the rest of the
63  * web application from starting, in contrast to a listener failure. This can
64  * lead to peculiar side effects if other servlets get started that depend on
65  * initialization of the root web application context.
66  *
67  * @author Juergen Hoeller
68  * @author Darren Davison
69  * @see ContextLoaderListener
70  * @see org.springframework.web.util.Log4jConfigServlet
71  */

72 public class ContextLoaderServlet extends HttpServlet JavaDoc {
73
74     private ContextLoader contextLoader;
75
76
77     /**
78      * Initialize the root web application context.
79      */

80     public void init() throws ServletException JavaDoc {
81         this.contextLoader = createContextLoader();
82         this.contextLoader.initWebApplicationContext(getServletContext());
83     }
84
85     /**
86      * Create the ContextLoader to use. Can be overridden in subclasses.
87      * @return the new ContextLoader
88      */

89     protected ContextLoader createContextLoader() {
90         return new ContextLoader();
91     }
92
93     /**
94      * Return the ContextLoader used by this servlet.
95      * @return the current ContextLoader
96      */

97     public ContextLoader getContextLoader() {
98         return this.contextLoader;
99     }
100
101
102     /**
103      * Close the root web application context.
104      */

105     public void destroy() {
106         if (this.contextLoader != null) {
107             this.contextLoader.closeWebApplicationContext(getServletContext());
108         }
109     }
110
111
112     /**
113      * This should never even be called since no mapping to this servlet should
114      * ever be created in web.xml. That's why a correctly invoked Servlet 2.3
115      * listener is much more appropriate for initialization work ;-)
116      */

117     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
118         getServletContext().log(
119                 "Attempt to call service method on ContextLoaderServlet as [" +
120                 request.getRequestURI() + "] was ignored");
121         response.sendError(HttpServletResponse.SC_BAD_REQUEST);
122     }
123
124
125     public String JavaDoc getServletInfo() {
126         return "ContextLoaderServlet for Servlet API 2.2/2.3 " +
127             "(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
128     }
129
130 }
131
Popular Tags