RegistAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:2k
- package test;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.util.Calendar;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionError;
- import org.apache.struts.action.ActionErrors;
- public final class RegistAction extends Action{
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- MyActionMapping mam = (MyActionMapping)mapping;
- boolean flag = mam.getCreateLog();
-
- UserForm userform = (UserForm) form;
- String name = userform.getName();
- String psw = userform.getPsw();
- String target = "registed";
-
- ActionErrors errors = new ActionErrors();
- if(name.equals("scott") && psw.equals("tiger")){
- if(flag){
- log(name);
- }
- }else{
- if(!name.equals("scott")){ //不存在的用户名
- errors.add("name",
- new ActionError("error.regist.name.unknown",name));
- }else{ //用户名和密码不匹配
- errors.add(ActionErrors.GLOBAL_ERROR,
- new ActionError("error.regist.failure"));
- }
- target = "login";
- }
- if (!errors.isEmpty()) {
- saveErrors(request, errors);
- }
- return (mapping.findForward(target));
- }
-
- protected void log(String name){
- Calendar today = Calendar.getInstance();
- String fileName = "logs\app_log_" + today.get(Calendar.YEAR)
- + "-" + (today.get(Calendar.MONTH) + 1) + "-"
- + today.get(Calendar.DAY_OF_MONTH) + ".txt";
- try{
- FileWriter fw = new FileWriter(fileName,true);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(today.getTime().toString() + ": user " + name + " log in.n");
- bw.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }