Servlet – RequestDispatcher
RequestDispatcher 是javax.servlet包下的接口。使用这个接口,我们在收到请求后在 servlet 中得到一个对象。使用 RequestDispatcher 对象,我们向其他资源发送请求,其中包括( servlet、HTML 文件或 JSP 文件)。 RequestDispatcher 对象可用于将请求转发到资源或将资源包含在响应中。资源可以是动态的或静态的。
如何创建 RequestDispatcher 的对象?
获取对象的三种方式:
1. RequestDispatcher requestDispatcher=ServletContext.getRequestDispatcher(String path);
Description:
- public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container.
- path is a string specifying the pathname to the resource(servlet, HTML file, or JSP file).
2. RequestDispatcher requestDispatcher=ServletContext.getNamedDispatcher(String name);
Description:
- public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container.
- name is a string specifying the name of a servlet to wrap.
3. RequestDispatcher requestDispatcher=request.getRequestDispatcher(“String path”);
Description:
- request is the HttpServletRequest type object.
- path is a string specifying the pathname to the resource. If it is relative, it must be relative to the current servlet.
方法及说明
该类包含两个方法:
1.前进
Syntax:
void forward(ServletRequest request,ServletResponse response) throws ServletException,IOException
Description:
- Modifier and Type:- void
- This method is used to forward a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
- The method get called before the response has been sent to the client. If the response is already sent then the method will throws an IllegalStateException.
- The parameter request(HttpServletRequest type) and response(HttpServletResponse type) are the same objects as were passed to the calling servlet’s service method.
- This method sets the dispatcher type of the given request to DispatcherType.FORWARD.
例子:
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) {
}
}
}
Note: The above code will not run in online IDE this is server-side code.
2.包括
Syntax:
void include(ServletRequest request,ServletResponse response) throws ServletException,IOException
Description:
- Modifier and Type:- void
- This method is used to include the response of resource(for which the request passed servlet, JSP page, HTML file) in the current servlet response.
- The parameter request(HttpServletRequest type) and response(HttpServletResponse type) are the same objects as were passed to the calling servlet’s service method.
- This method sets the dispatcher type of the given request to DispatcherType.INCLUDE.
例子:
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) {
}
}
}
Note: The above code will not run in online IDE this is server-side code.
字段和描述
Type | Name of Field | Description |
---|---|---|
static String | FORWARD_REQUEST_URI | The 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_PATH | The 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_INFO | The 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_PATH | The 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_STRING | The 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_URI | The string contains the name of the request attribute under which the request URI of the target of include is stored. |
static String | INCLUDE_CONTEXT_PATH | The 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_INFO | The 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_PATH | The 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_STRING | The string contains the name of the request attribute under which the query string of the target of an include is stored. |
static String | ERROR_EXCEPTION | The string contains the name of the request attribute under which the exception object is propagated during an error dispatch. |
static String | ERROR_EXCEPTION_TYPE | The 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_MESSAGE | The string contains the name of the request attribute under which the exception message is propagated during an error dispatch. |
static String | ERROR_REQUEST_URI | The 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_NAME | The 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_CODE | The string contains the name of the request attribute under which the response status is propagated during an error dispatch. |