01: package org.apache.velocity.tools.view.servlet;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import javax.servlet.http.HttpServletRequest;
23:
24: /**
25: * <p>A set of utility methods for the servlet environment.</p>
26: *
27: * @version $Id: ServletUtils.java 471244 2006-11-04 18:34:38Z henning $
28: */
29: public class ServletUtils {
30:
31: /**
32: * Retrieves the path for the specified request regardless of
33: * whether this is a direct request or an include by the
34: * RequestDispatcher.
35: */
36: public static String getPath(HttpServletRequest request) {
37: // If we get here from RequestDispatcher.include(), getServletPath()
38: // will return the original (wrong) URI requested. The following special
39: // attribute holds the correct path. See section 8.3 of the Servlet
40: // 2.3 specification.
41: String path = (String) request
42: .getAttribute("javax.servlet.include.servlet_path");
43: // also take into account the PathInfo stated on SRV.4.4 Request Path Elements
44: String info = (String) request
45: .getAttribute("javax.servlet.include.path_info");
46: if (path == null) {
47: path = request.getServletPath();
48: info = request.getPathInfo();
49: }
50: if (info != null) {
51: path += info;
52: }
53: return path;
54: }
55:
56: }
|