loginaction.java

来自「网络编程技术与实例源码」· Java 代码 · 共 33 行

JAVA
33
字号
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {

    public ActionForward perform(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    {
        String username = ((LoginForm) form).getUserName();
        String password = ((LoginForm) form).getPassword();
        ActionErrors errors = new ActionErrors();
        if ((!username.equals("deryl")) || (!password.equals("radar")))
            errors.add("password",new ActionError("error.password.mismatch"));
        if ( errors.size()>0 ) {
            saveErrors(request,errors);
            return mapping.findForward("login");
        }
        // 存储权限到attribute中
        HttpSession session = request.getSession();
        session.setAttribute("authentication", username);
        // 控制转移到页面
        return mapping.findForward("success");
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?