1.请求重定向:是客户端的行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保存,地址栏的URL地址会改变,一次新的转发。
2.请求转发:是服务器的行为,request.getRequestDispatcher(.jsp).forward(request,response)试一次请求,转发后请求对象会保存,地址栏的URL地址不会改变,还是上一次请求。
理解:
例子:
例如一个表单login.jsp,把表单的内容提交给response.jsp,在response.jsp里面做一次请求重定向和请求转发给request.jsp
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>用户注册</h1> <hr> <form action="response.jsp" name="regForm" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" /> </td> </tr> <tr> <td>爱好:</td> <td> <input type="checkbox" name="favorite" value="read" />读书 <input type="checkbox" name="favorite" value="music" />音乐 <input type="checkbox" name="favorite" value="movie" />电影 <input type="checkbox" name="favorite" value="internet" />上网 </td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form> <a href = "request.jsp?username=张三" >URL传参</a> </body> </html>
reponse.jsp
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8" %> <% response.setContentType("text/html;charaset=utf-8");//设置响应的MIME类型 out.println("<h1>response内置对象</h1>"); //out.flush(); PrintWriter outer = response.getWriter(); outer.println("response的输出流"); //请求重定向 //response.sendRedirect("request.jsp"); //请求转发 request.getRequestDispatcher("request.jsp").forward(request,response); %>
request.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>request内置对象</h1> <% request.setCharacterEncoding("utf-8"); %> <% request.setAttribute("password","123465");%> 用户名<%=request.getParameter("username") %><br> 爱好<% if(request.getParameterValues("favorite")!=null){ String[] favorites = request.getParameterValues("favorite"); for(int i = 0;i<favorites.length;i++){ out.println(favorites[i]+"  "); } } %> <hr> <%=request.getAttribute("password")%><br> 获取请求体的MIME类型:<%=request.getContentType()%><br> 返回请求用的协议类型以及版本号: <%=request.getProtocol()%><br> 返回接受请求的服务器主机名: <%=request.getServerName()%><br> 服务器端口号 :<%=request.getServerPort()%><br> 返回的编码格式 :<%=request.getCharacterEncoding()%><br> 请求文件的长度 :<%=request.getContentLength()%><br> 请求客户端的IP地址 <%=request.getRemoteAddr()%><br> 请求的真实路径: <%=request.getRealPath("request.jsp")%><br> 请求的上下文路径: <%=request.getContextPath()%><br> </body> </html>
结果,请求转发的
可以看到对象有被保存,并且URL没有改变
请求重定向
可以看到对象没有被保存,并且URL发生了改变