<waf:form name="existingcustomer" action="j_signon_check" method="POST">以下略...
<table cellpadding="5" cellspacing="0" border="0">
<tr>
<td class="petstore" align="center" colspan="2">
<b>Yes.</b>
</td>
</tr>
<tr>
<td class="petstore_form" align="right">
<b>User Name:</b>
</td>
<td class="petstore_form">
<c:choose>
<c:when test="${cookie['bp_signon'] != null && cookie['bp_signon']
!=''}">
<waf:input CSSClass="petstore_form"
type="text"
size="15"
name="j_username"
validation="validation">
<waf:value><c:out value="${cookie['bp_signon'].value}"/></waf:value>
</waf:input>
</td>
</tr>
<tr>
if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) {接着在validateSignON()函式进行使用者验证工作,从Request取出使用者输入的字段值,若使用者有勾选Remember My UserName(记住我的帐号)功能,则产生Cookie记录使用者帐号,再来透过EJB tier从数据库读取资料进行比对,验证成功则将使用者帐号(USER_NAME)及是否已登入(SIGNED_ON_USER)参数存入session,从Request取出目的URL(ORIGINAL_URL),将网页转导就会到达我们的目的地-使用者基本资料浏览画面(customer.do)
System.out.PRintln("FORM SIGNON CHECK");
validateSignOn(request, response, chain);
// jump out of this method
return;
}
public void validateSignOn(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//从Request取出使用者输入的字段值
// convert to a http servlet request for now
HttpServletRequest hreq = (HttpServletRequest)request;
HttpServletResponse hres = (HttpServletResponse)response;
// get the user name
String userName = hreq.getParameter(FORM_USER_NAME);
// get the password
String password = hreq.getParameter(FORM_PASSWORD);
// check if the user wants userName set in cookie
String rememberUserName =
hreq.getParameter(REMEMBER_USERNAME);
//若使用者有勾选Remember My User Name(记住我的帐号)功能,则产生Cookie记录使用者帐号
if (rememberUserName != null) {
// set a cookie with the username in it
Cookie userNameCookie = new Cookie(COOKIE_NAME, userName);
// set cookie to last for one month
userNameCookie.setMaxAge(2678400);
hres.addCookie(userNameCookie);
} else {
// see if the cookie exists and remove accordingly
Cookie[] cookies = hreq.getCookies();
if (cookies != null) {
for (int loop=0; loop < cookies.length; loop++) {
if (cookies[loop].getName().equals(COOKIE_NAME)) {
cookies[loop].setMaxAge(0);
hres.addCookie(cookies[loop]);
}
}
}
}
//透过EJB从数据库读取资料进行比对
//validate against the registered users
SignOnLocal signOn = getSignOnEjb();
//请加入侦察程序代码,方便稍候程序验证
System.out.println("进行EJB tier使用者验证");
//帐号及密码验证
boolean authenticated = signOn.authenticate(userName, password);
if (authenticated) {
//验证成功则将使用者帐号(USER_NAME)及是否已登入(SIGNED_ON_USER)参数
存入Session
// place a true boolean in the session
if (hreq.getSession().getAttribute(USER_NAME) !
= null) {
hreq.getSession().removeAttribute(USER_NAME);
}
hreq.getSession().setAttribute(USER_NAME, userName);
// remove the sign on user key before putting it back in
if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) {
hreq.getSession().removeAttribute(SIGNED_ON_USER);
}
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(true));
//将网页转导就会到达我们的目的地-使用者基本资料浏览画面(customer.do)
// redirect to the original destination
String targetURL =
(String)hreq.getSession().getAttribute(ORIGINAL_URL);
hres.sendRedirect(targetURL);
return;
} else {
//若验证有误则将网页转导到登入失败画面(signon_error.screen)
hres.sendRedirect(signOnErrorPage);
return;
}
}
//取得SignOn Local Stateless Session Bean Reference
private SignOnLocal getSignOnEjb() throws ServletException {
SignOnLocal signOn = null;
try {
InitialContext ic = new InitialContext();
Object o = ic.lookup("java:comp/env/ejb/local/SignOn");
SignOnLocalHome home =(SignOnLocalHome)o;
signOn = home.create();
} catch (javax.ejb.CreateException cx) {
throw new ServletException("Failed to Create SignOn EJB: caught "
+ cx);
} catch (javax.naming.NamingException nx) {
throw new ServletException("Failed to Create SignOn EJB: caught "
+ nx);
}
return signOn;
}
Object o = ic.lookup("java:comp/env/ejb/local/SignOn");
(出处:http://www.VeVb.com)
新闻热点
疑难解答