LoginAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. package StudyBbs;
  2. import javax.sql.DataSource;
  3. import java.sql.Connection;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import org.apache.struts.action.Action;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.apache.struts.action.ActionMessage;
  13. import org.apache.struts.action.ActionMessages;
  14. public final class LoginAction extends Action{  
  15. public ActionForward execute(
  16. ActionMapping mapping,
  17. ActionForm form,
  18. HttpServletRequest request,  
  19. HttpServletResponse response) throws Exception {
  20.     LoginUserForm userform = (LoginUserForm) form;         
  21. String name = userform.getUsername();
  22. String psw = userform.getPassword();
  23.     ServletContext context = servlet.getServletContext();
  24. DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
  25.         DB db = new DB(dataSource);
  26.         HttpSession session = request.getSession();
  27.         session.setMaxInactiveInterval(365*24*3600);
  28. String PageForward;
  29. ActionMessages errors = new ActionMessages();
  30.         
  31.         if ((!name.equals("guest"))&&(!User.checkUser(db,name,psw))) {  //登陆成功
  32.             errors.add(ActionMessages.GLOBAL_MESSAGE,
  33.                            new ActionMessage("errors.loginFail"));
  34. if (!errors.isEmpty()) {
  35. saveErrors(request, errors);
  36.   PageForward="toWrong";
  37. }
  38. else{                                          //登陆成功
  39. session.setAttribute(Constants.SORT_LIST_KEY,Sort.Search(db));
  40. session.setAttribute(Constants.USERNAME_KEY,name);
  41.       PageForward="LoginSuccessed";
  42. }
  43. db.close();
  44.     return (mapping.findForward(PageForward));
  45.   }
  46. }