重定向和转发

重定向

用户通过浏览器发送一个请求,web服务器(Tomcat)会接受到这个请求,会给浏览器发送一个状态码 302。并且设置一个重定向的路径。浏览接到302状态码,会自动加载到服务器设置的路径。

登录页面跳转到主页
特征:
​ 1.重定向的过程是浏览器的行为
​ 2.实际上浏览器做了2次请求
​ 3.上一次请求的request对象会丢失
​ 4.重定向有一个非常明显的特征,浏览器的url改变了
​ 5.重定向可以任意的网络资源

示例:

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="LoginServlet" method="post">
    <input type="text" name="username"><br>
    <input type="password" name="password"><br>
    <input type="submit" value="登录">
</form>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
首页
</body>
</html>

LoginServlet.java:

package com.qfedu.a_servlet;

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 java.io.IOException;
import java.io.PrintWriter;

/**
 * @author xue_yun_xiang
 * @create 2021-04-09-20:14
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);

        //开始设置响应的重定向
        //username password的值是不能带到target.html页面中的
        response.sendRedirect("target.html");

    }
}

转发


户发送请求到服务器
服务器接收当前的请求
会调用内部的处理方式处理该请求
最终把响应发送给客户端

特征:
​ 1.转发是服务器行为
​ 2.浏览器在整个过程中发生了一次行为
​ 3.转发会带有用户的HttpServletRequest
​ 4.URL没有发生任何变化
​ 5.转发只能在当前项目中进行。转发不能到网络资源

示例:

TestServlet1.java:

package com.qfedu.a_servlet;

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 java.io.IOException;

/**
 * @author xue_yun_xiang
 * @create 2021-04-09-20:32
 */
@WebServlet("/TestServlet1")
public class TestServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        System.out.println("用户请求1");

        //可以给当前的request设置一些数据
        request.setAttribute("name", "王一");

        //通过HttpServlet对象,转发对象到其他资源中(另外一个servlet)
        request.getRequestDispatcher("TestServlet2").forward(request, response);
    }
}

TestServlet2.java:

package com.qfedu.a_servlet;

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 java.io.IOException;

/**
 * @author xue_yun_xiang
 * @create 2021-04-09-20:32
 */
@WebServlet("/TestServlet2")
public class TestServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        Object name = request.getAttribute("name");
        String s = name.toString();
        System.out.println(name);
        System.out.println(s);
        response.getWriter().append(s);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值