想想學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="登入&