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中文網其他相關文章!