MCPcopy Create free account
hub / github.com/apache/tomcat / getRequestDispatcher

Method getRequestDispatcher

java/org/apache/catalina/connector/Request.java:1300–1370  ·  view source on GitHub ↗
(String path)

Source from the content-addressed store, hash-verified

1298 }
1299
1300 @Override
1301 public RequestDispatcher getRequestDispatcher(String path) {
1302
1303 Context context = getContext();
1304 if (context == null) {
1305 return null;
1306 }
1307
1308 if (path == null) {
1309 return null;
1310 }
1311
1312 int fragmentPos = path.indexOf('#');
1313 if (fragmentPos > -1) {
1314 log.warn(sm.getString("request.fragmentInDispatchPath", path));
1315 path = path.substring(0, fragmentPos);
1316 }
1317
1318 // If the path is already context-relative, just pass it through
1319 if (path.startsWith("/")) {
1320 return context.getServletContext().getRequestDispatcher(path);
1321 }
1322
1323 /*
1324 * Relative to what, exactly?
1325 *
1326 * From the Servlet 4.0 Javadoc: - The pathname specified may be relative, although it cannot extend outside the
1327 * current servlet context. - If it is relative, it must be relative against the current servlet
1328 *
1329 * From Section 9.1 of the spec: - The servlet container uses information in the request object to transform the
1330 * given relative path against the current servlet to a complete path.
1331 *
1332 * It is undefined whether the requestURI is used or whether servletPath and pathInfo are used. Given that the
1333 * RequestURI includes the contextPath (and extracting that is messy) , using the servletPath and pathInfo looks
1334 * to be the more reasonable choice.
1335 */
1336
1337 // Convert a request-relative path to a context-relative one
1338 String servletPath = (String) getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
1339 if (servletPath == null) {
1340 servletPath = getServletPath();
1341 }
1342
1343 // Add the path info, if there is any
1344 String pathInfo = getPathInfo();
1345 String requestPath;
1346
1347 if (pathInfo == null) {
1348 requestPath = servletPath;
1349 } else {
1350 requestPath = servletPath + pathInfo;
1351 }
1352
1353 int pos = requestPath.lastIndexOf('/');
1354 String relative;
1355 if (context.getDispatchersUseEncodedPaths()) {
1356 if (pos >= 0) {
1357 relative = URLEncoder.DEFAULT.encode(requestPath.substring(0, pos + 1), StandardCharsets.UTF_8) + path;

Callers

nothing calls this directly

Calls 12

getContextMethod · 0.95
getServletContextMethod · 0.95
getAttributeMethod · 0.95
getServletPathMethod · 0.95
getPathInfoMethod · 0.95
warnMethod · 0.65
getStringMethod · 0.65
getRequestDispatcherMethod · 0.65
encodeMethod · 0.65
indexOfMethod · 0.45
startsWithMethod · 0.45

Tested by

no test coverage detected