定向转发和重定向实现 <select >下拉表单数据传送
2014-12-12 12:10
417 查看
定向转发的特点:
(1). 实行转发时浏览器上的网址不变 (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~)
(2). 实行转发时 : 只有一次请求。 不信,看这下面的图: (俗话说,没图说个jb)
定向转发
关于定向转发实现selected选项功能:
上面这个控件的代码:
JSP代码:
效果图:
关于: 然后需要一个Servlet 类: (即纯java文件)
一个 javabean文件:
重定向的特点:
(1)执行重定向时浏览器上的网址改变.
(2)重定向实际上产生了两次请求 (看下面的图)
(3)执行重定向时 的网址可以是任何网址。
调用的 API 函数:
response.sendRedirect("Demo_1/Login.jsp? username="+username+"&authority="+authority);
对于定向转发和重定向的实际执行情况,可以简单的慨括为:
对于重定向:
发送请求 -->服务器运行-->响应请求,返回给浏览器一个新的地址与响应码-->浏览器根据响应码,判定该响应为重定向,自动发送一个新的请求给服务器,请求地址为之前返回的地址-->服务器运行-->响应请求给浏览器
对于定向的转发:
发送请求 -->服务器运行-->进行请求的重新设置,例如通过request.setAttribute(name,value)-->根据转发的地址,获取该地址的网页-->响应请求给浏览器
特别需要注意的是:
重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域。
转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。
然后运用重定向实现<select> 下拉列表的代码:
对于JSP:
对于Servlet类:
对于JavaBean类:
显示的效果:
(1). 实行转发时浏览器上的网址不变 (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~)
(2). 实行转发时 : 只有一次请求。 不信,看这下面的图: (俗话说,没图说个jb)
RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); rd.forward(request, response);
定向转发
关于定向转发实现selected选项功能:
上面这个控件的代码:
JSP代码:
<%@ page language="java" import="java.util.*" pageEncoding="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>注册页面</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> <% String authority = (String) request.getAttribute("authority"); %> <form action="LoginServlet" method="post" > username :<input type="text" name="username" value="<%=null==request.getAttribute("username")?"":request.getAttribute("username")%>"><br> password :<input type="password" name="password"><br> authority: <select name="authority"> <option value="1"<%="1".equals(authority)?"selected":""%>>common user</option> <option value="2"<%="2".equals(authority)?"selected='selected'":""%>>administrator</option> </select><br> <input type="submit" value="submit"> </form> </body> </html>
效果图:
关于: 然后需要一个Servlet 类: (即纯java文件)
package Demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password =request.getParameter("password"); String authority =request.getParameter("authority"); Login log = new Login(); HttpSession session = request.getSession(); if("1".equals(authority)) { // 登录的是普通用户 if("zhangsan".equals(username) && "123".equals(password)) { // 将用户的信息放置到Session当中 log.setUsername(username); log.setAuthority(authority); log.setPassword(password); session.setAttribute("log", log); }else { //定向转发 request.setAttribute("username",username); request.setAttribute("password", password); request.setAttribute("authority",authority); RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); rd.forward(request, response); } } else if("2".equals(authority)){ // 登录的是系统管理员 if("Tom".equals(username) && "456".equals(password)) { log.setAuthority(authority); log.setPassword(password); log.setUsername(username); session.setAttribute("log",log); } else { // 采取的是定向转发 request.setAttribute("username",username); request.setAttribute("password", password); request.setAttribute("authority",authority); RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); rd.forward(request, response); } } else { request.setAttribute("username",username); request.setAttribute("password", password); request.setAttribute("authority",authority); RequestDispatcher rd = request.getRequestDispatcher("Demo_1/Login.jsp"); rd.forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
一个 javabean文件:
package Demo; //设置一个JavaBean 类 public class Login { private String username ; private String password ; private String authority ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAuthority() { return authority; } public void setAuthority(String authority) { this.authority = authority; } }
重定向的特点:
(1)执行重定向时浏览器上的网址改变.
(2)重定向实际上产生了两次请求 (看下面的图)
(3)执行重定向时 的网址可以是任何网址。
调用的 API 函数:
response.sendRedirect("Demo_1/Login.jsp? username="+username+"&authority="+authority);
对于定向转发和重定向的实际执行情况,可以简单的慨括为:
对于重定向:
发送请求 -->服务器运行-->响应请求,返回给浏览器一个新的地址与响应码-->浏览器根据响应码,判定该响应为重定向,自动发送一个新的请求给服务器,请求地址为之前返回的地址-->服务器运行-->响应请求给浏览器
对于定向的转发:
发送请求 -->服务器运行-->进行请求的重新设置,例如通过request.setAttribute(name,value)-->根据转发的地址,获取该地址的网页-->响应请求给浏览器
特别需要注意的是:
重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域。
转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。
然后运用重定向实现<select> 下拉列表的代码:
对于JSP:
<%@ page language="java" import="java.util.*" pageEncoding="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>注册页面</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> <% String username = request.getParameter("username"); String authority = request.getParameter("authority"); %> <form action="LoginServlet" method="post" > username :<input type="text" name="username" <%= null == username ? "":username %> ><br> password :<input type="password" name="password"><br> authority: <select name="authority"> <option value="1" <%= "1" == authority ?"":"selected"%> >common user</option> <option value="2" <%= "2" == authority ?"":"selected"%> >administrator</option> </select><br> <input type="submit" value="submit"> </form> </body> </html>
对于Servlet类:
package Demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password =request.getParameter("password"); String authority =request.getParameter("authority"); Login log = new Login(); HttpSession session = request.getSession(); if("1".equals(authority)) { // 登录的是普通用户 if("zhangsan".equals(username) && "123".equals(password)) { // 将用户的信息放置到Session当中 log.setUsername(username); log.setAuthority(authority); log.setPassword(password); session.setAttribute("log", log); }else { //执行重定向函数 response.sendRedirect("Demo_1/Login.jsp? username="+username+"&authority="+authority); } } else if("2".equals(authority)){ // 登录的是系统管理员 if("Tom".equals(username) && "456".equals(password)) { log.setAuthority(authority); log.setPassword(password); log.setUsername(username); session.setAttribute("log",log); } else { // 采取的是重定向 response.sendRedirect("Demo_1/Login.jsp?username="+username+"&authority="+authority); } } else { // 采取的是重定向 response.sendRedirect("Demo_1/Login.jsp?username="+username+"&authority="+authority); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
对于JavaBean类:
package Demo; //设置一个JavaBean 类 public class Login { private String username ; private String password ; private String authority ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAuthority() { return authority; } public void setAuthority(String authority) { this.authority = authority; } }
显示的效果:
相关文章推荐
- servlet获取表单提交的数据、请求转发、请求包含、请求重定向
- 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。
- <fieldset> 标签(实现表单分组以及特殊效果)
- <java><JTable>使用AbstractTableModel实现更新、删除、插入数据
- asp.net页面间传值的几种方法 表单提交 传送页面代码 复制代码 <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server
- 【JavaWeb-6】HttpServletResponse的字符字节输出流、编码、文件下载、Captcha图片与HttpServletRequest获取request数据表单非表单数据、重定向与转发
- <下拉框实现,二> - 屏幕的I/O控件或者Table Control的单元格通过下拉框赋值
- Struts2中表单数据怎样实例化 <input name=user.name>struts实现拿值方法
- 纯javascipt实现表单<input>数据加减 checbox里面存 数量和价格 ID等信息信息
- HttpServletResponse的字符字节输出流、编码、文件下载、Captcha图片与HttpServletRequest获取request数据表单非表单数据、重定向与转发
- jquery实现多表单数据传送
- jsp实现表单异步验证+传送接收数据
- Socket实现两台手机通信,定向转发数据
- <php+mysql>PHP基础,使用PHP访问表单数据
- android ContentProvider结合SQLiteHelper实现数据的共享<一>
- 实现Form表单域中Post大于100K的数据
- 利用userData实现客户端保存表单数据
- 如何用.net实现两台机子之间的数据间的传送?
- 在一个jsp页面实现二级下拉框联动,实时读取数据库数据
- java脚本实现下拉框和文本框的数据交换