本篇博客介绍的是Servlet 里的RequestDispatcher的调派请求,调派请求有两种方法。第一种是include可以将另一个Servlet的操作流程包括至目前Servlet的操作流程中。例如以下的事例
teacher1.view
package xhxy;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;
@WebServlet("/teacher1.view")
public class teacher1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public teacher1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.println("你好我是任课老师,我问问出题老师");
RequestDispatcher dispatcher=request.getRequestDispatcher("Teacher2.view");
dispatcher.include(request, response);
out.print("我还是任课老师");
out.close();
}
}
teacher2.view
package xhxy;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Teacher2
*/
@WebServlet("/Teacher2.view")
public class Teacher2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Teacher2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.println("我是出题老师,重点是。。。。");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行的结果如下所示:
方法二:forward,forward的含义是转发是把请求转发给另一个Servlet自己不进行处理。
把include改为forward之后的代码
package xhxy;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;
@WebServlet("/teacher1.view")
public class teacher1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public teacher1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.println("你好我是任课老师,我问问出题老师");
RequestDispatcher dispatcher=request.getRequestDispatcher("Teacher2.view");
dispatcher.forward(request, response);
out.print("我还是任课老师");
out.close();
}
}
运行的结果如下所示:
从这两张运行结果的图片也可以看出include方法和forward方法的区别。