Javaweb開發中不同JSP頁面之間的七大傳參方式__html5

來源:互聯網
上載者:User

想想學javaweb已經有了一些時日了,現在總結一下JSP頁面之間的傳參方法 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="登入&
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.