定向转发的特点:
(1). 实行转发时浏览器上的网址不变 (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~)
(2). 实行转发时 : 只有一次请求。 不信,看这下面的图: (俗话说,没图说个jb)
(3). 定向转发的网址必须是本站点的网址. (因为它不消除,消除数据)
(4) 定向转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。
实现的两个API:
1 RequestDispatcher rd 2 = request.getRequestDispatcher("Demo_1/Login.jsp"); 3 4 rd.forward(request, response);
关于定向转发实现selected选项功能:
上面这个控件的代码:
JSP代码:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <title>注册页面</title> 12 <meta http-equiv="pragma" content="no-cache"> 13 <meta http-equiv="cache-control" content="no-cache"> 14 <meta http-equiv="expires" content="0"> 15 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 16 <meta http-equiv="description" content="This is my page"> 17 <!-- 18 <link rel="stylesheet" type="text/css" href="styles.css"> 19 --> 20 </head> 21 22 <body> 23 <% String authority = (String) request.getAttribute("authority"); %> 24 <form action="LoginServlet" method="post" > 25 username :<input type="text" name="username" value="<%=null==request.getAttribute("username")?"":request.getAttribute("username")%>"><br> 26 password :<input type="password" name="password"><br> 27 28 authority: 29 <select name="authority"> 30 <option value="1"<%="1".equals(authority)?"selected":""%>>common user</option> 31 32 <option value="2"<%="2".equals(authority)?"selected='selected'":""%>>administrator</option> 33 34 </select><br> 35 36 <input type="submit" value="submit"> 37 38 </form> 39 </body> 40 </html>
效果图:
关于: 然后需要一个Servlet 类: (即纯java文件)
1 package Demo; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 public class LoginServlet extends HttpServlet { 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 String username = request.getParameter("username"); 18 String password =request.getParameter("password"); 19 String authority =request.getParameter("authority"); 20 21 Login log = new Login(); 22 HttpSession session = request.getSession(); 23 if("1".equals(authority)) 24 { 25 // 登录的是普通用户 26 if("zhangsan".equals(username) && "123".equals(password)) 27 { 28 // 将用户的信息放置到Session当中 29 30 log.setUsername(username); 31 log.setAuthority(authority); 32 log.setPassword(password); 33 session.setAttribute("log", log); 34 }else { 35 36 //定向转发 37 request.setAttribute("username",username); 38 request.setAttribute("password", password); 39 request.setAttribute("authority",authority); 40 RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); 41 rd.forward(request, response); 42 } 43 } 44 else if("2".equals(authority)){ 45 // 登录的是系统管理员 46 if("Tom".equals(username) && "456".equals(password)) 47 { 48 log.setAuthority(authority); 49 log.setPassword(password); 50 log.setUsername(username); 51 session.setAttribute("log",log); 52 } 53 else { 54 55 // 采取的是定向转发 56 57 request.setAttribute("username",username); 58 request.setAttribute("password", password); 59 request.setAttribute("authority",authority); 60 RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); 61 rd.forward(request, response); 62 } 63 } else { 64 65 request.setAttribute("username",username); 66 request.setAttribute("password", password); 67 request.setAttribute("authority",authority); 68 RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); 69 rd.forward(request, response); 70 71 } 72 73 } 74 75 public void doPost(HttpServletRequest request, HttpServletResponse response) 76 throws ServletException, IOException { 77 this.doGet(request, response); 78 } 79 }
一个 javabean文件:
1 package Demo; 2 3 //设置一个JavaBean 类 4 5 public class Login { 6 7 private String username ; 8 private String password ; 9 private String authority ; 10 11 public String getUsername() { 12 return username; 13 } 14 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 19 public String getPassword() { 20 return password; 21 } 22 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 27 public String getAuthority() { 28 return authority; 29 } 30 31 public void setAuthority(String authority) { 32 this.authority = authority; 33 } 34 }
重定向的特点:
(1)执行重定向时浏览器上的网址改变.
(2)重定向实际上产生了两次请求 (看下面的图)
(3)执行重定向时 的网址可以是任何网址。
调用的 API 函数:
1 response.sendRedirect("Demo_1/Login.jsp? username="+username+"&authority="+authority);
对于定向转发和重定向的实际执行情况,可以简单的慨括为:
对于重定向:
发送请求 -->服务器运行-->响应请求,返回给浏览器一个新的地址与响应码-->浏览器根据响应码,判定该响应为重定向,自动发送一个新的请求给服务器,请求地址为之前返回的地址-->服务器运行-->响应请求给浏览器
对于定向的转发:
发送请求 -->服务器运行-->进行请求的重新设置,例如通过request.setAttribute(name,value)-->根据转发的地址,获取该地址的网页-->响应请求给浏览器
特别需要注意的是:
重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域。
转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。
然后运用重定向实现<select> 下拉列表的代码:
对于JSP:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <title>注册页面</title> 12 <meta http-equiv="pragma" content="no-cache"> 13 <meta http-equiv="cache-control" content="no-cache"> 14 <meta http-equiv="expires" content="0"> 15 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 16 <meta http-equiv="description" content="This is my page"> 17 <!-- 18 <link rel="stylesheet" type="text/css" href="styles.css"> 19 --> 20 </head> 21 22 <body> 23 <% 24 String username = request.getParameter("username"); 25 String authority = request.getParameter("authority"); 26 %> 27 <form action="LoginServlet" method="post" > 28 username :<input type="text" name="username" <%= null == username ? "":username %> ><br> 29 password :<input type="password" name="password"><br> 30 31 authority: 32 <select name="authority"> 33 <option value="1" <%= "1" == authority ?"":"selected"%> >common user</option> 34 35 <option value="2" <%= "2" == authority ?"":"selected"%> >administrator</option> 36 37 </select><br> 38 39 <input type="submit" value="submit"> 40 41 </form> 42 </body> 43 </html>
对于Servlet类:
1 package Demo; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 public class LoginServlet extends HttpServlet { 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 String username = request.getParameter("username"); 18 String password =request.getParameter("password"); 19 String authority =request.getParameter("authority"); 20 21 Login log = new Login(); 22 HttpSession session = request.getSession(); 23 if("1".equals(authority)) 24 { 25 // 登录的是普通用户 26 if("zhangsan".equals(username) && "123".equals(password)) 27 { 28 // 将用户的信息放置到Session当中 29 30 log.setUsername(username); 31 log.setAuthority(authority); 32 log.setPassword(password); 33 session.setAttribute("log", log); 34 }else { 35 //执行重定向函数 36 response.sendRedirect("Demo_1/Login.jsp? username="+username+"&authority="+authority); 37 } 38 } 39 else if("2".equals(authority)){ 40 // 登录的是系统管理员 41 if("Tom".equals(username) && "456".equals(password)) 42 { 43 log.setAuthority(authority); 44 log.setPassword(password); 45 log.setUsername(username); 46 session.setAttribute("log",log); 47 } 48 else { 49 // 采取的是重定向 50 response.sendRedirect("Demo_1/Login.jsp?username="+username+"&authority="+authority); 51 } 52 } else { 53 54 // 采取的是重定向 55 response.sendRedirect("Demo_1/Login.jsp?username="+username+"&authority="+authority); 56 } 57 58 } 59 60 public void doPost(HttpServletRequest request, HttpServletResponse response) 61 throws ServletException, IOException { 62 this.doGet(request, response); 63 } 64 }
对于JavaBean类:
1 package Demo; 2 3 //设置一个JavaBean 类 4 5 public class Login { 6 7 private String username ; 8 private String password ; 9 private String authority ; 10 11 public String getUsername() { 12 return username; 13 } 14 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 19 public String getPassword() { 20 return password; 21 } 22 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 27 public String getAuthority() { 28 return authority; 29 } 30 31 public void setAuthority(String authority) { 32 this.authority = authority; 33 } 34 }
显示的效果: