在web程序中,经常是由多个Servlet来完成请求.RequestDispatcher接口就是为了多个Servlet之间的调整而实现的.该接口可以用httpSerbletRequest的getRequestDispatcher()方法取得.调用时指定跳转的URL地址即可完成跳转动作.RequestDispatcher接口有两种方法实现跳转
一.include方法
本示例不需要前台页面,直接通过地址访问.
IncludeMethod.java代码
1 package com.gys; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 @WebServlet( 14 name="demo", 15 urlPatterns={"/include"} 16 ) 17 public class IncludeMethod extends HttpServlet{ 18 @Override 19 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 20 throws ServletException, IOException { 21 doGet(req, resp); 22 } 23 @Override 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 response.setContentType("text/html;charset=utf-8"); 27 PrintWriter out=response.getWriter(); 28 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 29 out.println("<HTML>"); 30 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 31 out.println(" <BODY>"); 32 out.print(" <h1>第一个servlet</h1>"); 33 RequestDispatcher dispatcher=request.getRequestDispatcher("/includeSecond"); 34 dispatcher.include(request, response); 35 out.println(" </BODY>"); 36 out.println("</HTML>"); 37 out.flush(); 38 out.close(); 39 } 40 }
IncludeMethod2.java
1 package com.gys; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 13 @WebServlet( 14 name="include2", 15 urlPatterns={"/includeSecond"} 16 ) 17 public class IncludeMethod2 extends HttpServlet{ 18 @Override 19 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 20 throws ServletException, IOException { 21 doGet(req, resp); 22 } 23 @Override 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 response.setContentType("text/html;charset=utf-8"); 27 PrintWriter out=response.getWriter(); 28 out.println("这是第二个servlet(被包涵在第一个servlet里面)"); 29 } 30 }
测试结果:
二.forward()
forwardMethod.java代码
1 package com.gys2; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 @WebServlet( 12 name="forwardMethod", 13 urlPatterns={"/test"} 14 ) 15 16 public class forwardMethod extends HttpServlet { 17 @Override 18 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 19 throws ServletException, IOException { 20 doGet(req, resp); 21 } 22 @Override 23 protected void doGet(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 request.getRequestDispatcher("/test.html").forward(request, response); 26 } 27 }
运行结果是这个样子的:
时间: 2024-12-23 22:06:51