使用存取器讀取JavaBean中的數據
01
package com.javaweb.ch08;
02
import java.util.*;
03
//一個簡單的JavaBean示例
04
public class Person{
05
//name屬性
06
private String name;
07
//age屬性
08
private int age;
09
//sex屬性
10
private String sex;
11
//friends屬性
12
private ArrayList friends;
13
//無參構造方法
14
public Person(){
15
}
16
//自定義構造的方法
17
public Person(String name,int age,String sex){
18
this.name = name;
19
this.age = age;
20
this.sex = sex;
21
}
22
//獲取name的屬性值
23
public String getName(){
24
return name;
25
}
26
//獲取age屬性值
27
public int getAge(){
28
return age;
29
}
30
//獲取sex的屬性值
31
public String getSex(){
32
return sex;
33
}
34
//設置name的屬性
35
public void setName(String name){
36
this.name = name;
37
}
38
//設置age的屬性值
39
public void setAge(int age){
40
this.age = age;
41
}
42
//設置sex的屬性值
43
public void setSex(String sex){
44
this.sex = sex;
45
}
46
//設置friends的值
47
public void setFriends(ArrayList friends){
48
this.friends = friends;
49
}
50
//獲取friends的屬性值
51
public ArrayList getFriends(){
52
return friends;
53
}
54
}
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.util.*,com.javaweb.ch08.*" %>
03
<!DOCTYPE html>
04
<html>
05
<head>
06
<title>設置JavaBean中的屬性</title>
07
</head>
08
<body>
09
<%
10
//實例化一個Person
11
Person person = new Person("zhangdapeng",24,"男");
12
//創建一個friends實例
13
ArrayList friends = new ArrayList();
14
//添加值
15
friends.add("wang wu");
16
friends.add("li si");
17
person.setFriends(friends);
18
//存儲在session范圍內
19
session.setAttribute("person",person);
20
21
%>
22
<a href="GetJavaBean.jsp">跳轉到GetJavaBean.jsp</a>
23
</body>
24
</html>
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<!DOCTYPE html>
03
<html>
04
<head>
05
<title>取得JavaBean中的屬性值</title>
06
</head>
07
<body>
08
姓名:${sessionScope.person.name}<br />
09
年齡:${sessionScope.person.age}<br />
10
性別:${sessionScope.person.sex}<br />
11
朋友:${sessionScope.person.friends[0]},${sessionScope.person.friends[1]}<br />
12
</body>
13
</html>
使用存儲器讀取Map中的數據
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.util.*,com.javaweb.ch08.*" %>
03
<!DOCTYPE html>
04
<html>
05
<head>
06
<title>設置Map頁面</title>
07
</head>
08
<body>
09
<%
10
//新建一個HashMap
11
HashMap userInfo = new HashMap();
12
//在HashMap中設置值
13
userInfo.put("username","zhangdapeng");
14
userInfo.put("password","zhnagda123");
15
//將值存儲到session范圍中
16
session.setAttribute("userInfo",userInfo);
17
18
%>
19
<a href="GetMapDemo.jsp">跳轉到GetMapDemo.jsp</a>
20
</body>
21
</html>
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<!DOCTYPE html>
03
<html>
04
<head>
05
<title>使用存儲器讀取map中的數據</title>
06
</head>
07
<body>
08
用戶名:${sessionScope.userInfo.username}<br />
09
密碼:${sessionScope.userInfo.password}<br />
10
</body>
11
</html>
使用存儲器讀取數組中的數據
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.util.*,com.javaweb.ch08.*" %>
03
<!DOCTYPE html>
04
<html>
05
<head>
06
<title>設置Array頁面</title>
07
</head>
08
<body>
09
<%
10
String[] names = {"zhangda1","zhangda2","zhangda3"};
11
//將值存儲到session范圍中
12
session.setAttribute("names",names);
13
14
%>
15
<a href="GetMapDemo.jsp">跳轉到GetArrayDemo.jsp</a>
16
</body>
17
</html>
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<!DOCTYPE html>
03
<html>
04
<head>
05
<title>使用存儲器讀取map中的數據</title>
06
</head>
07
<body>
08
用戶名1:${sessionScope.names[0]}<br />
09
用戶名2:${sessionScope.names[1]}<br />
10
</body>
11
</html>
存儲器的復雜應用
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.util.*,com.javaweb.ch08.*" %>
03
<!DOCTYPE html>
04
<html>
05
<head>
06
<title>設置Array頁面</title>
07
</head>
08
<body>
09
<%
10
ArrayList<Person> persons = new ArrayList<Person>();
11
12
Person person1 = new Person("wang wu",24,"男");
13
Person person2 = new Person("wang liu",24,"女");
14
15
persons.add(person1);
16
persons.add(person2);
17
18
session.setAttribute("persons",persons);
19
20
%>
21
<a href="GetMapDemo.jsp">跳轉到GetArrayDemo.jsp</a>
22
</body>
23
</html>
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<!DOCTYPE html>
03
<html>
04
<head>
05
<title>使用存儲器讀取map中的數據</title>
06
</head>
07
<body>
08
用戶名1:${sessionScope.persons[0].name},${sessionScope.persons[0].age},${sessionScope.persons[0].sex}<br />
09
用戶名2:${sessionScope.persons[1].name},${sessionScope.persons[1].age},${sessionScope.persons[1].sex}<br />
10
</body>
11
</html>
摘自 張大鵬的博客