回顾
将键值对存储到session的重要方法:
1.将对应的键-值对存入session 对象
void setAttribute(String name,Object value)
2.从session 对象中根据键返回值
Object getAttribute(String name)
将键-值(对象)对存储到session对象
java的数据类型分为简单类型和引用类型。简单类型又可以成为值类型、原始类型。如果“键值对”中的“值”不是简单类型而是引用类型。也即是不是简单的一个float、int、char类型的数据,而是希望一个类类型对应的数据(对象),那么将会大大增强开发者的开发体验,加大开发效率。这个类类型有可能是一个自定义的Student类,也有可能是一个集合类,如ArrayList<Student>.
所以setAttribute方法的运用可以是:
session.setAttribute(键名,Student对象的引用名);
session.setAttribute(键名,ArrayList<Student>对象的引用名);
案例1
将用户名和密码存储到服务器,且用户名和密码作为一个User对象,是否能将
User对象存储到session中?可以采取如下思路:将整个User对象作为键值对中的
“值”。
页面1:login.jsp
User user = new User("Jake","123");
session.setAttribute("userinfo",user);
页面2:welcome.jsp
访问session中存储的数据:
u = (User)(session.getAttribute("userinfo"));
欢迎你!<%=u.getName()%>
案例2
login.jsp页面将一个Teacher对象作为键值对中的“值”,存储到session对象。welcome.jsp页面访问session,并将这个Teacher对象的信息一一显示出来。
login.jsp:
<body>
<%
Teacher teacher = new Teacher("00001","Jake",15,"女","副教授");
session.setAttribute("teacher",teacher); %>
<jsp:forward page= "welcome.jsp"></jsp:forward>
</body>
Welcome.jsp:
<body>
你好,<%=((Teacher)session.getAttribute("teacher")).getName() %><br>
你的工号是:<%=((Teacher)session.getAttribute("teacher")).getTno()%><br>
你的年龄是:<%=((Teacher)session.getAttribute("teacher")).getAge() %><br>
你的性别是:<%=((Teacher)session.getAttribute("teacher")).getSex() %><br>
你的职称是:<%=((Teacher)session.getAttribute("teacher")).getTitle() %>
</body>
Teacher.java:
package dao;
publicclass Teacher {
String tno;
public String getTno() {
returntno;
}
publicvoid setTno(String tno) {
this.tno = tno;
}
public String getSex() {
returnsex;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
String sex;
intage;
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age = age;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getTitle() {
returntitle;
}
public Teacher( String name, String title,int age) {
this.age = age;
this.name = name;
this.title = title;
}
public Teacher(String tno,String name, int age, String sex, String title) {
super();
this.tno = tno;
this.sex = sex;
this.age = age;
this.name = name;
this.title = title;
}
publicvoid setTitle(String title) {
this.title = title;
}
public Teacher(String name, String title) {
super();
this.name = name;
this.title = title;
}
String name;
String title;
publicstaticvoid main(String[] args) {
// TODO Auto-generatedmethod stub
}
}
举一反三,请大家思考,是否能将一个集合对象存储到session中呢?
案例3
Login.jsp将以下ArrayList中的5个教师的信息存放到session中,welcome.jsp页面访问session,并将ArrayList中的5个教师信息一一显示出来。
Login.jsp:
<body>
<%
Teacher teacher = new Teacher("00001","Jake",45,"女","副教授");
ArrayList<Teacher>list = new ArrayList<Teacher>();
list.add(teacher);
list.add(new Teacher("00002","Mike",38,"男","副教授"));
list.add(new Teacher("00003","Cake",48,"女","教授"));
list.add(new Teacher("00004","Bike",38,"男","教授"));
list.add(new Teacher("00005","Duke",28,"男","助教"));
session.setAttribute("list",list); %>
<jsp:forward page= "welcome.jsp"></jsp:forward>
</body>
welcome.jsp:
<body>
此处显示所有教师的所有信息
</body>
本页面内容全部由作者姚远原创,请使用者写明出处后再行使用。

