JavaWeb:RequestDispatcher

Posted 波尔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb:RequestDispatcher相关的知识,希望对你有一定的参考价值。

环境

  • JDK 8
  • Spring Tool Suite 4.6.1
  • Servlet 3.1
  • Tomcat 8.5
  • Maven 3.6.3

RequestDispatcher 说明

RequestDispatcher 定义了很多和跳转路径相关的属性,
这里可以不用关心,主要是服务器的实现需要关心这些属性。

forward

请求转发,即服务器端跳转。

public void forward(ServletRequest request, ServletResponse response)
        throws ServletException, IOException;

include

包含。

public void include(ServletRequest request, ServletResponse response)
        throws ServletException, IOException;

RequestDispatcher 示例

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>jiangbo.javaweb</groupId>
    <artifactId>21java-web-request-dispatcher</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <description>Request Dispatcher 的示例</description>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Servlet01

package jiangbo.javaweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Servlet01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Servlet01: " + resp);

        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = resp.getWriter();

        writer.println("第一个页面前");
        req.getRequestDispatcher("/servlet02").include(req, resp);
        writer.println("第一个页面后");
    }
}

Servlet02

package jiangbo.javaweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Servlet02 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Servlet02: " + resp);

        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println("第二个页面");
    }
}

配置 web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <servlet>
        <servlet-name>servlet01</servlet-name>
        <servlet-class>jiangbo.javaweb.Servlet01</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>servlet01</servlet-name>
        <url-pattern>/servlet01</url-pattern>
    </servlet-mapping>
    
    <servlet>
        <servlet-name>servlet02</servlet-name>
        <servlet-class>jiangbo.javaweb.Servlet02</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>servlet02</servlet-name>
        <url-pattern>/servlet02</url-pattern>
    </servlet-mapping>

</web-app>

浏览器访问

将该项目部署到服务器并启动,在客户端访问 http://localhost:8080/21java-web-request-dispatcher/servlet01 项目地址,然后关闭 tomcat。

浏览器输出

第一个页面前 第二个页面 第一个页面后

以上是关于JavaWeb:RequestDispatcher的主要内容,如果未能解决你的问题,请参考以下文章

javaweb基础——RequestDispatcher接口

JavaWeb学习——页面跳转方式

JavaWeb中使用到的类与接口整理servlet包

关于RequestDispatcher的原理

Servlet请求转发RequestDispatcher接口

RequestDispatcher中 forward()和include()对比