KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > servlet > Forward


1 // ========================================================================
2
// $Id: Forward.java,v 1.9 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.servlet;
17 import java.io.IOException JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.RequestDispatcher JavaDoc;
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServlet JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.mortbay.log.LogFactory;
32
33 /* ------------------------------------------------------------ */
34 /** Forward Servlet Request.
35  * This servlet can be configured with init parameters to use
36  * a RequestDispatcher to forward requests.
37  *
38  * The servlet path of a request is used to look for a initparameter
39  * of that name. If a parameter is found, it's value is used to get a
40  * RequestDispatcher.
41  *
42  * @version $Id: Forward.java,v 1.9 2005/08/13 00:01:28 gregwilkins Exp $
43  * @author Greg Wilkins (gregw)
44  */

45 public class Forward extends HttpServlet JavaDoc
46 {
47     private static Log log = LogFactory.getLog(Forward.class);
48
49     /* ------------------------------------------------------------ */
50     Map JavaDoc _forwardMap= new HashMap JavaDoc();
51
52     /* ------------------------------------------------------------ */
53     public void init(ServletConfig JavaDoc config)
54          throws ServletException JavaDoc
55     {
56         super.init(config);
57
58         Enumeration JavaDoc enm = config.getInitParameterNames();
59         while (enm.hasMoreElements())
60         {
61             String JavaDoc path=(String JavaDoc)enm.nextElement();
62             String JavaDoc forward=config.getInitParameter(path);
63             _forwardMap.put(path,forward);
64         }
65
66     }
67     
68     /* ------------------------------------------------------------ */
69     public void doPost(HttpServletRequest JavaDoc sreq, HttpServletResponse JavaDoc sres)
70         throws ServletException JavaDoc, IOException JavaDoc
71     {
72         doGet(sreq,sres);
73     }
74     
75     /* ------------------------------------------------------------ */
76     public void doGet(HttpServletRequest JavaDoc sreq, HttpServletResponse JavaDoc sres)
77         throws ServletException JavaDoc, IOException JavaDoc
78     {
79         String JavaDoc path = (String JavaDoc)
80             sreq.getAttribute("javax.servlet.include.servlet_path");
81         if (path==null)
82             path=sreq.getServletPath();
83         if (path.length()==0)
84         {
85             path=(String JavaDoc)sreq.getAttribute("javax.servlet.include.path_info");
86             if (path==null)
87                 path=sreq.getPathInfo();
88         }
89
90         String JavaDoc forward=(String JavaDoc)_forwardMap.get(path);
91         if(log.isDebugEnabled())log.debug("Forward "+path+" to "+forward);
92         if (forward!=null)
93         {
94             ServletContext JavaDoc context =
95                 getServletContext().getContext(forward);
96             String JavaDoc contextPath=sreq.getContextPath();
97             if (contextPath.length()>1)
98                 forward=forward.substring(contextPath.length());
99             
100             RequestDispatcher JavaDoc dispatch =
101                 context.getRequestDispatcher(forward);
102             if (dispatch!=null)
103             {
104                 dispatch.forward(sreq,sres);
105                 return;
106             }
107         }
108
109         sres.sendError(404);
110     }
111
112     /* ------------------------------------------------------------ */
113     public String JavaDoc getServletInfo()
114     {
115         return "Forward Servlet";
116     }
117
118     /* ------------------------------------------------------------ */
119     public synchronized void destroy()
120     {
121         log.debug("Destroyed");
122     }
123     
124 }
125
Popular Tags