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

Java编程

开发平台:

Java

  1. package test;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.util.Calendar;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.struts.action.Action;
  10. import org.apache.struts.action.ActionForm;
  11. import org.apache.struts.action.ActionForward;
  12. import org.apache.struts.action.ActionMapping;
  13. import org.apache.struts.action.ActionError;
  14. import org.apache.struts.action.ActionErrors;
  15. public final class RegistAction extends Action{  
  16. public ActionForward execute(
  17. ActionMapping mapping,
  18. ActionForm form,
  19. HttpServletRequest request,  
  20. HttpServletResponse response) throws Exception {
  21. MyActionMapping mam = (MyActionMapping)mapping;
  22. boolean flag = mam.getCreateLog();
  23.     UserForm userform = (UserForm) form;         
  24. String name = userform.getName();
  25. String psw = userform.getPsw();
  26.     String target = "registed";
  27.    
  28.     ActionErrors errors = new ActionErrors();
  29. if(name.equals("scott") && psw.equals("tiger")){
  30.     if(flag){
  31.     log(name);
  32.     }
  33.     }else{
  34.     if(!name.equals("scott")){ //不存在的用户名
  35. errors.add("name",
  36. new ActionError("error.regist.name.unknown",name));
  37. }else{ //用户名和密码不匹配
  38. errors.add(ActionErrors.GLOBAL_ERROR,
  39. new ActionError("error.regist.failure"));
  40. }
  41. target = "login";
  42. }
  43. if (!errors.isEmpty()) {
  44. saveErrors(request, errors);
  45.     return (mapping.findForward(target));  
  46. }
  47. protected void log(String name){
  48. Calendar today = Calendar.getInstance();
  49. String fileName = "logs\app_log_" + today.get(Calendar.YEAR) 
  50. + "-" + (today.get(Calendar.MONTH) + 1) + "-" 
  51. + today.get(Calendar.DAY_OF_MONTH) + ".txt";
  52. try{
  53. FileWriter fw = new FileWriter(fileName,true);
  54. BufferedWriter bw = new BufferedWriter(fw);
  55. bw.write(today.getTime().toString() + ": user " + name + " log in.n");
  56. bw.close();
  57. }catch(IOException e){
  58. e.printStackTrace();
  59. }
  60. }
  61. }