JavaEE5学习笔记07-Web层与EJB集成总结(1):servlet与EJB集成
1.?????? 集成时的感受
单丝不成线,孤木不成林。尽管JavaEE5标准不只是作为Web系统而提出的,但是所有的软件系统都是给人用的,都离不开UI处理。而EJB可以作为单机程序Swing、AWT的后台支持,也可以作为B/S的后台支持。既然是B/S就离不开Web框架。JSF也是JavaEE5的标准,它的规范中提出:“可以将EJB组建通过EJB容器注入到Web组件当中”。简而言之,JSF的托管Bean可以像之前咱们笔记中其他EJB组件一样,被容器注入EJB组建以供其Web控制器层(笔者个人觉得托管Bean就可以当做MVC的控制器)使用。而Servlet,你别看一般项目都不用,而是用Struts(Struts框架自己封装了Servlet,他叫做Action),Servlet也是JavaEE5的标准,所以EJB容器的实现必须也能支持将EJB组件注入到Servlet中(呵呵,享受到了家庭定制标准的温暖啊,好歹咱也是个标准啊~~)。那么Struts有点可惜了,暂时不能享受EJB容器提供的服务。不过Struts可以像一般客户端一样,采用JNDI查找服务对象的方式来获取EJB组件。(笔者这么说其实有点儿废话,其实任何的客户端都可以通过JNDI来查找服务对象啊~~)。不过这里稍微说一下,也是稍微抱怨一下~~~在JavaEE5标准中部署有点麻烦,稍微不注意就抛异常,这个我们之后还会提到。这在JavaEE6标准中得到了缓解,JavaEE6允许在一个WAR项目中就可以识别EJB组件,并且注入其中,不需要提供部署ear文件。
2.?????? Servlet+EJB+JBoss集成Demo
下面我们先来看一个SessionBean,这个SessionBean在EJB和其他Web层集成的时候也会用到 。
SessionBean接口
package ejb.sessionBean;
import javax.ejb.Local;
import ejb.messageDrivenBean.dto.UserDTO;
?
/**
?* 本地添加用户信息的sessionBean
?* @author liuyan
?*
?*/
@Local
public interface UserService {
??? public boolean insertUser(UserDTO userDTO);
}
SessionBean实现类
/**
?* 容器管理事务的用户添加的SessionBean
?*
?* @author liuyan
?*/
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class UserServiceEAOImpl implements UserService {
?
??? /**
??? ?* 资源注入
??? ?*/
??? @Resource(mappedName = "java:/jbossdemo")
??? private DataSource dataSource;
?
??? @Resource
??? private SessionContext sessionContext;
?
??? /**
??? ?* 添加方法实现
??? ?*/
??? @Override
??? public boolean insertUser(UserDTO userDTO) {
?
?????? if (userDTO == null || userDTO.getId() == 0
????????????? || userDTO.getName() == null) {
?????????? return false;
?????? }
?
?????? Connection connection = null;
?????? Statement statement = null;
?????? try {
?????????? connection = dataSource.getConnection();
?????????? statement = connection.createStatement();
?????????? StringBuffer insertSQL = new StringBuffer(
????????????????? "insert into person values(");
?
?????????? insertSQL.append(userDTO.getId()).append(",'").append(
????????????????? userDTO.getName()).append("')");
?????????? System.out.println("SQL::" + insertSQL.toString());
?????????? statement.executeUpdate(insertSQL.toString());
?????????? statement.close();
?????????? connection.close();
?????????? return true;
?????? } catch (Exception e) {
?????????? System.out.println("事务回滚~~");
?????????? sessionContext.setRollbackOnly();
?????????? e.printStackTrace();
??? ?????? return false;
?????? }
??? }
?
}
以上就是为Web层服务的SessionBean了,它的作用很简单,就是往数据库插入记录。参数是UserDTO实体,因为此实体很简单,只有id和name,这2个属性在此不再给出代码了。
下面来看Servlet
/**
?* 使用EJB组建的Servlet
?*
?* @author liuyan
?*
?*/
public class EJBServlet extends HttpServlet {
?
??? /**
??? ?*
??? ?*/
??? private static final long serialVersionUID = -8245899459149632416L;
?
??? /**
??? ?* 注入其他EJB组件
??? ?*/
??? @EJB(beanName = "UserServiceEAOImpl")
??? private UserService userService;
?
??? /**
??? ?* Constructor of the object.
??? ?*/
??? public EJBServlet() {
?????? super();
??? }
???????
??? public void doGet(HttpServletRequest request, HttpServletResponse response)
?????????? throws ServletException, IOException {
?????? this.doPost(request, response);
??? }
?
??? /**
??? ?* 执行业务
??? ?*/
??? public void doPost(HttpServletRequest request, HttpServletResponse response)
?????????? throws ServletException, IOException {
?????? String id = request.getParameter("id");
?????? String name = request.getParameter("name");
?????? name = new String(name.getBytes("iso8859-1"), "utf-8");
?????? UserDTO userDTO = new UserDTO();
?????? userDTO.setId(Integer.parseInt(id));
?????? userDTO.setName(name);
?????? userService.insertUser(userDTO);
?????? response.sendRedirect(request.getContextPath()
????????????? + "/insertUserFormList.jsp");
??? }
?
??? public void init() throws ServletException {
?????? // Put your code here
??? }
}
这个Servlet只注意2部分就行,一个是注入的EJB组件,就是刚刚的SessionBean组件,另一个就是直接调用了该组件的insertUser方法。