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

Java编程

开发平台:

Java

  1. package BookStore;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.apache.struts.action.Action;
  5. import org.apache.struts.action.ActionForm;
  6. import org.apache.struts.action.ActionForward;
  7. import org.apache.struts.action.ActionMapping;
  8. import org.apache.struts.action.ActionMessage;
  9. import org.apache.struts.action.ActionMessages;
  10. import javax.servlet.ServletContext;
  11. import javax.sql.DataSource;
  12. import java.sql.Connection;
  13. import org.apache.struts.validator.DynaValidatorForm;
  14. import org.apache.struts.action.ActionMessage;
  15. import org.apache.struts.action.ActionMessages;
  16. public final class RegistAction extends Action{  
  17. public ActionForward execute(
  18. ActionMapping mapping,
  19. ActionForm form,
  20. HttpServletRequest request,  
  21. HttpServletResponse response) throws Exception {
  22.     DynaValidatorForm userForm = (DynaValidatorForm) form;         
  23. String username = (String)userForm.get("username");
  24. String password = (String)userForm.get("password");
  25. String name = (String)userForm.get("name");
  26. String tel = (String)userForm.get("tel");
  27. String address = (String)userForm.get("address");
  28. String email = (String)userForm.get("email");
  29.     ServletContext context = servlet.getServletContext();
  30. DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
  31.         DB db = new DB(dataSource);
  32.         User user=new User();
  33.         user.setUsername(username);
  34.         user.setPassword(password);
  35.         user.setName(name);
  36.         user.setTel(tel);
  37.         user.setAddress(address);
  38.         user.setEmail(email);
  39. String PageForward;
  40. ActionMessages errors = new ActionMessages();
  41.         
  42.         if (user.Insert(db)) {
  43. PageForward="toLogin";  //新用户注册成功
  44.         }
  45.         else{                       //新用户注册失败
  46.             errors.add(ActionMessages.GLOBAL_MESSAGE,
  47.                            new ActionMessage("errors.registFail"));
  48. if (!errors.isEmpty()) {
  49. saveErrors(request, errors);
  50.   PageForward="toWrong";
  51.         } 
  52.            
  53. db.close();
  54.    return  mapping.findForward(PageForward);  
  55.   }
  56. }