forward:hello 与 redirect:hello的区别
对于某些Controller的处理方法,当返回值为String类型时,返回的结果中可能含有forward或redirect前缀;
如:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/forward")
public String replyWithForward(HttpServletRequest request, String userId){
request.setAttribute("userid", userId); System.out.println("userId =" +userId); return "forward:hello";
} @RequestMapping("/redirect")
public String replyWithRedirect(HttpServletRequest request, String userId){
request.setAttribute("userid", userId); System.out.println("userId = "+userId); return "redirect:hello";
}
}
测试页面hello.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String userId = (String)request.getAttribute("userid");
System.out.println("获取到的userId为:"+userId);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试页面</title>
</head>
<body>
hello, <%= userId %>;
</body>
</html>
当路径为 http://localhost:8080/crazysnailweb/user/forward?userId=123时,浏览器中的URL不会发生变化,且页面输出:

当路径为http://localhost:8080/crazysnailweb/user/redirect?userId=123 时,浏览器中URL变为http://localhost:8080/crazysnailweb/user/hello,且页面输出:

注:redirect会让浏览器发起一个新的请求,因而原来request对象中的参数丢失;而forward所到的目标地址位于当前请求中,request中的参数不会丢失;
forward:hello 与 redirect:hello的区别的更多相关文章
- 请求转发(Forward)和重定向(Redirect)的区别
forward(转发): 是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,因为这个跳转过程实在 ...
- Django框架----render函数和redirect函数的区别
render函数和redirect函数的区别: render:只会返回页面内容,但是未发送第二次请求 redirect:发挥了第二次请求,url更新 具体实例说明 render: redirect:
- render函数和redirect函数的区别+反向解析
render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象) request对象:----->所有的请求 ...
- 直接请求转发(Forward)和间接请求转发(Redirect)两种区别?
用户向服务器发送了一次HTTP请求,该请求肯能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相互转发请求,但是用户是感觉不到请求转发的.根据转发方式的不同,可以区分为直接请求转发 ...
- java面试题之----转发(forward)和重定向(redirect)的区别
阅读目录 一:间接请求转发(Redirect) 二:直接请求转发(Forward) 用户向服务器发送了一次HTTP请求,该请求可能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相 ...
- 转发(Forward)和重定向(Redirect)的区别
转发是服务器行为,重定向是客户端行为. 转发(Forword) :通过RequestDispatcher对象的forward(HttpServletRequest request,HttpServle ...
- 转发forward和重定向redirect的区别
本质区别:转发只发送一次请求,重定向发送两次请求. 转发: request.getRequestDispatcher("/HiServlet").forward(request,r ...
- 转发(forward)和重定向(redirect)的区别?
1)forward是容器中控制权的转向,是服务器请求资源,服务器直接访问目标地址的URL,把那个URL 的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所 ...
- spring controller中默认转发、forward转发、redirect转发之间的区别
默认转发 @RequestMapping("/123") public String test(HttpSession session) { System.out.println( ...
随机推荐
- position定位问题
position属性规定了元素的定位类型,默认为static.该属性还可以有下值:absolute:生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位.fixed:生成绝对定位的 ...
- c结构体初始化问题
结构体中的数组的初始化问题 我的代码 C/C++ code #include <stdio.h> #include <stdlib.h> struct a { int leng ...
- Mac安装Mysql过程
1.Mysql官网下载安装包 http://dev.mysql.com/downloads/mysql/ 选择Mac OS X 10.10 (x86, 64-bit), DMG Archive版本下载 ...
- css 多栏自适应布局
在页面重构中,我们经常会需要实现多栏布局,例如n栏固定宽度 + m栏自适应宽度的组合,绝对布局+padding+百分比宽度是容易想到的比较暴力的解决方法,但是作为未来的"工程师", ...
- Java NIO 选择器(Selector)的内部实现(poll epoll)
http://blog.csdn.net/hsuxu/article/details/9876983 之前强调这么多关于linux内核的poll及epoll,无非是想让大家先有个认识: Java NI ...
- linq 多条件查询 where 拼接+分页
首先定义一个静态类 public static class QueryAssembly { /// <summary> /// 返回true /// </summary> // ...
- java ,js获取web工程路径
一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...
- 网卡添加VLAN TAG
#modprobe 8021q 用命令 lsmod | grep 8021q 来检查 以配置网卡eth0为例,添加vlan号:1002 ================================ ...
- ubuntu netbeans compile ygopro client with google protobuf lib
environment: ubuntu 16.04 netbeans 8.2 ygopro Fluorohydride with Irrlicht Game Engine 问题1: google pr ...
- 对C++ Primer的10.3.9单词转换的思考
这篇代码有几个知识点可以复习一下,而且小白学到了新知识o(╯□╰)o #include <iostream> #include <string> #include <ma ...