RequestDispatcher 接口允许将请求路由到另一个资源,该资源可以是 HTML、Servlet 或 JSP。该界面还可用于合并附加资源的内容。是Servlet协作方式之一。 RequestDispatcher 接口是 java.io.RequestDispatcher 接口的一部分。 Servlet 包。使用此接口,Servlet 在收到请求后返回一个对象。
广告 该类别中的热门课程 JAVA SERVLET - 专业化 | 18 门课程系列 | 6 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
servlet RequestDispatcher 使用用户界面来请求一个源链接到另一个源。 requestdispatcher 将表单数据发送到验证 servlet 页面。如果 servlet 页面验证了该信息,则请求调度程序会使用路径或字符串名称将链接转发到另一个 servlet 或 JSP 页面。如果网页未验证信息,请求调度程序对象将包含 HTML 或 JSP 页面并显示错误消息。
RequestDispatcher 对象可以向资源发送请求或将其包含在响应中。资源可以是静态的或动态的。在 servlet 页面上创建 servlet requestdispatcher 有三种方法。首先,我们必须使用路径或页面路径的字符串名称。
以下语法展示了如何创建带有路径的请求调度程序对象。
语法:
RequestDispatcher requestdispatcherObject = ServletContext.getRequestDispatcher(" String file_path");
说明:
以下语法展示了如何创建请求调度程序的对象。
语法:
RequestDispatcher requestdispatcherObject = ServletContext.getNamedDispatcher(" String name");
说明:
以下语法展示了如何使用请求接口创建请求调度程序的对象。
语法:
RequestDispatcher requestdispatcherObject = request.getRequestDispatcher(" String file_path");
说明:
requestdispatcher 有两种方法用于 servlet 和 Html 页面。此方法将文件源转发或包含到下一个源。
如果信息有效并且网页被转发到下一个servlet页面,则使用forward方法。
语法:
以下语法展示了如何使用forward方法。
void forward(ServletRequest req, ServletResponse resp) throws ServletException, IOException
说明:
示例:
代码:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Register extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = resp.getWriter(); String first_id = req.getParameter("fn"); RequestDispatcher rdispatcher = req.getRequestDispatcher("/index.html"); rdispatcher.include(req, resp); } }
如果信息无效,则该页面将包含带有错误消息的同一页面。
语法:
以下语法展示了如何使用 include 方法。
void include(ServletRequest req, ServletResponse resp) throws ServletException, IOException
说明:
示例:
代码:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Register extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = resp.getWriter(); String first_id = req.getParameter("fn"); printout.print("Sorry!! Wrong UserId!"); RequestDispatcher rdispatcher = req.getRequestDispatcher("/index.html"); rdispatcher.include(req, resp); } }
The servlet requestdispatcher requires the following four files:
index.html: create the required form
Code:
<!DOCTYPE html> <html> <head> <title> Basic form </title> </head> <body> <form action = "first_servlet" method = "post"> <label for = "fn"> User Id: </label> <input type = "text" id = "fn" name = "fn"><br><br> <label for = "ln"> First name: </label> <input type = "text" id = "ln" name = "ln"><br> <input type = "submit" value = "Submit"> </form> </body> </html>
Register.java: create the first servlet with a request dispatcher interface
Code:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Register extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = resp.getWriter(); String first_id = req.getParameter("fn"); String first_name = req.getParameter("ln"); if(first_id.equals("servlet"){ RequestDispatcher rdispatcher = req.getRequestDispatcher("sec_servlet"); rdispatcher.forward(req, resp); } else{ printout.print("Sorry!! Wrong UserId!"); RequestDispatcher rdispatcher = req.getRequestDispatcher("/index.html"); rdispatcher.include(req, resp); } } }
FinalServlet.java: create a second servlet page for the required output
Code:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FinalServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = resp.getWriter(); String name = req.getParameter("ln"); printout.print("Welcome "+name); } }
Web.xml: create servlet parameters with its page
Code:
<web-app> <servlet> <servlet-name> Register </servlet-name> <servlet-class> Register </servlet-class> </servlet> <servlet> <servlet-name> FinalServlet </servlet-name> <servlet-class> FinalServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> Register </servlet-name> <url-pattern> /first_servlet </url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> FinalServlet </servlet-name> <url-pattern> /sec_servlet </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index.html </welcome-file> </welcome-file-list> </web-app>
Output 1: form page
Output 2: servlet page output with an error message
Output 2: servlet page final output
The servlet requestdispatcher interface moves users from one source to another web application source. It is forward and includes the file path per requirement and source output.
以上是Servlet 请求调度程序的详细内容。更多信息请关注PHP中文网其他相关文章!