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();
//声明错误集对象
ActionErrors errors = new ActionErrors();
//声明数据源和连接对象
DataSource dataSource;
Connection cnn=null;
//校验输入
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);
}
//调用业务逻辑
if(errors.size()==0){
String validated = "";
try{
//取得数据库连接
dataSource = getDataSource(request,"A");
cnn = dataSource.getConnection();
UserInfoBo userInfoBo=new UserInfoBo(cnn);
validated =userInfoBo.validatePwd(username,password);
if(validated.equals("match")){
//一切正常就保存用户信息并转向成功的页面
HttpSession session = request.getSession();
session.setAttribute("userInfoForm", form);
return mapping.findForward("success");
}
}
catch(Throwable e){
//处理可能出现的错误
e.printStackTrace();
ActionError error=new ActionError(e.getMessage());
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
}
//如出错就转向输入页面,并显示相应的错误信息
saveErrors(request, errors);
return new ActionForward(mapping.getInput());
}
}
|