定向转发的特点:

          (1). 实行转发时浏览器上的网址不变  (如果你这点忽视了,那你就要接受我无尽的鄙视吧! 哇咔咔~~~)

             (2). 实行转发时 :   只有一次请求。  不信,看这下面的图:   (俗话说,没图说个jb)

           (3).  定向转发的网址必须是本站点的网址.    (因为它不消除,消除数据)

          (4)  定向转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。

实现的两个API:        

 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;
}
}

显示的效果:

定向转发和重定向实现 <select >下拉表单数据传送的更多相关文章

  1. css解决select下拉表单option高度的办法

    css在给select下拉表单设置样式如边框时可以轻松搞定,而我们在不喜欢其默认的下拉箭头的样式时试图通过background:url(图片路径)来修改之,则往往会出现浏览器的兼容性问题,在网上查了好 ...

  2. ajax处理select下拉表单

    $('#gameid').change(function() { var gameid = $(this).val(); if (this.value != '') { $.ajax({ url: ' ...

  3. s:select下拉框validation验证

    S:select下拉框验证: <td colspan="5"> <s:select name="vo.typeVO.corp" list=&q ...

  4. 自定义样式的select下拉框深入探索

    第一个版本: 首先实现自定义select下拉框应该具有的功能,我是选择将原来的select隐藏掉,自己在jquery代码中动态写进去<dl><dd><dt>这样的结 ...

  5. jquery取消选择select下拉框

    有三个select下拉框一个大类,两个小类隐藏,需要在选择大类的时候,小类显示同时清除另外的小类选择的项这需求有点儿.......... 下面是三个select: <select name=&q ...

  6. jQuery插件:模拟select下拉菜单

    没搞那么复杂,工作中,基本够用.. <!doctype html> <html> <head> <meta charset="utf-8" ...

  7. html select 下拉箭头隐藏

    html select 下拉箭头隐藏 <!DOCTYPE html> <html> <head lang="en"> <meta char ...

  8. jquery选中将select下拉框中一项后赋值给text文本框

    jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...

  9. js如何获取select下拉框的value以及文本内容

    select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的.其他的就不说了,现 ...

随机推荐

  1. N年后给自己一些忠诚的建议

    给自己S年后的一封信: 也许,现在的自己已经经历了种种历练,或成为干将,许是拔杆而起的创业者,再者仍然是一名奋斗中的工薪族.无论现在如何,请记得: M年前,自己坐在小房子里写下的信件. 那时候,自己是 ...

  2. c++ 动态分配二维数组 new 二维数组

    #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _T ...

  3. linux phpstorm安装和激活方法

    安装方法:http://www.linuxdiyf.com/linux/19328.html 激活方法:http://www.cnblogs.com/buyucoder/p/5291771.html ...

  4. Create Hierarchical Tree To Control Records In Oracle Forms

    Download Source Code Providing an example form for creating hierarchical trees in Oracle Forms to co ...

  5. Berkeley DB的常见API简单分析

    1.用来存储类信息的数据库不要求能够存储重复的关键字    例:    dbConfig.setSortedDuplicates(false);2.DatabaseEntrt能够支持任何能够转化为by ...

  6. 《Linux内核设计的艺术》学习笔记(一)从开机加电到加载三个汇编源码

      实验内核版本:0.11 ◆ 从开机到main函数的三步: ① 启动BIOS,准备实模式下的中断向量表和中断服务程序: ② 从启动盘加载OS程序到内存中,加载OS程序的工作就是利用第一步中的中断服务 ...

  7. iOS - AVPlayer 音视频播放

    前言 NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject @available(iOS 4.0, *) public class ...

  8. 省市县distpicker的使用

    下载地址https://github.com/fengyuanchen/distpicker 1.引入 <!-- 引入地址 begin --> <script type=" ...

  9. Python学习笔记11—函数

    建立第一个函数 /usr/bin/env Python #coding:utf-8 def add_function(a,b): c = a+b print c if __name__==" ...

  10. uva 11800 Determine the Shape

    vjudge上题目链接:Determine the Shape 第二道独自 A 出的计算几何水题,题意就是给你四个点,让你判断它是哪种四边形:正方形.矩形.菱形.平行四边形.梯形 or 普通四边形. ...