jsp---EL存取器
来源:岁月联盟
时间:2012-02-05
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>
摘自 张大鹏的博客
上一篇:jsp---简单的计算器