📄 registaction.java
字号:
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -