📜  Servlet – RequestDispatcher

📅  最后修改于: 2022-05-13 01:55:48.928000             🧑  作者: Mango

Servlet – RequestDispatcher

RequestDispatcher 是javax.servlet包下的接口。使用这个接口,我们在收到请求后在 servlet 中得到一个对象。使用 RequestDispatcher 对象,我们向其他资源发送请求,其中包括( servlet、HTML 文件或 JSP 文件)。 RequestDispatcher 对象可用于将请求转发到资源或将资源包含在响应中。资源可以是动态的或静态的。

如何创建 RequestDispatcher 的对象?

获取对象的三种方式

方法及说明

该类包含两个方法

1.前进

例子:

Java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class GFG extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
    {
        // Perform all the work as per your
          // application's architecture
        try {
            RequestDispatcher requestDispatcher;
 
            // path is a string specifying the pathname to
            // the resource. If it is relative, it must be
            // relative against the current servlet
            requestDispatcher=request.getRequestDispatcher("path");
            requestDispatcher.forward(request, response);
        }
        catch (ServletException servletException) {
        }
        catch (IOException ioException) {
        }
        catch (IllegalStateException illegalStateException) {
        }
    }
}


Java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class GFG extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
    {
        // Perform all the work as
        // per your application's architecture
        try {
            RequestDispatcher requestDispatcher;
 
            // path is a string specifying the pathname to
            // the resource. If it is relative, it must be
            // relative against the current servlet
            requestDispatcher=request.getRequestDispatcher("path");
            requestDispatcher.include(request, response);
        }
        catch (ServletException servletException) {
        }
        catch (IOException ioException) {
        }
    }
}


2.包括

例子:

Java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class GFG extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
    {
        // Perform all the work as
        // per your application's architecture
        try {
            RequestDispatcher requestDispatcher;
 
            // path is a string specifying the pathname to
            // the resource. If it is relative, it must be
            // relative against the current servlet
            requestDispatcher=request.getRequestDispatcher("path");
            requestDispatcher.include(request, response);
        }
        catch (ServletException servletException) {
        }
        catch (IOException ioException) {
        }
    }
}

字段和描述

Type

Name of Field

Description

static String FORWARD_REQUEST_URIThe string contains the name of the request attribute under which the original request URI is made available to the target of a forward.
static String FORWARD_CONTEXT_PATHThe string contains the name of the request attribute under which the original context path is made available to the target of a forward.
static String FORWARD_PATH_INFOThe string contains the name of the request attribute under which the original path info is made available to the target of a forward.
static String FORWARD_SERVLET_PATHThe string contains the name of the request attribute under which the original servlet path is made available to the target of a forward.
static String FORWARD_QUERY_STRINGThe string contains the name of the request attribute under which the original query string is made available to the target of a forward.
static String INCLUDE_REQUEST_URIThe string contains the name of the request attribute under which the request URI of the target of include is stored.
static String INCLUDE_CONTEXT_PATHThe string contains the name of the request attribute under which the context path of the target of an include is stored.
static String INCLUDE_PATH_INFOThe string contains the name of the request attribute under which the path info of the target of an include is stored.
static String INCLUDE_SERVLET_PATHThe string contains the name of the request attribute under which the servlet path of the target of an include is stored.
static String INCLUDE_QUERY_STRINGThe string contains the name of the request attribute under which the query string of the target of an include is stored.
static String ERROR_EXCEPTIONThe string contains the name of the request attribute under which the exception object is propagated during an error dispatch.
static String ERROR_EXCEPTION_TYPEThe string contains the name of the request attribute under which the type of the exception object is propagated during an error dispatch.
static String ERROR_MESSAGEThe string contains the name of the request attribute under which the exception message is propagated during an error dispatch.
static String ERROR_REQUEST_URIThe string contains the name of the request attribute under which the request URI whose processing caused the error is propagated during an error dispatch.
static String ERROR_SERVLET_NAMEThe string contains the name of the request attribute under which the name of the servlet in which the error occurred is propagated during an error dispatch.
static String ERROR_STATUS_CODEThe string contains the name of the request attribute under which the response status is propagated during an error dispatch.