【问题标题】:Why is the forward slash mandatory when forwarding from servlet to JSP?为什么从 servlet 转发到 JSP 时必须使用正斜杠?
【发布时间】:2015-07-12 19:47:13
【问题描述】:

当在 servlet 中使用请求调度程序转发到 JSP 时,为什么必须用正斜杠表示 JSP,如下所示:

    getServletContext().getRequestDispatcher("/foo.jsp").forward(request, response);

如果我在没有正斜杠的情况下使用它,我会在 Tomcat 中遇到异常。

但是当我使用请求调度程序重定向到 servlet 时,我可以省略正斜杠。如果有一个映射到 url 模式的 servlet,则下面的代码片段可以正常工作-:

getServletContext().getRequestDispatcher("bar").forward(request, response);

我知道/ 表示网络应用程序的根,但为什么 servlet 不需要它,而仅 JSP 需要它? Servlet 也属于特定的网络应用程序。

【问题讨论】:

  • 斜杠表示根目录,jsp不需要
  • 你能分享你不需要 / 的 servlet 映射吗?
  • 这可能是因为,您的 JSP 在根文件夹中,而您的 servlet url 与当前 servlet url 相关,例如 path/currentUrl->path/bar
  • @JohnAment @WebServlet("/bar") 这是我用的。当我不在 JSP 中使用斜线时,我得到了一个 Tomcat 异常。

标签: java jsp


【解决方案1】:

所有的servlet对象都在容器中的同一个地方运行,但是jsp去不同的地方。

部署后的 SERVLET 位置 你会在这里找到所有的 servlet apache-tomcat-7.0.55\webapps\Test\WEB-INF\classes\com\it\servlet

MainController.class

ABC.class

部署后的 JSP 位置

apache-tomcat-7.0.55\work\Catalina\localhost\Test\org\apache\jsp

apiTester_jsp.class

【讨论】:

    【解决方案2】:

    您错误地认为正斜杠表示网络应用程序的根。有时可能意味着,但并非总是如此。

    正斜杠“/”字符用于链接中的 JSP,例如:

    <a href="/foo/stuff.htm">Stuff page</a>
    

    在 servlet 中通过 RequestDispatcher.forward()HTTPResponse.sendRedirect() 方法进行 URL 重定向。

    只有在重定向 URL 的开头应用时才有效。

    以下是应用程序服务器如何在后台解释它的规则:

    1. 首先:请注意,重定向地址始终区分大小写 - 即使在您的重定向 URL 的域段中也是如此。请参阅下面的示例代码中的我的 cmets,以通过示例说明什么会起作用,什么会失败。

    2. 如果重定向以“http://”开头,则将在重定向中使用指定的绝对路径。

      否则您的重定向网址将被应用为相对网址。

    3. 如果重定向 URL 以正斜杠字符“/”开头,则指示您的应用程序服务器构造一个与 Web 容器相关的 URL!

      例如:相对于localhost:8080

      所以命令...

      response.sendRedirect("/foo/stuff.htm")

      从 servlet 内部,或

      &lt;a href="/foo/stuff.htm"&gt;Stuff page&lt;/a&gt;

      从 JSP 内部,将带您到

      localhost:8080/foo/stuff.htm.

    4. 在您的重定向 URL 开头缺少正斜杠(以及缺少协议签名)将指示应用服务器构造其相对于原始请求 URL 的 URL!即用户在客户端输入浏览器的URL。

      重要的是要知道这个构造的 URL 既不是

      • 相对于域

        也不

      • 相对于网络容器!

      再一次:应用服务器构造的url将相对于客户端请求的原始url!

      例如:如果客户端提供 URL

      http://www.example.com/level1/level2/stuff.htm

      然后是命令...

      response.sendRedirect("foo/stuff.htm")

      来自 servlet 或,

      &lt;a href="foo/stuff.htm"&gt;Stuff page&lt;/a&gt;

      来自 JSP,会将您重定向到

      http://www.example.com/level1/level2/foo/stuff.htm

      
      
      

      // WILL NOT WORK! Reason: Case sensitivity. response.sendRedirect("/teluskolearnings/login.jsp");

      // WILL WORK! Reason: Case sensitivity. response.sendRedirect("/TeluskoLearnings/login.jsp");

      // Will redirect to localhost:8080/login.jsp as the forward slash tells app // server to build the url RELATIVE TO THE APP SERVER. // So you will be pointed to '@987654324@'. // This is not what we want. response.sendRedirect("/login.jsp");

      // Will redirect to localhost:8080/TeluskoLearnings/login.jsp // as the ABSENCE of forward slash tells app server to build the url // RELATIVE TO THE URL! // So you will be pointed to // '@987654325@'. // This IS what we want. response.sendRedirect("login.jsp");

      // Will redirect to localhost:8080/TeluskoLearnings/foo/login.jsp // (you can see the redirection in the address bar, even if you get a // 404 - page not found) as the ABSENCE of forward slash (at the start) tells // app server to build the URL RELATIVE TO THE REQUESTED URL! // This also means that if the user entered // '@987654326@"' // he will be pointed to // '@987654327@' // (provided of course, that "/level1/level2/stuff" is captured inside the // urlPatterns parameter of the @WebServlet() annotation). response.sendRedirect("foo/login.jsp");

    查看:https://www.safaribooksonline.com/library/view/head-first-servlets/9780596516680/ch04s27.html

    【讨论】:

      猜你喜欢
      • 2011-04-05
      • 2012-03-25
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2016-03-12
      相关资源
      最近更新 更多