Petstore源码追踪记(3)-商业逻辑处理(三)
http://www.rdxx.com 01年11月18日 11:08 互联网 我要投稿
//判断使用者从登入画面(signon.screen)进行验证工作
if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) {
System.out.println("FORM SIGNON CHECK");
validateSignOn(request, response, chain);
// jump out of this method
return;
}
接着在validateSignON()函式进行使用者验证工作,从Request取出使用者输入的字段值,若使用者有勾选Remember My UserName(记住我的帐号)功能,则产生Cookie记录使用者帐号,再来透过EJB tier从数据库读取资料进行比对,验证成功则将使用者帐号(USER_NAME)及是否已登入(SIGNED_ON_USER)参数存入Session,从Request取出目的URL(ORIGINAL_URL),将网页转导就会到达我们的目的地-使用者基本资料浏览画面(customer.do)
;若验证有误则将网页转导到登入失败画面(signon_error.screen)
,请读者顺便加上侦察程序代码。
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)) {
