KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > servlet > ContextForwardServlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.servlet;
19
20 import java.io.IOException 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.UnavailableException JavaDoc;
27 import javax.servlet.http.HttpServlet JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 /**
32  * Servlet that forwards GET and POST requests to a servlet
33  * in an alternate context. The servlet path and alternate
34  * context are passed in as configuration parameters (e.g.
35  * via <config-param> elements in the web.xml).
36  */

37 public class ContextForwardServlet extends HttpServlet JavaDoc {
38
39     // name of the configuration parameter containing the context path
40
public static final String JavaDoc CONTEXT_PATH = "context-path";
41     // name of the configuration parameter containing the servlet path
42
public static final String JavaDoc SERVLET_PATH = "servlet-path";
43
44     private ServletContext JavaDoc forwardContext;
45     private String JavaDoc servletPath;
46
47     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
48         super.init(config);
49         String JavaDoc contextPath = config.getInitParameter(CONTEXT_PATH);
50         servletPath = config.getInitParameter(SERVLET_PATH);
51         if (contextPath == null || servletPath == null) {
52             throw new UnavailableException JavaDoc("context-path and servlet-path " +
53                     "must be provided as configuration parameters");
54         }
55         forwardContext = getServletContext().getContext(contextPath);
56     }
57
58     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
59             throws ServletException JavaDoc, IOException JavaDoc {
60         doPost(req, resp);
61     }
62
63     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
64             throws ServletException JavaDoc, IOException JavaDoc {
65         String JavaDoc dispatchURI = servletPath + (req.getPathInfo() == null ? "" : req.getPathInfo());
66         String JavaDoc queryString = req.getQueryString();
67         if (queryString != null) {
68             dispatchURI += "?" + queryString;
69         }
70         RequestDispatcher JavaDoc dispatcher = forwardContext.getRequestDispatcher(dispatchURI);
71         dispatcher.forward(req, resp);
72     }
73 }
74
Popular Tags