一、创建一个JavaBean
UserBean.java
package jsp.test; public class UserBean { private String userName; private String pwd; private String name; private String gender; private int age; private String email; private String tel; private String mphone; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getMphone() { return mphone; } public void setMphone(String mphone) { this.mphone = mphone; } }
二、JavaBean的生命周期
---------1.page范围
counter.java
package bean; public class counter { public counter(){ } private int count=0; public int getCount() { return count; } public void setCount(int count) { count = count; } }
usingCounter.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>page 类型的生命周期</title> </head> <body> <jsp:useBean id="count" scope="page" class="bean.counter"/> <font color="blue">Show:page</font><br><br> <br>You are the <font color=green> <jsp:getProperty name="count" property="count"/></font>Viewer </body> </html>
使用page计数器的值永远保持为1,不更新。。
-----------2.request
setRequest.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>request type life time</title> </head> <body> <jsp:useBean id="count" scope="request" class="bean.counter"/> <font color=blue>area: request </font><br><br> You are the <font color=green> <jsp:getProperty property="count" name="count"/>s </font>viewer<br><br> <jsp:forward page="request.jsp"></jsp:forward> </body> </html>
requset.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>request type life time</title> </head> <body> <jsp:useBean id="count" scope="request" class="bean.counter"/> <font color=blue>request.jsp</font><br><br><br> You are the <font color=green> <jsp:getProperty name="count" property="count"/> </font>viewer<br> <% out.println("This is request.jsp page's counter......."); %> </body> </html>
计数器会加1。。
3------------session
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>session type life time</title> </head> <body> <jsp:useBean id="count" scope="session" class="bean.counter"></jsp:useBean> <font color=blue>area: session</font><br><br><br> You are the : <font color="green"> <jsp:getProperty property="count" name="count"/>viewer </font> </body> </html>
session每次浏览计数都会从头开始。。
4------------application
appication.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>application type life time</title> </head> <body> <jsp:useBean id="count" scope="application" class="bean.counter"></jsp:useBean> <font color=blue>area: session</font><br><br><br> You are the : <font color="green"> <jsp:getProperty property="count" name="count"/>viewer </font> </body> </html>
四个中生命周期最长的一个,计数器会不断累加,除非删除,与jsp引擎相当