ServletRequest.getRequestDispatcher和ServletContext.getRequestDispatcher的区别。

2021-07-08 14:31:24 浏览数 (1)

本节介绍以下几种跳转方法。

1.用RequestDispatcher跳转:

马克-to-win: javax.servlet.RequestDispatcher是Sun公司编写的一个Interface,顾名思义,它可以把用户请求"a资源"的request派遣(dispatch)到"b资源"上,从而实现跳转。(本来人家要访问'a资源",你的程序却让人家自动的跳转到"b资源"上。) 注意:RequestDispatcher有两种形式:这两种形式都能达到跳转的目的。一种是ServletRequest.getRequestDispatcher(String url)。一种是ServletContext.getRequestDispatcher(String url)。但正常来讲,二者都只能访问同一web应用的资源。见下面理论论述和实验。

马克-to-win:注意以下两者的而区别:ServletContext.getRequestDispatcher(String

url)中的url只能使用绝对路径; 而ServletRequest.getRequestDispatcher(String

url)中的url可以使用相对路径(两个资源在同一个目录下,所以省去路径),也可以绝对路径。假如你写成相对路径时,比如

request.getRequestDispatcher("add").forward(request, response);

系统会在同一目录下找add,见后面的例子。

但是假如你写成request.getRequestDispatcher(/WebModule1/jsp1.jsp).forward

(request, response); 系统也会傻傻的变成:request.getRequestDispatcher(http://localhost:

8080/WebModule1/WebModule1/jsp1.jsp).forward(request, response);

当然你更不要写成:request.getRequestDispatcher(http://localhost:

8083/WebModule1/jsp1.jsp).forward(request, response).道理是一样的。<

下面这段话来自于Sun公司的官方文档,大家可以参考一下。对于初学者来讲,没必要完全搞懂。

from documentation: request.getRequestDispatcher: The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root.(马克-to-win:上面的翻译就是相对时不能访问到当前servlet的外面,用/就代表当前应用的根目录,见我下面的例子)

底下我们就通过一个实验来说明这二者的区别在。本个实验中,web.xml主要内容是这样的:

<servlet>

<servlet-name>ServletHello1</servlet-name>

<servlet-class>com.ServletHello1</servlet-class>

</servlet>

<servlet>

<servlet-name>AddCookie</servlet-name>

<servlet-class>com.AddCookie</servlet-class>

</servlet>

<servlet>

<description>

</description>

<servlet-name>DeleteCookie</servlet-name>

<servlet-class>com.DeleteCookie</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ServletHello1</servlet-name>

<url-pattern>/Hello/MarkToWinServletHello1</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>AddCookie</servlet-name>

<url-pattern>/Cookie/AddCookie</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>DeleteCookie</servlet-name>

<url-pattern>/Cookie/DeleteCookie</url-pattern>

</servlet-mapping>

AddCookie.java:

package com;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class AddCookie extends HttpServlet {

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

String rav=(String)request.getAttribute("requestkey");

String scs=(String)this.getServletContext().getAttribute("ServletContextKey");

System.out.println(rav " " scs);

}

}

package com;

import java.io.IOException;

import java.io.InputStream;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ServletHello1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String rav=(String)request.getAttribute("requestkey");

String scs=(String)this.getServletContext().getAttribute("ServletContextKey");

System.out.println(rav " " scs);

}

}

1.html是这样的:

test

例:3.1.1

package com;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class DeleteCookie extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setAttribute("requestkey","requstvalue1");

getServletContext().setAttribute("ServletContextKey","ServletContextValue");

/*下面任意一种方法都能正确执行。*/

request.getRequestDispatcher("/Cookie/AddCookie").forward(request, response);

// request.getRequestDispatcher("AddCookie").forward(request, response);

// request.getRequestDispatcher("/WEB-INF/1.html").forward(request, response);

// request.getRequestDispatcher("/Hello/MarkToWinServletHello1").forward(request, response);

// getServletContext().getRequestDispatcher("/Hello/MarkToWinServletHello1").forward(request, response);

// getServletContext().getRequestDispatcher("/WEB-INF/1.html").forward(request, response);

/*下面的写法错误,找不到AddCookie*/

// request.getRequestDispatcher("/AddCookie").forward(request, response);

// getServletContext().getRequestDispatcher("http://localhost:8080/ServletHello/Hello/MarkToWinServletHello1").forward(request, response);

}

}

更多请看:https://blog.csdn.net/qq_44594371/article/details/103163543

jsp

0 人点赞