对于某些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. Android(java)学习笔记143:android提供打开各种文件的API接口:setDataAndType

    android 打开各种文件(setDataAndType) private void openFile(File file){ Intent intent = new Intent(); inten ...

  2. gulp 实践

    文档站YDoc 相关注意事项 sass 编译 目录结构 可以直接使用sass编译 ➜ ydoc git:(v2) ✗ sass ./sass/app.scss ./template/source/ap ...

  3. XCode模拟器屏幕显示内容非常慢

    今天遇到了一个问题,就是XCode模拟器屏幕显示的非常慢,但是点击一下模拟器,内容就会马上显示出来,最后查找出问题是自己不注意把刷新UI界面的代码放到了异步线程中去执行了,刷新UI界面的代码放到主线程 ...

  4. ModelAndView使用方法

    配置支持ModelAndView 在application.xml中配置支持ModelAndView,配置方式有两种. 配置一 <bean id="ViewResolver" ...

  5. C#微信公众号开发 -- (三)用户关注之后自动回复

    通过了上一篇文章之后的微信开发者验证之后,我们就可以做微信公众号的代码开发了. 当我们点击关注某个公众号的时候,有时候会发现他会自动给我们回复一条消息,比如欢迎关注XXX公众号.这个功能其实是在点击关 ...

  6. com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException

    在日志中, 查看导入的包是否是 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;

  7. oracle_11g 不同用户之间的数据迁移

    众所周知,IMP工具的FROMUSER和TOUSER参数可以实现将一个用户的的数据迁移到另外一个用户.同样的功能在IMPPDP工具中如何得以体现呢?答案就是:使用IMPPDP的REMAP_SCHEMA ...

  8. iOS lanchImage 和icon的设置

    1 icon的设置 打开项目中的Assets.xcassets   这里边有一个icon 首先需要有icon 的尺寸 尺寸如下: 29*29   2x 29*29   3x 40*40  2x 40* ...

  9. UVA 11078 Open Credit System(扫描 维护最大值)

    Open Credit System In an open credit system, the students can choose any course they like, but there ...

  10. HDU 5351 MZL's Border (规律,大数)

    [HDU 5351 MZL's Border]题意 定义字符串$f_1=b,f_2=a,f_i=f_{i-1}f_{i-2}$. 对$f_n$的长度为$m$的前缀$s$, 求最大的$k$满足$s[1] ...