javaweb--请求转发

本文详细介绍了HTTP请求转发的概念,强调它是一种服务器内部资源跳转的方式,不涉及外部资源。请求转发的主要用途包括任务分布和资源共享。特点包括地址栏路径不变以及只能转发到同一服务器内部。通过示例代码展示了如何在Servlet中实现请求转发,以及在实际应用中如何处理转发后的资源访问。同时,文章还讨论了在页面中进行请求转发的情况,并指出在没有正确配置base标签时可能出现的路径问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求转发的定义:

这是一种在服务器内部的资源跳转方式.(换言之,请求转发是不能跳到服务器之外的资源)

重要用途

1.把任务分布在不同的内部资源中完成
2.可以通过request在不同资源中实现资源的传递,因为请求转发是使用同一个请求

特点:

1.浏览器地址栏路径不发生变化

2.只能转发到当前服务器的内部资源中

3.转发只是一次请求(不过是内部发出的)

 

 

 请求转发模拟

public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取请求的参数
        String username = request.getParameter("username");
//        查看参数
        System.out.println("获取到的请求参数为:"+username);
        System.out.println(" 一系列操作...");
        //添加域数据,相当于给材料盖章
        request.setAttribute("key","true");
//        问路Servlet2怎么走,请求转发必须要以 / 打头 表示地址为http://ip:port/工程名/,映射到idea代码的web目录
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Servlet2");
//        得到跳转对象,并开始开始跳转
        requestDispatcher.forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

 

public class Servlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取请求参数
        String username = request.getParameter("username");
//        查看是否有servlet1的域数据
        Object key = request.getAttribute("key");
        System.out.println("查看是否有servlet1的域数据:"+key);
        System.out.println("一系列操作");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

 

 

简单对页面进行请求转发

servlet程序

public class Servlet_forC extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        转发请求
        System.out.println("经过了Servlet_forC程序");
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/dd/a.html");
        requestDispatcher.forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

 

主页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>主页</title>
  </head>
  <body>
  这是主页
  <a href="./dd/a.html">跳转到./dd/a.html</a> <br>
  <a href="http://localhost:8888/servelet_2_war_exploded/Servlet_forC">请求转发</a>
  </body>
</html>

 

 跳转的页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>dd下的a.html</title>
<!--   base标签设置页面相对路径工作时参照的地址 -->
    <base href="http://localhost:8888/servelet_2_war_exploded/dd/a.html">
</head>
<body>

    <a href="../index.jsp">跳转回主页</a>
</body>
</html>

如果没有使用base标签,又因为请求转发不会改变地址栏的信息,所以这时候跳转回首页的相对路径就是以Servlet的地址为基础来进行的,这时候就会出现错误的跳转地址

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔雀南飞梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值