Where does getRequestDispatcher (the "path") look like?

Using the built-in tomcat, this code:

System.out.println("getServletPath: " + request.getServletPath()); System.out.println("getServletContext: " + request.getServletContext().getContextPath()); System.out.println("getServerName: " + request.getServerName()); System.out.println("getServerPort: " + request.getServerPort()); 

Prints out:

 getServletPath: /example getServletContext: getServerName: localhost getServerPort: 9090 

Does this mean that:

 request.getRequestDispatcher("/example/read.jsp").forward(request, response); 

Take a look at this URL on forward(request, response) on the JSP:

http://localhost:9090/example/read.jsp ?

Is there any way to print what the absolute URL getRequestDispatcher("relativePath") refers to?

+7
java tomcat servlets
source share
1 answer

The servlet spec explains this

The getRequestDispatcher method takes a String argument that describes the path within the ServletContext. This path must be relative to the root of the ServletContext and begin with the character '/, or be empty . The method uses the path to search for the servlet, using the rules for matching servlet paths in Chapter 12, β€œMapping requests for Servlets,” wraps it with a RequestDispatcher, and returns the resulting object. If the servlet cannot be resolved based on the given path, a RequestDispatcher is provided that returns the contents for this path.

These rules are as follows.

  • The container will try to find the exact match of the request path to the servlet path. A successful match chooses a servlet.
  • The container will recursively try to match the longest path prefix. This is done by moving down the path tree to the directory at that time, using the / character as a path separator. The longest match defines the selected servlet.
  • If the last segment of the URL path contains an extension (for example, .jsp), the servlet container will try to map the servlet that processes the extension requests. An extension is defined as part of the last segment after the last. character.
  • If none of the previous three rules leads to a servlet match, the container will try to serve the content corresponding to the requested resource. If for the application, it will be used. Many containers provide an implicit servlet by default for serving content.

You are asking

Does this mean that:

request.getRequestDispatcher ("/ example/display.jsp"). Forward (request, response); Look at this URL to redirect (request, response) to the JSP:

http://localhost:9090/example/display.jsp ?

No, it does not send an HTTP request, so the path has nothing to do with the URI. This is more of an internal path that the Servlet container will try to map to its various url mappings for Servlets.

You also ask

Is there a way to print which absolute getRequestDispatcher ("relativePath") URL addresses?

Not. And this is not a completely absolute URL. This is a path that can be processed by some resource in the context of a web application.


After your editing, you addWebapp to your Tomcat instance.

 tomcat.addWebapp(null, "/view2/example2", new File("src/com/example/view/example").getAbsolutePath()); 

Then you send a request to

  /view2/example2/read.jsp 

I assume read.jsp is in

 src/com/example/view/example/ 

I believe in the publicly accessible part of the web application, and therefore the Servlet container can display it and respond to it.

You also added webapp with addContext , which seems to be similar to addWebapp

 context = tomcat.addContext("", base.getAbsolutePath()); 

and servlet mappings are added to this context.

 Tomcat.addServlet(context, "example", new ExampleController()); context.addServletMapping("/example/*", "example"); 

I was mistaken that /example/* not able to handle /example .

When you submit a request for

 /example 

since the context path is "", Context will be used above, and the match will match the registered ExampleController . Your Servlet code will execute and reach

 request.getRequestDispatcher("/view2/example2/read.jsp").forward(request, response); 

Pay attention to javadoc ServletRequest#getRequestDispatcher(String)

The specified path name may be relative, although it cannot be extended outside the current servlet context.

In other words, this Servlet , ExampleController was registered in the ServletContext mapped to the context path "" , i.e. root. The path /view2/example2/read.jsp is in a different context. Since this context has no mapping for it, it responds with 404.

You can get a link to other web applications in a different context. You should use ServletContext#getContext(String) . for example

  ServletContext otherContext = request.getServletContext().getContext("/view2/example2"); 

Now that you have the ServletContext , you can get the RequestDispatcher for the resource in this context.

 otherContext.getRequestDispatcher("/read.jsp").forward(request, response); 

as ServletContext#getRequestDispatcher(String) claims

The path name must begin with the / character and is interpreted as relative to the current context root.


The final answer:

getRequestDispatcher("path") will look at the directory installed in the addWebapp method when referencing a JSP file. If a blank page or NullPointerException displayed, make sure you do the following:

  • Remove all addWebapp definitions.
  • Run addContext , then addWebapp , so they both point to ROOT :

File base = new File("src/com/example/view"); context = tomcat.addContext("", base.getAbsolutePath()); tomcat.addWebapp(null, "/", base.getAbsolutePath());

  • At the servlet point to jsp using request.getRequestDispatcher("/example/read.jsp").forward(request, response); provided that the directory / example exists in "src/com/example/view" .
+5
source share

All Articles