首页 > 其他分享 >10-Redirect重定向

10-Redirect重定向

时间:2022-08-15 21:13:59浏览次数:77  
标签:Redirect 10 重定向 req import servlet javax 请求

10-Redirect重定向

概述

本文主要讲解重定向,什么是重定向,重定向(redirect)和我们之前学的转发(diapatcher)有什么相同和不同之处呢?
重定向
重定向是通过各种方法将各种网络请求重新定个方向转到其他位置
请求转发
请求转发是在web服务器内部,将请求从一个servlet转到另一个servlet

重定向VS请求转发

  1. 重定向浏览器是发送两次请求,而请求转发浏览器只发一次请求
  2. 重定向浏览器地址发生变化,请求转发则浏览器地址不变
  3. 重定向是从浏览器发送出去的,请求可以使用request域对象
  4. 重定向没有request域对象,而请求转发可以使用request域对象
  5. 重定向可以去请求在本网站外部的资源,请求转发是在web服务器内部,不能请求本网站外的资源

实践

请求转发示例参见转发
本实践需要接上一篇
新增以下文件或配置
RedirectServlet.java


package com.kuang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @since 2022-08-15
 */
public class RedirectServlet extends HttpServlet {
    private static final long serialVersionUID = -1351713222498041389L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("/s4/image");
    }

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0"
         metadata-complete="true">
    <servlet>
        <servlet-name>image</servlet-name>
        <servlet-class>com.kuang.servlet.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>image</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>redirect</servlet-name>
        <servlet-class>com.kuang.servlet.RedirectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>redirect</servlet-name>
        <url-pattern>/redirect</url-pattern>
    </servlet-mapping>
</web-app>

再一个jsp的小例子

RequestTest.java


package com.kuang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @since 2022-08-15
 */
public class RequestTest extends HttpServlet {
    private static final long serialVersionUID = 6521499379637028043L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username + ":" + password);
        resp.sendRedirect("/s4/success.jsp");
    }

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

index.jsp

<html>
<body>
<h2>Hello World!</h2>
<form action="${pageContext.request.contextPath}/login" method="get">
    用户名:<input type="text" name="username"> <br>
    密码:<input type="password" name="password"> <br>
    <input type="submit">
</form>
</body>
</html>

success.jsp

<html>
<body>
<h2>Success</h2>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0"
         metadata-complete="true">
    <servlet>
        <servlet-name>image</servlet-name>
        <servlet-class>com.kuang.servlet.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>image</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>redirect</servlet-name>
        <servlet-class>com.kuang.servlet.RedirectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>redirect</servlet-name>
        <url-pattern>/redirect</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>req</servlet-name>
        <servlet-class>com.kuang.servlet.RequestTest</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>req</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
</web-app>

在登录页面输入用户名密码后会跳转到成功页面

标签:Redirect,10,重定向,req,import,servlet,javax,请求
From: https://www.cnblogs.com/Oh-mydream/p/16589630.html

相关文章

  • 银河麒麟桌面操作系统V10-SP1下载安装
    1.制作U盘启动盘(Windows系统环境下)首先插入U盘,然后下载启动盘制作工具ventoy,下载路径为windows存储空间中。下载链接:https://www.lanzoui.com/b01bd54gb压缩包解压如下......
  • mysql 免密登录(解决报错ERROR 1045 (28000))
    mysql登录时,ERROR1045(28000):错误解决办法通常出现的报错如下:ERROR1045(28000):Accessdeniedforuser'ODBC'@'localhost'(usingpassword:NO)ERROR1045(2......
  • Tomcat10下载与安装
    参考链接:https://blog.csdn.net/weixin_47700137/article/details/116055222下载下载地址:https://tomcat.apache.org/  下载后解压:配置环境添加环境变量  ......
  • ASEMI整流桥KBPC3510W参数特性,KBPC3510W封装尺寸
    编辑-ZASEMI整流桥KBPC3510W参数:型号:KBPC3510W最大重复峰值反向电压(VRRM):1000V最大RMS电桥输入电压(VRMS):700V最大直流阻断电压(VDC):1000V最大平均正向整流输出电流(IF):35A......
  • HC32L110 在 Ubuntu 下使用 J-Link 烧录
    目录HC32L110(一)HC32L110芯片介绍和Win10下的烧录HC32L110(二)HC32L110在Ubuntu下的烧录HC32L110在Ubuntu下使用J-Link烧录以下说明在Ubuntu下如何配置HC......
  • Win10+VS2019+Qt5.15.2下编译QCAD
    Win10+VS2019+Qt5.15.2下编译QCAD目录Win10+VS2019+Qt5.15.2下编译QCAD环境配置Qt安装VisualStudio2019安装QCAD编译Clone编译QCAD编译QtScripts插件运行问题总结参考......
  • 基于C++的OpenGL 10 之光照贴图
    1.引言本文基于C++语言,描述OpenGL的光照贴图前置知识可参考:基于C++的OpenGL09之材质-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词......
  • 1080 MOOC期终成绩——25分
    对于在中国大学MOOC学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分(满分100)。总评成绩的计算公式为G=......
  • 1079 延迟的回文数——20分
    给定一个k+1位的正整数N,写成ak…a1a0的形式,其中对所有i有0<=ai<10且ak>0。N被称为一个回文数,当且仅当对所有i有ai=ak-i。零也被定义为一个回文数。......
  • Kotlin学习快速入门(10)—— 重载运算符使用
    原文:Kotlin学习快速入门(10)——重载运算符使用-Stars-One的杂货小窝Kotlin中提供了基础的运算符,但是只是针对基础的数据类型,如Int,Double等如果我们想让两个对象可以......