getRequestDispatcher(" path")在哪里看? [英] Where does getRequestDispatcher("path") look?

查看:262
本文介绍了getRequestDispatcher(" path")在哪里看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用嵌入式tomcat,此代码:

Using embedded 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());

打印出来:

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

这是否意味着:

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

将此网址查看为转发(请求,响应)到JSP:

Will look at this URL to forward(request, response) to the JSP:

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

有没有办法打印出绝对 URL getRequestDispatcher(relativePath)正在寻址?

Is there a way to print out what absolute URL getRequestDispatcher("relativePath") is addressing?

推荐答案

Servlet规范解释了这个


getRequestDispatcher 方法接受一个 String 参数,该参数描述ServletContext范围内的
路径。 此路径必须是相对于ServletContext根目录的
,并以'/'开头,或
为空
。该方法使用路径查找servlet,使用第12章将请求映射到
Servlets中的
servlet路径匹配规则,
用RequestDispatcher对象包装它,并返回
结果对象。如果不能根据给定的
路径解析servlet,则提供RequestDispatcher,返回
该路径的内容。

The getRequestDispatcher method takes a String argument describing a path within the scope of the ServletContext. This path must be relative to the root of the ServletContext and begin with a ‘/’, or be empty. The method uses the path to look up a servlet, using the servlet path matching rules in Chapter 12, "Mapping Requests to Servlets", wraps it with a RequestDispatcher object, and returns the resulting object. If no servlet can be resolved based on the given path, a RequestDispatcher is provided that returns the content for that path.

这些规则如下



  1. 容器将尝试找到完全匹配的请求的路径到servlet的路径。成功匹配选择
    servlet。

  2. 容器将递归尝试匹配最长的路径前缀。这是通过使用'/'字符作为路径分隔符逐步降低路径树目录
    来完成的。最长的
    匹配确定所选的servlet。

  3. 如果URL路径中的最后一个段包含扩展名(例如.jsp),则servlet容器将尝试匹配一个servlet,处理
    扩展请求。扩展名被定义为
    的一部分,是最后一个'。'字符后的最后一个段。

  4. 如果前三个规则都没有导致servlet匹配,则容器将尝试提供适合所请求的
    资源的内容。如果为
    应用程序定义了默认servlet,则将使用它。许多容器提供了一个隐含的
    默认servlet来提供内容。

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
  4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.


你问


这是否意味着:

Does that mean that:

request.getRequestDispatcher(/ example / display。 jsp)。forward(请求,
响应);将查看此URL以转发(请求,响应)到
JSP:

request.getRequestDispatcher("/example/display.jsp").forward(request, response); Will look at this URL to forward(request, response) to the JSP:

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

不,它不发送HTTP请求,所以路径有与URI无关。它更像是一个内部路径,Servlet容器将尝试与其各种Servlet的url-mappings匹配。

No, it doesn't send an HTTP request, so the path has nothing to do with a URI. It's more of an internal path that the Servlet container will try to match with its various url-mappings for Servlets.

你也可以询问


有没有办法打印出getRequestDispatcher(relativePath)正在寻址的绝对URL?

Is there a way to print out what absolute URL getRequestDispatcher("relativePath") is addressing?

没有。它并不是一个绝对的URL。这是一个可以由Web应用程序上下文中的某些资源处理的路径。

No. And it isn't exactly an absolute URL. It's a path that can be handled by some resource in the web application context.

编辑后,您 addWebapp 到您的 Tomcat 实例。

After your edit, you addWebapp to your Tomcat instance.

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

然后您发送请求

 /view2/example2/read.jsp

我要去了假设 read.jsp

src/com/example/view/example/

我相信它位于Web应用程序的可公开访问的部分,因此是Servlet容器可以渲染它并用它做出响应。

I believe it's in the publicly accessible part of the web application and therefore the Servlet container can render it and respond with it.

你还添加了一个带有 addContext 的webapp,它似乎是类似于 addWebapp

You've also added a webapp with addContext which seems to be similar to addWebapp

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

并添加了对上下文的servlet映射。

and added servlet mappings to this context.

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

我错了 / example / * 无法处理 / example

I was wrong about the /example/* not being able to handle /example.

当您发送请求时

/example

因为上下文路径是,将使用上面的 Context ,映射将匹配 ExampleController 在上面注册。您的 Servlet 代码将执行并到达

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

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

注意 ServletRequest#getRequestDispatcher(String)


指定的路径名​​可能是相对的,但它不能在当前servlet上下文之外扩展

The pathname specified may be relative, although it cannot extend outside the current servlet context.

换句话说,这个 Servlet ExampleController 已在 ServletContext 中注册,映射到上下文路径,即。根。路径 /view2/example2/read.jsp 指的是另一个上下文。由于此上下文没有映射,因此它以404响应。

In other words, this Servlet, ExampleController was registered in the ServletContext mapped to the context path "", ie. root. The path /view2/example2/read.jsp is referring to another context. Since this context doesn't have a mapping for it, it responds with 404.

您可以在不同的上下文中获取对其他Web应用程序的引用。您必须使用 的ServletContext#的getContext(字符串) 。例如

You can get a reference to another web applications in a different context. You have to use ServletContext#getContext(String). For example

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

现在你有了 ServletContext ,你可以为 上下文中的资源获取 RequestDispatcher

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

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

ServletContext#getRequestDispatcher(String) states

since ServletContext#getRequestDispatcher(String) states


路径名必须以/开头,并且被解释为相对于当前上下文根。

The pathname must begin with a / and is interpreted as relative to the current context root.

最终答案:

getRequestDispatcher(path)将在引用JSP文件时查看 addWebapp 方法中设置的目录。如果显示空白页面或 NullPointerException ,请确保您已完成以下操作:

getRequestDispatcher("path") will look at the directory set in the addWebapp method when referencing a JSP file. If a blank page or NullPointerException is displayed, ensure that you have done the following:


  1. 删除所有 addWebapp 定义。

  2. 运行 addContext 然后 addWebApp 喜欢这样,所以他们都指向 ROOT

  1. Remove all the addWebapp definitions.
  2. Run addContext then addWebApp like this so they both point to ROOT:

文件库=新文件(src / com / example / view);
context = tomcat.addContext(,base.getAbsolutePath());
tomcat.addWebapp(null,/,base.getAbsolutePath());


  1. 在servlet中指向jsp,使用 request.getRequestDispatcher(/ example / read.jsp)。forward(request,response); ,前提是目录/示例存在于src / com / example / view

  1. In the servlet point to the jsp using request.getRequestDispatcher("/example/read.jsp").forward(request, response); provided that the directory /example exists in "src/com/example/view".

这篇关于getRequestDispatcher(" path")在哪里看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆