public class SignOnEJB implements SessionBean {
private static final String USER_HOME_ENV_NAME =
"java:comp/env/ejb/local/User";
private InitialContext ic = null;
private UserLocalHome ulh = null;
public void ejbCreate() throws CreateException {
try {
ic = new InitialContext();
//取得UserLocalHome Reference,它是代表使用者基本資料的Local Entity Bean
ulh = (UserLocalHome) ic.lookup(USER_HOME_ENV_NAME);
} catch (NamingException ne) {
throw new EJBException("SignOnEJB Got naming exception! " +
ne.getMessage());
}
}
/**
*此函數(shù)由SignOnFilter呼叫,依使用者帳號找出對應的User實體
*(instance),然后呼叫User實體的密碼比對函數(shù)-user.matchPassWord()
* business method used to check if a user is allowed to sign on
*/
public boolean authenticate(String userName, String password) {
//請加入偵察程序代碼,方便稍候程序驗證
System.out.println("SignOnEJB執(zhí)行authenticate()進行使用者驗證
userName="+userName+", password="+password);
try {
UserLocal user = ulh.findByPrimaryKey(userName);
return user.matchPassword(password);
} catch (FinderException fe) {
return false; // User not found, so authentication failed.
}
} 以下略...public boolean matchPassword(String password) {
//請加入偵察程序代碼,方便稍候程序驗證
System.out.println("UserEJB執(zhí)行matchPassword()進行密碼比對");
return password.equals(getPassword());
}
boolean signedOn = false;
if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) {
signedOn
=((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue();
//加入偵察碼
System.out.println("signedOn="+signedOn);
} else {
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false));
}
// jump to the resource if signed on
//若已登入過,則結束此Filter工作,進入Filter chain,以本例來說,它為
Filter chain中最后一個Filter,所以就是不做任何事,讓使用者進入他的目的畫面
if (signedOn) {
//加入偵察碼
System.out.println("使用者已登入過!");
chain.doFilter(request,response);
return;
}
參考前面敘述重新編譯部署后執(zhí)行,可得下圖預期結果:
圖20 第三階段程序驗證結果
customer.do
到這里相信讀者能對Petstore登入流程控管有更深入的了解,通過登入流程就到達了使用者基本數(shù)據(jù)瀏覽畫面(customer.do),*.do與前二篇介紹的*.screen不同,*.screen代表的是一個畫面,如main.screen代表首頁,它可由多個.jsp所組成;*.do代表的是一個動作,customer.do代表對使用者基本資料相關動作,如新增、修改、刪除,它會透過EJB tier與資料庫互動,最后得到的結果也是要展現(xiàn),運用*.screen的機制組成結果畫面,所以我們可以這樣想象*.screen是名詞,*.do是動詞,以本例來說,只是讀取資料,雖然運用到*.do,但并沒有任何動作,為了能讓讀者了解整個架構,還是在此稍事說明。
請開?deploytool,點選左邊窗格Files > applications > PetstoreEAR > PetstoreWAR > MainServlet,選擇右邊 Alias頁,可發(fā)現(xiàn)處理*.do即是MainServlet
圖21 *.do對應MainServlet
點選General頁可找到實際對應類別,源碼在
Petstore_home/src/waf/src/controller/com/sun/j2ee/blueprints/waf/controller/web/MainServlet.java
,在約79列可找到初始函數(shù):
public void init(ServletConfig config) throws ServletException {
//讀取預設語系,值為”en_US”
String defaultLocaleString = config.getInitParameter("default_locale");
defaultLocale = I18nUtil.getLocaleFromString(defaultLocaleString);
this.context = config.getServletContext();
String requestMappingsURL = null;
try {
//讀取mapping.xml
requestMappingsURL =
context.getResource("/WEB-INF/mappings.xml").toString();
} catch (java.net.MalformedURLException ex) {
System.err.println("MainServlet: initializing ScreenFlowManager
malformed URL exception: " + ex);
}
//將mappings.xml轉成HashMap類別并存入ServletContext
urlMappings = URLMappingsXmlDAO.loadRequestMappings(requestMappingsURL);
context.setAttribute(WebKeys.URL_MAPPINGS, urlMappings);
eventMappings = URLMappingsXmlDAO.loadEventMappings(requestMappingsURL);
context.setAttribute(WebKeys.EVENT_MAPPINGS, eventMappings);
//ScreenFlowManager初始化并存入ServletContext
getScreenFlowManager();
//RequestProcessor初始化并存入ServletContext
getRequestProcessor();
}
<url-mapping url="customer.do" screen="customer.screen" >這一段卷標(tag)有三個參數(shù):
<web-action-class>com.sun.j2ee.blueprints.petstore.controller.web.actions.CustomerHtmlAction</web-action-class>
</url-mapping>
(出處:http://m.survivalescaperooms.com)
新聞熱點
疑難解答