重定向与请求转发
参数可以放在Map中发送
一:访问不同工程下的路径
1、在配置文件中拿参数:
@Value("${auto.visiturl}")
private String visiturl;
2、实现发送代码:
String result = HttpClientUtil.requestPost(visiturl, map, false);//发送
Map<String, String> map = new HashMap<String, String>();
try {
map.put("username", this.userName);
map.put("ip", this.bbsIpString);
map.put("t", this.timestatem+"");
map.put("code", this.code);
map.put("code1", this.code1);
map.put("ReturnUrl", "https://www.cnblogs.com/781811964-Fighter/p/4273891.html");
String result = HttpClientUtil.requestPost(visiturl, map, false);//发送
System.out.println("========================result"+result);
} catch (Exception e) {
e.printStackTrace();
}
二:访问同一工程下的路径
1.重定向:return "redirect:/front/user/toLogin.jhtml";
当然实用重定向要在:SpringMVC中 要有@RequestMapping("/**")注解(@RequestMapping("/**")可参考:http://www.iteye.com/news/32657/)
同时不能有@responseBody注解的情况下
三:response.sendRedirect()与request.getRequestDispatcher().forward(request,response)的区别 :
3.response.sendRedirect("http://localhost:8083/1****/auto/autoSign.jhtml?map="+map);
1、response.sendRedirect(url)-----重定向到指定URL request.getRequestDispatcher(url).forward(request,response) -----请求转发到指定URL
2、response.sendRedirect(url)-----是客户端跳转
request.getRequestDispatcher(url).forward(request,response) -----是服务器端跳转
3、response.sendRedirect(url)跳转到指定的URL地址后,上个页面(跳转之前的原来页面)中的请求全部结束,
原request对象将会消亡,数据将会消失。紧接着,当前新页面会新建request对象,即产生新的request对象。
【详细过程:redirect 会首先发一个response给浏览器,然后浏览器收到这个response后再发一个requeset给服务器,
服务器接收后发新的response给浏览器。这时页面从浏览器获取来的是一个新的request。这时,在原来跳转之前的页面
用request.setAttribute存的东西都没了,如果在当前的新页面中用request.getAttribute取,得到的将会是null。】 request.getRequestDispatcher(url).forward(request,response)是采用请求转发方式,在跳转页面的时候是带着
原来页面的request和response跳转的,request对象始终存在,不会重新创建。【详细过程:forward 发生在服务器内部,
是在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面收到的request不是从浏览器直接发来的,
可能是在转页时己经用request.setAttribute在request里放了数据,在转到的页面就可以直接用request.getAttribute获得数据了。】
4、使用response.sendRedirect()地址栏中的网址将改变
使用request.getRequestDispatcher().forward(request,response)地址栏中的网址保持不变。
5、使用response.sendRedirect()时如果需要传递参数,那只能在url后加参数,
如:url?id=1,而不能通过request或response方式。 使用request.getRequestDispatcher().forward(request,response)如果需要传递参数,
可以在程序内通过response.setAttribute("name",name)来传至下一个页面.而不能在后面带参数传递,
比如servlet?name=frank这样不行。
详细内容请查看:https://www.cnblogs.com/suizhikuo/archive/2013/11/10/3417081.html