您的位置:首页 > 其它

定向转发和重定向实现 <select >下拉表单数据传送

2014-12-12 12:10 417 查看
定向转发的特点:

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


显示的效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐