1. 程式人生 > >web開發jsp之間頁面傳遞引數的7種方式

web開發jsp之間頁面傳遞引數的7種方式



1.利用javabean

Javabean類:

package entity;

public class User {
    private String username="";
    private String gender="";
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getGender
() { return gender; } public void setGender(String gender) { this.gender = gender; } public User() { } }

傳引數的頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>引數傳遞頁</title> </head> <body> <jsp:useBean id="user" class="entity.User" scope="session" /> <center> <h1>傳參頁面</h1> </center> <hr
>
<% user.setUsername("紳士"); user.setGender("男"); %> <center> 點選我,<a href="receive.jsp">跳轉</a> </center> </body> </html>

接收引數的頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
        <hr>
        <jsp:useBean id="user" class="entity.User" scope="session"></jsp:useBean>
        <p>使用JSP動作接收傳參</p>
        <h4>
            性別:<jsp:getProperty name="user" property="username" /><br> 密碼:<jsp:getProperty
                name="user" property="gender" /><br>
        </h4>
        <hr>
        <p>使用JSP普通方式接收引數</p>
        <h4>
            性別:<%=user.getUsername()%><br> 密碼:<%=user.getGender()%><br>
        </h4>
    </center>
</body>
</html>

2.繫結到session物件

傳參頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    </center>
    <hr>
    <%
    session.setAttribute("username", "紳士");
    session.setAttribute("gender", "男");

    %>
    <center>
         <a href="receive.jsp">傳遞引數</a>  
    </center>
</body>
</html>
 
 
接收引數頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%  

            out.print("姓名:"+session.getAttribute("username"));  
        %>  
        <br/>  
        <%  
            out.print("性別:"+session.getAttribute("gender"));  
        %>  
    </center>
</body>
</html>

3.繫結到application

傳參頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    </center>
    <hr>
    <%
    application.setAttribute("username", "紳士");
    application.setAttribute("gender", "男");

    %>
    <center>
         <a href="receive.jsp">傳遞引數</a>  
    </center>
</body>
</html>

接收引數頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%  

            out.print("姓名:"+application.getAttribute("username"));  
        %>  
        <br/>  
        <%  
            out.print("性別:"+application.getAttribute("gender"));  
        %>  
    </center>
</body>
</html>

4.繫結到request物件

這裡用採用的是請求轉發的方式 傳參頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    </center>
    <hr>

        <%  
            request.setAttribute("name","紳士");  
        %>  
        <jsp:forward page="receive.jsp"/>  

</body>
</html>

接收引數頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%  
            out.println("傳遞過來的引數是:"+request.getAttribute("name"));  
        %>  
    </center>
</body>
</html>

5.使用JSP動作指令傳參

傳參頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    </center>
    <%
    String username="紳士";
    String gender="男";
    %>
    <hr>
    <jsp:forward page="receive.jsp">
        <jsp:param name="name" value="Jakc"  />
        <jsp:param name="gender" value="man" />
    </jsp:forward>
</body>
</html>

接收引數頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%
        request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            out.print("姓名:" + name);
        %>
        <br />
        <%
            out.print("性別:" + request.getParameter("gender"));
        %>
    </center>
</body>
</html>

6. 表單傳參

傳參頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    </center>
    <form action="receive.jsp" method="get" align="center">
        姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br />

        密碼:<input type="password" name="password" size="20" value=""
            maxlength="20"><br /> <br /> <input type="submit"
            name="submit" value="登入"> <input type="reset" name="reset"
            value="重置"><br />
    </form>
</body>
</html>

接收引數頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%
            request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            out.print("姓名:" + name);
        %>
        <br />
        <%
            out.print("性別:" + request.getParameter("password"));
        %>
    </center>
</body>
</html>

7.URL傳參

傳參頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    </center>
    <hr>
    <%!

    String username="紳士";
    String gender="男";

    %>
    <center>
         <a href="receive.jsp?username=<%=username %>&gender=<%=gender%>">傳遞引數</a>  
    </center>
</body>
</html>

接收引數頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%  
            String name=request.getParameter("username");  

            out.print("姓名:"+name);  
        %>  
        <br/>  
        <%  
            out.print("性別:"+request.getParameter("gender"));  
        %>  
    </center>
</body>
</html>

URL傳參的時候還有一個特別的用處,是當你在JSP頁面中使用for迴圈輸出某些資訊的時候,URL傳參的獨特之處就體現出來了 
 假設我使用for迴圈5次,每次瀏覽器輸出點選我跳轉,並同時向session繫結引數(方便起見就用迴圈變數吧)
像這樣:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    <%
        for (int i = 0; i < 5; i++) {
            session.setAttribute("index", i);
    %>
    <a href="receive.jsp">點選我跳轉 </a><hr>


    <%
        }
    %>
    </center>
</body>
</html>

接收頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>

        接收到的順序:<%=session.getAttribute("index") %>

    </center>
</body>
</html>
沒測試之前誤以為點選不同的連線會傳遞不同的引數,但是事實上卻是每一次穿過來的引數都是4,這就很奇怪!

 

為什麼是這樣呢? 這裡博主做出解釋:因為session要等到輸出所有的內容,也就是說等到遞增到4之後,才將最後一個i的值傳給下一個頁面(也就是說,i=0,時候,session繫結的物件的值是0,i=1的時候,繫結的物件的值是1,…….) 但是呢要等到i遞增到4之後整個JSP頁面輸出完畢之後才傳到下一個JSP頁面,所以每一次傳到去的引數就是4,接收到的引數自然每一次都是4了!

那我們要怎麼做才能做到當我點選不同的連線的時候傳遞引數是其對應的引數呢??(比如說我們點選第一個連結的時候,傳遞過去的引數是0;第二個引數的時候,引數是1….)

這個時候使用URL傳參就很好用了

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引數傳遞頁</title>
</head>
<body>
    <center>
        <h1>傳參頁面</h1>
    <%
        for (int i = 0; i < 5; i++) {
    %>
    <a href="receive.jsp?index=<%= i %>">點選我跳轉 </a><hr>


    <%
        }
    %>
    </center>
</body>
</html>
接收頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收引數頁</title>
</head>
<body>
    <center>
        <h1>接收引數頁面</h1>
        <hr>
        <%--接收引數的時候必須使用這個方法否則會找不到傳遞過來的引數 --%>
        使用request.getParameter接收到的引數 接收到的順序:<%=request.getParameter("index")%>
        <hr>
        使用request.getAttribute接收到的引數
        <%=request.getAttribute("index")%>
    </center>
</body>
</html>

這裡請注意了,一定要使用request.getParameter(string name)這個方法才能得到引數,否則是不能得到引數的!