Use accessors to read data from JavaBean
01
Package com. javaweb. ch08;
02
Import java. util .*;
03
// A simple JavaBean example
04
Public class Person {
05
// Name attribute
06
Private String name;
07
// Age attributes
08
Private int age;
09
// Sex attributes
10
Private String sex;
11
// Friends attributes
12
Private ArrayList friends;
13
// Construction method without Parameters
14
Public Person (){
15
}
16
// Custom Constructor
17
Public Person (String name, int age, String sex ){
18
This. name = name;
19
This. age = age;
20
This. sex = sex;
21
}
22
// Obtain the attribute value of name
23
Public String getName (){
24
Return name;
25
}
26
// Get the age property value
27
Public int getAge (){
28
Return age;
29
}
30
// Obtain the sex property value
31
Public String getSex (){
32
Return sex;
33
}
34
// Set the name attribute
35
Public void setName (String name ){
36
This. name = name;
37
}
38
// Set the age Attribute Value
39
Public void setAge (int age ){
40
This. age = age;
41
}
42
// Set the sex property value
43
Public void setSex (String sex ){
44
This. sex = sex;
45
}
46
// Set the value of friends
47
Public void setFriends (ArrayList friends ){
48
This. friends = friends;
49
}
50
// Obtain the attribute value of 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> set attributes in JavaBean </title>
07
</Head>
08
<Body>
09
<%
10
// Instantiate a Person
11
Person person = new Person ("zhangdapeng", 24, "male ");
12
// Create a friends instance
13
ArrayList friends = new ArrayList ();
14
// Add Value
15
Friends. add ("wang wu ");
16
Friends. add ("li si ");
17
Person. setFriends (friends );
18
// Stored in the session range
19
Session. setAttribute ("person", person );
20
21
%>
22
<A href = "GetJavaBean. jsp"> jump to 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> obtain the attribute value in JavaBean </title>
06
</Head>
07
<Body>
08
Name: $ {sessionScope. person. name} <br/>
09
Age: $ {sessionScope. person. age} <br/>
10
Gender: $ {sessionScope. person. sex} <br/>
11
Friend: $ {sessionScope. person. friends [0]}, $ {sessionScope. person. friends [1]} <br/>
12
</Body>
13
</Html>
Use memory to read data in 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> set the Map page </title>
07
</Head>
08
<Body>
09
<%
10
// Create a HashMap
11
HashMap userInfo = new HashMap ();
12
// Set the value in HashMap
13
UserInfo. put ("username", "zhangdapeng ");
14
UserInfo. put ("password", "zhnagda123 ");
15
// Store the value in the session range
16
Session. setAttribute ("userInfo", userInfo );
17
18
%>
19
<A href = "GetMapDemo. jsp"> jump to 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> use memory to read data in map </title>
06
</Head>
07
<Body>
08
Username: $ {sessionScope. userInfo. username} <br/>
09
Password: $ {sessionScope. userInfo. password} <br/>
10
</Body>
11
</Html>
Use memory to read data in the array
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> set the Array page </title>
07
</Head>
08
<Body>
09
<%
10
String [] names = {"zhangda1", "zhangda2", "zhangda3 "};
11
// Store the value in the session range
12
Session. setAttribute ("names", names );
13
14
%>
15
<A href = "GetMapDemo. jsp"> jump to 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> use memory to read data in map </title>
06
</Head>
07
<Body>
08
Username 1: $ {sessionScope. names [0]} <br/>
09
Username 2: $ {sessionScope. names [1]} <br/>
10
</Body>
11
</Html>
Complex applications of memory
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> set the Array page </title>
07
</Head>
08
<Body>
09
<%
10
ArrayList <Person> persons = new ArrayList <Person> ();
11
12
Person person1 = new Person ("wang wu", 24, "male ");
13
Person person2 = new Person ("wang liu", 24, "female ");
14
15
Persons. add (person1 );
16
Persons. add (person2 );
17
18
Session. setAttribute ("persons", persons );
19
20
%>
21
<A href = "GetMapDemo. jsp"> jump to 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> use memory to read data in map </title>
06
</Head>
07
<Body>
08
Username 1: $ {sessionScope. persons [0]. name}, $ {sessionScope. persons [0]. age}, $ {sessionScope. persons [0]. sex} <br/>
09
Username 2: $ {sessionScope. persons [1]. name}, $ {sessionScope. persons [1]. age}, $ {sessionScope. persons [1]. sex} <br/>
10
</Body>
11
</Html>
From Zhang Dapeng's blog