西元2006年02月19日
use action/servlet to call EJB3
現在我們撰寫 WebApplication, 現在應該都是採用 MVC 的架構,
就算不是利用 Opensource 如 Struts, Webwork, Tapestry 等等,
也可能是利用 JSF, 甚至自己撰寫的 Servlet 作為 Model2 的 Controller.
這在學習 ejb3 有一個好處, 因為目前無法在 jsp 撰寫 @annotations,
我們可以透過 controller 的 servlet 呼叫 action 或任何 java class,
如此一來, 在 java source 之中,
只需要撰寫
package com.softleader.web.servlet;
import javax.ejb.EJB;
public class HelloServlet {
@EJB
private MySessionLocal mysession;
public String doGet( xxxxx ) {
String name = request.getParameter("name");
return mysession.getHello(name);
}
}
透過 @EJB 的 annotation, 我們可以不用在使用複雜的
InitialContext ic = new InitialContext();
ic.lookup("ejb/MySession");
再去轉型, 容易發生導致 ClassCastException 的錯誤 :)
17:25 發表於 EJB3 | 永久網址 | 留言 (0) | Email this


The comments are closed.