1.转发(服务端行为)
request.getRequestDispatcher().forward(request,respond)
转发在服务器端发挥作用,通过forward()方法提交信息在多个页面之间进行传递。
转发的特点是:
1.地址栏不会改变
2.转发只能转发到当前Web应用内的资源
3.在转发过程中,可以将数据保存到request域对象当中去
4.转发只有一次请求
5.转发是服务器端行为
转发的过程:
1.客户端浏览器发送http
2.web浏览器接收请求
3.调用内部的一个方法在容器内部完成请求处理和转发工作
需注意的是:转发的路径必须是同一个web容器下的url。在客户端浏览器路径栏显示的仍然是第一次访问的路径。转发行为是浏览器只做了一次访问请求。
2.重定向(客户端行为)
resp.sendRedirect(" ")
重定向的特点:
1.重定向地址栏会改变
2.重定向可以跳转到当前web应用,甚至是外部域名的网站。
3.不能在重定向的过程中,将数据保存到request域对象中。
重定向的过程:
1.客户端浏览器发送http请求
2.web服务器接收后,发送302状态码响应,并且发送location给客户端服务器。
3.客户端服务器发现是302响应后,则自动发送一个http请求,请求url为重定向的地址,响应的服务器根据此请求寻找资源并发送给客户。
3.两者区别:
(1)请求转发是一次请求一次响应,而重定向是两次请求两次响应;
RequestDispatcher.forward方法的调用者与被调用者之间共享相同的request对象和response对象,它们属于同一个访问请求和响应过程.而HttpServletResponse.sendRedirect方法调用者与被调用者使用各自的request对象和response对象,它们属于两个独立的访问请求和响应过程。
重定向到客户端/本服务端页面(自己重定向到自己,浏览器地址栏不变cookie域名不变sessionId不变)
(2)请求转发地址栏不会改变,而重定向地址栏会显示第二次请求的地址;
(3)请求转发只能转发给本项目的其他资源,而重定向不仅可以重定向到本项目的其他资源, 还可以重定向到其他项目;
(4)请求转发是服务器端的行为,转发时只需要给出转发的资源路径即可,如Servlet的访问路径;而重定向需要给出全路径,即路径要包含项目名;
因为在springmvc的controller中return "redirect:/"重定向到自己时,底层已经帮我们追加了context-path。
而response.sendRedirect("/hello/login");在重定向到自己的登录控制器时,需要我们加上server.servlet.context-path=/hello配置的/hello,
response.sendRedirect方法不是mvc封装的。
(5)请求转发比重定向的效率高,因为请求转发是一个请求。在以后的开发中,如果需要地址栏的地址发生改变,就选择重定向;如果需要在Servlet之间通过request域进行数据通信,就选择请求转发。
4.转发示例:
//servlet的转发
request.getRequestDispatcher("/ResultServlet").forward(request, response);
//jsp页面的转发
request.getRequestDispatcher("index.jsp").forward(request, response);
5.重定向示例:
servlet和jsp页面的重定向:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { request.setCharacterEncoding("UTF-8"); String studentname = request.getParameter("studentname"); String studentpwd = request.getParameter("studentpwd"); // 拿Service接口 new Service实现 StudentService studentservice = new StudentServiceImpl(); Student student = studentservice.loginStudent(studentname, studentpwd); if (student != null) { // 将登录对象存入session,方便以后使用。 request.getSession().setAttribute("student", student); //由于登录成功去首页需要再携带成绩数据,需要 //先【重定向】到另一个Servlet(IndexInfoServlet)[习惯称之为:初始化首页页面]获取到成绩数据, //再通过其(IndexInfoServlet)【转发《session中存入的"student"》《request中存入的"resultList"》】到首页index.jsp response.sendRedirect("IndexInfoServlet"); /* * //登录成功 跳转首页 (用于验证是否能成功传递登录对象到index.jsp) * request.getRequestDispatcher("index.jsp").forward(request,response); */ } else { // 登录失败: response.sendRedirect("login.jsp"); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
6.request.getScheme()、getServerName()、getServerPort()、getContextPath()
1、request.getScheme() 返回当前链接使用的协议,一般返回http,ssl返回https;
2、在程序中的应用如下:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
<base href="<%=basePath%>" target="_blank">
7.base标签定义及使用说明
<base> 标签为页面上的所有的相对链接规定默认 URL 或默认目标。
在一个文档中,最多能使用一个 <base> 元素。<base> 标签必须位于 <head> 元素内部。
提示:请把 <base> 标签排在 <head> 元素中第一个元素的位置,这样 head 区域中其他元素就可以使用 <base> 元素中的信息了。
注释:如果使用了 <base> 标签,则必须具备 href 属性或者target 属性或者两个属性都具备。
href | URL | 规定页面中所有相对链接的基准 URL。 |
target | _blank _parent _self _top framename | 规定页面中所有的超链接和表单在何处打开。该属性会被每个链接中的 target 属性覆盖。 |
7.1如果是jsp页面一般借助el表达式,这样定义:
7.2如果是html页面一般借助thymeleaf就可以,不需base标签:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>home页</title>
</head>
<body>
<h3>我是home页,不需要登录就可以看到我哦</h3>
<a href="" id="a">去detail页必须登录</a>
<a th:href="@{/order.html}" id="b">去order页不须登录</a><!--thymeleaf的@{}语法会自动加上根路径-->
</body>
<script type="text/javascript" th:inline="javascript">
//js获取工程根路径
ctxPath = /*[[@{/}]]*/ '';
document.querySelector('#a').href = ctxPath + 'detail.html';
//document.querySelector('#b').href = ctxPath + 'order.html'
// 1.
var ctxPath = /*[[@{/}]]*/'';
console.log(ctxPath); // 结果: /portalUrl/
// 2.
var ctxPath = [[${#servletContext.contextPath}]];
console.log(ctxPath); // 结果: /portalUrl
// 3.
var ctxPath = [[${#request.getContextPath()}]];
console.log(ctxPath); // 结果: /portalUrl
// 4.
var ctxPath = [[${#httpServletRequest.getContextPath()}]];
console.log(ctxPath); // 结果: /portalUrl
</script>
</html>