对于某些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的区别的更多相关文章

  1. 请求转发(Forward)和重定向(Redirect)的区别

    forward(转发): 是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,因为这个跳转过程实在 ...

  2. Django框架----render函数和redirect函数的区别

    render函数和redirect函数的区别: render:只会返回页面内容,但是未发送第二次请求 redirect:发挥了第二次请求,url更新 具体实例说明 render: redirect:

  3. render函数和redirect函数的区别+反向解析

    render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象)   request对象:----->所有的请求 ...

  4. 直接请求转发(Forward)和间接请求转发(Redirect)两种区别?

    用户向服务器发送了一次HTTP请求,该请求肯能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相互转发请求,但是用户是感觉不到请求转发的.根据转发方式的不同,可以区分为直接请求转发 ...

  5. java面试题之----转发(forward)和重定向(redirect)的区别

    阅读目录 一:间接请求转发(Redirect) 二:直接请求转发(Forward) 用户向服务器发送了一次HTTP请求,该请求可能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相 ...

  6. 转发(Forward)和重定向(Redirect)的区别

    转发是服务器行为,重定向是客户端行为. 转发(Forword) :通过RequestDispatcher对象的forward(HttpServletRequest request,HttpServle ...

  7. 转发forward和重定向redirect的区别

    本质区别:转发只发送一次请求,重定向发送两次请求. 转发: request.getRequestDispatcher("/HiServlet").forward(request,r ...

  8. 转发(forward)和重定向(redirect)的区别?

    1)forward是容器中控制权的转向,是服务器请求资源,服务器直接访问目标地址的URL,把那个URL 的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所 ...

  9. spring controller中默认转发、forward转发、redirect转发之间的区别

    默认转发 @RequestMapping("/123") public String test(HttpSession session) { System.out.println( ...

随机推荐

  1. 编程以外积累: 如何给项目生成类似VS2008的说明文档

    1:[下载] 目前微软提供的官方开源工具 Sandcastle结果跑到项目中一看,抬头就来了这么一段: The Sandcastle CodePlex project is no longer und ...

  2. python--class test

    # !usr/bin/env  python3#-*- coding:utf-8 -*- 'a test class'class Student(object):    def __init__(se ...

  3. Ubuntu 14.04 忘记用户密码(备忘)

    参考文章地址:www.linuxidc.com/Linux/2013-11/92236.htm 重启电脑 开机就会进入一个Grub引导页面,选择 "Ubuntu 高级选项"之后,按 ...

  4. HDU-1060(简单数学)

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) P ...

  5. 利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现

    1: 面向过程的编写方法 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image ...

  6. HeaderViewListAdapter cannot be cast to listAdapter问题原因及解决办法

    [o] 在listView中添加leaderView 和footerView的时候要注意在setAdapter之前调用,不然会报如下异常: listAdapter cannot be cast to ...

  7. nyoj832 合并游戏(状态压缩DP)

    题意 : n个石子, 给你一个n*n矩阵, A[i][j]表示第i个和第j个合并蹦出的金币值, 合并完石子 j 消失.求合并所有石子后,所得的最大金币数. 分析 :     1. 题中给的数据范围   ...

  8. Spring.net架构示例(含Aop和Ioc)源码

    最近写了一个Spring.net的架构. 一.架构主图 架构图的数据流程走向是: UI层=>UILogic>=>Service>Business=>DataAccess ...

  9. 二、 What's Maven,How to learning?

    1. 哈哈,什么是Maevn, ←_←|| ?我怎么知道,来看看官方解释, Apache Maven is a software project management and comprehensio ...

  10. Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)

    1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...