package action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import bussness.UserInfoBo;
import entity.UserInfoForm;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public final class LogonAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
UserInfoForm userInfoForm = (UserInfoForm) form;
//從web層獲得用戶名和口令
String username = userInfoForm.getUsername().trim();
String password = userInfoForm.getPassword().trim();
//聲明錯(cuò)誤集對象
ActionErrors errors = new ActionErrors();
//聲明數(shù)據(jù)源和連接對象
DataSource dataSource;
Connection cnn=null;
//校驗(yàn)輸入
if(username.equals("")){
ActionError error=new ActionError("error.missing.username");
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
if(password.equals("")){
ActionError error=new ActionError("error.missing.password");
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
//調(diào)用業(yè)務(wù)邏輯
if(errors.size()==0){
String validated = "";
try{
//取得數(shù)據(jù)庫連接
dataSource = getDataSource(request,"A");
cnn = dataSource.getConnection();
UserInfoBo userInfoBo=new UserInfoBo(cnn);
validated =userInfoBo.validatePwd(username,password);
if(validated.equals("match")){
//一切正常就保存用戶信息并轉(zhuǎn)向成功的頁面
HttpSession session = request.getSession();
session.setAttribute("userInfoForm", form);
return mapping.findForward("success");
}
}
catch(Throwable e){
//處理可能出現(xiàn)的錯(cuò)誤
e.printStackTrace();
ActionError error=new ActionError(e.getMessage());
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
}
//如出錯(cuò)就轉(zhuǎn)向輸入頁面,并顯示相應(yīng)的錯(cuò)誤信息
saveErrors(request, errors);
return new ActionForward(mapping.getInput());
}
} |