KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > servlets > ContextForwardServlet


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.servlets;
23
24 import java.io.IOException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpServlet JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.RequestDispatcher JavaDoc;
31 import javax.servlet.ServletConfig JavaDoc;
32
33 import org.jboss.logging.Logger;
34
35 /**
36  * A generic fowarding servlet that obtains the ServletContext for the
37  * forwardContext init-param and then obtain a request dispatcher for the
38  * request.getPathInfo and forward the request. This allows a global servlet
39  * mapping to redirect requests to a common target. An example would be a
40  * global web.xml error-pages with a single shared context for the web
41  * pages. This requires that the web context crossContext attribute is set to
42  * true.
43  *
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 37459 $
46  */

47 public class ContextForwardServlet extends HttpServlet JavaDoc
48 {
49    private static Logger log = Logger.getLogger(ContextForwardServlet.class);
50    /** The name of the context to which requests are forwarded */
51    private String JavaDoc forwardContext = "/error-pages";
52
53    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
54    {
55       super.init(config);
56       String JavaDoc param = config.getInitParameter("forwardContext");
57       if( param != null )
58          forwardContext = param;
59    }
60
61    /**
62     * Lookup the ServletContext associated with the forwardContext init-param
63     * and then obtain a request dispatcher for the request.getPathInfo and
64     * forward the request. This allows a global servlet mapping to redirect
65     * requests to a common target.
66     *
67     * @param request
68     * @param response
69     * @throws ServletException
70     * @throws IOException
71     */

72    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
73       throws ServletException JavaDoc, IOException JavaDoc
74    {
75       boolean trace = log.isTraceEnabled();
76       if( trace )
77       {
78          log.trace("["+forwardContext+"], PathInfo: "+request.getPathInfo()
79             + ", QueryString: "+request.getQueryString()
80             + ", ContextPath: "+request.getContextPath()
81             + ", HeaderNames: "+request.getHeaderNames()
82             + ", isCommitted: "+response.isCommitted()
83          );
84       }
85       String JavaDoc path = request.getPathInfo();
86       ServletContext JavaDoc sc = getServletContext().getContext(forwardContext);
87       if( sc != null )
88       {
89          if( trace )
90             log.trace("Found ServletContext for: "+forwardContext);
91          RequestDispatcher JavaDoc rd = sc.getRequestDispatcher(path);
92          if( rd != null )
93          {
94             if( trace )
95                log.trace("Found RequestDispatcher for: "+path);
96             rd.forward(request, response);
97             return;
98          }
99       }
100       throw new ServletException JavaDoc("No RequestDispatcher for: "+forwardContext+"/"+path);
101    }
102
103 }
104
Popular Tags