Java中使用 RequestDispatcher 和 HttpServletResponse 的 Servlet 协作
什么是 Servlet 协作?
特定Java Web 应用程序的 servlet 之间的信息交换称为Servlet Collaboration 。这允许通过方法调用将信息从一个 servlet 传递/共享到另一个。
Java提供的实现Servlet Collaboration的主要方式有哪些?
servlet api 提供了两个接口,即:
- javax.servlet.RequestDispatcher
- javax.servlet.http.HttpServletResponse
这两个接口包括负责实现在 servlet 之间共享信息的目标的方法。
使用 RequestDispatcher 接口
RequestDispatcher 接口提供了将客户端的请求分派到另一个 Web 资源的选项,可以是 HTML 页面、另一个 servlet、JSP 等。它提供以下两种方法:
- public void forward(ServletRequest request, ServletResponse response) throws ServletException, Java.io.IOException:
forward() 方法用于将客户端请求传输到另一个资源(HTML 文件、servlet、jsp 等)。调用此方法时,控制权将转移到下一个调用的资源。另一方面,include() 方法用于将调用文件的内容包含到被调用文件中。调用此方法后,控件仍保留在调用资源中,但处理后的输出包含在被调用资源中。
下图解释了它的工作方式:
- public void include(ServletRequest request, ServletResponse response) throws ServletException, Java.io.IOException:
include() 方法用于将调用资源的内容包含到被调用资源中。调用此方法时,控件仍保留在调用资源中。它只是将调用资源的处理输出包含到被调用资源中。
下图解释了它是如何工作的:
- 使用 RequestDispatcher 进行 Servlet 协作的示例
下面的例子解释了如何使用 RequestDispatcher 接口来实现 Servlet Collaboration:
索引.html
html
Java
// First java servlet that calls another resource
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
// The method to receive client requests
// which are sent using 'post'
res.setContentType("text/html");
PrintWriter out = response.getWriter();
// fetches username
String n = request.getParameter("userName");
// fetches password
String p = request.getParameter("userPass");
if(p.equals("Thanos"){
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
// Getting RequestDispatcher object
// for collaborating with servlet2
// forwarding the request to servlet2
rd.forward(request, response);
}
else{
out.print("Password mismatch");
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.include(request, response);
}
}
}
Java
// Called servlet in case password matches
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Welcome extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// fetches username
String n = request.getParameter("userName");
// prints the message
out.print("Welcome " + n);
}
}
html
Login
Login
WelcomeServlet
Welcome
Login
/servlet1
WelcomeServlet
/servlet2
index.html
html
Java
// Servlet class to redirect the text keyword
// in the 'name' field to google.com
// using sendRedirect()
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySearcher extends HttpServlet {
protected void doGet(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter("name");
response.sendRedirect("https://www.google.co.in/#q=" + name);
// response redirected to google.com
}
}
html
MySearcher
MySearcher
MySearcher
/search
index.html
- 登录。Java
Java
// First java servlet that calls another resource
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
// The method to receive client requests
// which are sent using 'post'
res.setContentType("text/html");
PrintWriter out = response.getWriter();
// fetches username
String n = request.getParameter("userName");
// fetches password
String p = request.getParameter("userPass");
if(p.equals("Thanos"){
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
// Getting RequestDispatcher object
// for collaborating with servlet2
// forwarding the request to servlet2
rd.forward(request, response);
}
else{
out.print("Password mismatch");
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.include(request, response);
}
}
}
- 欢迎。Java
Java
// Called servlet in case password matches
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Welcome extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// fetches username
String n = request.getParameter("userName");
// prints the message
out.print("Welcome " + n);
}
}
- web.xml
html
Login
Login
WelcomeServlet
Welcome
Login
/servlet1
WelcomeServlet
/servlet2
index.html
输出:
- 索引.html
- 如果密码匹配:
- 如果密码不匹配:
使用 HttpServletResponse 接口
- HttpServletResponse 接口负责管理 Http 响应。为了实现servlet协作,它使用以下方法:
public void sendRedirect(String URL)throws IOException;
- 此方法用于将响应重定向到另一个资源,可能是 servlet、jsp 或 html 文件。它接受的参数是一个可以是绝对和相对的 URL。它在客户端工作并使用浏览器的 URL 栏发出请求。
使用 sendRedirect() 进行重定向的示例
- 以下使用 servlet 创建的 Web 应用程序示例获取网页文本字段中写入的文本,并将其定向到 servlet。然后 servlet 将其重定向到 google,然后根据所写的文本生成搜索结果。
索引.html
html
Java
// Servlet class to redirect the text keyword
// in the 'name' field to google.com
// using sendRedirect()
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySearcher extends HttpServlet {
protected void doGet(HttpServletRequest
request,
HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter("name");
response.sendRedirect("https://www.google.co.in/#q=" + name);
// response redirected to google.com
}
}
- web.xml
html
MySearcher
MySearcher
MySearcher
/search
index.html
输出:
- 索引.html
- 搜索结果
RequestDiispatcher 的 forward() 方法和 HttpServletResponse 的 sendRedirect() 有什么区别?
- 虽然这两种方法看似做同样的事情,但两者之间还是有区别的,具体如下:
forward() | sendRedirect() |
It works on the server side | It works on the client side |
It sends the same request and response objects to another resource. | It always send a new request |
It works only within the server. | It can be used within and outside the server. |