forwardpage=url>
param>
forward>
3、内置对象(对象名,范围,作用)
P121表4-3
4、EL表达式
●语法:
${表达式}
P122表4-4、4-5
●关于.和[]操作符的使用
⏹${name.property}等价于${name["property"]}
⏹访问数组中的元素,使用“[]”,${customers[0]}
⏹${header[“user-ent”]}就不能使用${header.user-agent}
●隐式对象:
P123表4-6
●empty运算符的使用
以下情况empty运算符返回true${var}
Øvar变量不存在,即没有被定义。
Øvar变量的值为null。
Øvar变量引用集合(Set、List、Map)类型的对象,并且在集合对象中不包含任何元素
Øempty运算符可以与”!
”运算符一起使用。
${23+15.28}
${12>10}
5、范围(page.equest、session、application)
如何显示某范围内的数据?
${requestScope.param1}或者${param1}
下面代码什么结果?
<%
request.setAttribute("t1","5");
request.setAttribute("t2",newInteger
(2));
%>
test1=${t1+t2}
test2=${10-t2}
test3=${10-t1+t2}
test4=${t1}+${t2}
四、EJB
1、EJB概念
2、EJB3.0包含的bean种类(无状态会话bean、有状态会话bean、消息驱动bean)
无状态会话Bean保存会话状态
无状态会话Bean使用@Stateless标注定义
无状态会话Bean实例能在多个客户端共享
无状态会话Bean使用实例池
有状态会话Bean能够保存会话的状态
有状态会话Bean使用@Stateful标注定义
有状态会话Bean实例不能在多个客户端共享
有状态会话Bean不使用实例池
3、JPA包括在J2EE5规范中,
JPA借助持久化提供者操作实体
实体的生命周期包括新建、受管、分离、删除四个阶段
实体对应数据库中的表
4、
三、程序运行
四、程序代码部分:
一、在一个采用了分层体系结构的Web应用的持久化层中使用了DAO设计模式,DAO类BookDaoJDBCImpl中封装了对数据库中存放书籍信息的Books表进行增、删、改、查询操作的方法,
publicclassBook{
privateStringname;
privateStringauthor;
privateStringpublisher;
privateStringisbn;
privatedoubleprice;
publicBook(Stringname,Stringauthor,Stringpublisher,Stringisbn,doubleprice){
this.name=name;
this.author=author;
this.publisher=publisher;
this.isbn=isbn;
this.price=price;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetAuthor(){
returnauthor;
}
publicvoidsetAuthor(Stringauthor){
this.author=author;
}
publicStringgetPublisher(){
returnpublisher;
}
publicvoidsetPublisher(Stringpublisher){
this.publisher=publisher;
}
publicStringgetIsbn(){
returnisbn;
}
publicvoidsetIsbn(Stringisbn){
this.isbn=isbn;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}}
DAO类:
publicclassBookDaoJDBCImplimplementsBookDao{
privateDataSourceds;
publicBookDaoJDBC()throwsException{
Contextctx=newInitialContext();
ds=(DataSource)ctx.lookup("jdbc/myDatasource");
}
publicListfindAll()throwsException{
Listlist=newArrayList();
Connectioncon=ds.getConnection();
Statementst=con.createStatement();
ResultSetrs=st.executeQuery("select*fromapp.book");
while(rs.next()){
Bookbook=newBook(rs.getString
(1),rs.getString
(2),rs.getString(3),rs.getString(4),rs.getDouble(5));
list.add(book);
}
rs.close();
st.close();
con.close();
returnlist;
}
publicvoidremove(Stringid)throwsException{
Connectioncon=ds.getConnection();
PreparedStatementpst=con.prepareStatement("deletefromapp.bookwhere书号=?
");
pst.setString(1,id);
pst.executeUpdate();
pst.close();
con.close();
}
publicvoidupdate(Bookbook)throwsException{
Connectioncon=ds.getConnection();
PreparedStatementpst=con.prepareStatement("updateapp.bookset姓名=?
作者=?
出版社=?
价格=?
where书号=?
");
pst.setString(1,book.getName());
pst.setString(2,book.getAuthor());
pst.setString(3,book.getPublisher());
pst.setDouble(4,book.getPrice());
pst.setString(5,book.getIsbn());
pst.executeUpdate();
pst.close();
con.close();
}
publicvoidadd(Bookbook)throwsException{
Connectioncon=ds.getConnection();
PreparedStatementpst=con.prepareStatement("insertintoapp.bookvalues(?
?
?
?
?
)");
pst.setString(1,book.getName());
pst.setString(2,book.getAuthor());
pst.setString(3,book.getPublisher());
pst.setString(4,book.getIsbn());
pst.setDouble(5,book.getPrice());
pst.executeUpdate();
pst.close();
con.close();
}
publicBookfindById(Stringid)throwsException{
Connectioncon=ds.getConnection();
Statementst=con.createStatement();
ResultSetrs=st.executeQuery("select*fromapp.bookwhere书号='"+id+"'");
Bookbook=null;
while(rs.next()){
book=newBook(rs.getString
(1),rs.getString
(2),rs.getString(3),rs.getString(4),rs.getDouble(5));
}
returnbook;
}
}
二、参考上面内容:
在一个采用了分层体系结构的Web应用的持久化层中使用了DAO设计模式,DAO类CustomerDaoJDBCImpl中封装了对数据库中存放客户信息的customer表进行增、删、改、查询操作的方法,
Customer表字段:
number、name、phone、email
对应的Customer类
publicclassCustomer{
privateStringnumber;//编号
privateStringname;//名字
privateStringphone;//电话
privateStringemail;//电子邮箱
//省略
}
DAO类
publicclassCustomerDaoJDBCImplimplementsStudentDao{
privateDataSourceds;
publicStudentDaoJDBCImpl()throwsException{
Contextctx=newInitialContext();
ds=(DataSource)ctx.lookup("jdbc/sample");
}
/*添加客户信息的方法*/
publicvoidadd(Customerc)throwsException{
..}
/*查找所有客户信息的方法*/
publicListfindAll()throwsException{
。
。
。
}
/*更新客户信息的方法*/
publicvoidupdateCustomer(Customerc){
/**/
}
/*删除客户信息的方法*/
publicvoiddeleteCustomer(Customerc){
/**/
}
}
四、在一个采用了分层体系结构的Web应用StudentApp的持久化层中使用了DAO设计模式,DAO类StudentDaoJDBCImpl中封装了对数据库中存放学生信息的students表进行增、删、改、查询操作的方法。
Student表:
number、name、phone、email
p