Servlet-请求转发

 

 一、什么是请求转发

请求转发是指,服务器收到请求后从一个资源跳到另一个资源的操作叫做请求转发。

二、具体操作

  Servlet1

  1.获取请求的参数

  2.Servlet处理数据

  3.问路,Servlet2怎么走

  4.走向Servlet2


 
Servlet2
 1.获取请求的参数(办事的材料)
 2.查看Servlet1是否盖章
 3.处理自己的业务

相关代码
package com.orz.requestdispatcher.servlet;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 请求的转发
 * 1.获取请求的参数
 * 2.Servlet处理数据
 * 3.问路,Servlet2怎么走
 * 4.走向Servlet2
 * @author orz
 * @create 2020-09-10 22:33
 */
public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Servlet1 doGet");

        //1.获取请求的参数
        String username = req.getParameter("username");

        System.out.println("在Servlet1(柜台1)中查看参数(材料):username="+username);

        //2.Servlet处理数据
        //给材料盖个章,并传递到Servlet2中查看
        req.setAttribute("key1","柜台1的章");

        //3.问路,Servlet2怎么走

        /*
         *请求转发必须以斜杠打头  斜杠 / 表示   http://ip:port/工程名称/       (映射到idea代码的web目录)
         */

       RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");


        //4.走向Servlet2
        requestDispatcher.forward(req,resp);
    }
}
View Code
package com.orz.requestdispatcher.servlet;

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

import java.io.IOException;

/**
 * 请求的转发
 * 1.获取请求的参数(办事的材料)
 * 2.查看Servlet1是否盖章
 * 3.处理自己的业务
 * @author orz
 * @create 2020-09-10 22:34
 */
public class Servlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Servlet2 doGet");

        //1.获取请求的参数(办事的材料)
        String username = req.getParameter("username");
        System.out.println("在Servlet2(柜台2)中查看参数(材料):username="+username);
        //2.查看Servlet1是否盖章
        Object key1 = req.getAttribute("key1");
        System.out.println("柜台1是否有章:"+key1);

        //3.处理自己的业务
        System.out.println("Servlet2");
    }
}
View Code

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>Servlet1</servlet-name>
        <servlet-class>com.orz.requestdispatcher.servlet.Servlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet1</servlet-name>
        <url-pattern>/servlet1</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Servlet2</servlet-name>
        <servlet-class>com.orz.requestdispatcher.servlet.Servlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet2</servlet-name>
        <url-pattern>/servlet2</url-pattern>
    </servlet-mapping>
</web-app>
View Code
 
posted @ 2020-09-10 22:49  orz江小鱼  阅读(222)  评论(0编辑  收藏  举报