编写一个用户登陆程序,有Login.jsp输入用户名、密码,程序提交之后与Servlet配置中的初始参数中的用户 名与密码,进行比较,若符合则显示登陆成功,否则提示用户名或密码错误。
public org.apache.struts.action.ActionForward perform(org.apache.struts.action.ActionMapping actionMapping, org.apache.struts.action.ActionForm actionForm, javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse)
{
org.apache.struts.action.ActionErrors errors = new ActionErrors();
com.cargo.crotrol.AdminForm adminForm = (com.cargo.crotrol.AdminForm)actionForm;
com.cargo.model.Admin admin = adminForm.getAdmin();
java.lang.String id = getServlet().getServletConfig().getInitParameter("id");
java.lang.String password = getServlet().getServletConfig().getInitParameter("password");
if (!admin.getId().equals(id.trim()) || !admin.getPassword().equals(password.trim()))
{
errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("error.logon.fail"));
saveErrors(httpServletRequest, errors);
}
if (!errors.empty())
{
return new ActionForward(actionMapping.getInput());
} else
{
javax.servlet.http.HttpSession session = httpServletRequest.getSession();
session.setAttribute("id", admin.getId());
return actionMapping.findForward("success");
}
}
可以参考一下这个 这个是在struts 里写的
{
org.apache.struts.action.ActionErrors errors = new ActionErrors();
com.cargo.crotrol.AdminForm adminForm = (com.cargo.crotrol.AdminForm)actionForm;
com.cargo.model.Admin admin = adminForm.getAdmin();
java.lang.String id = getServlet().getServletConfig().getInitParameter("id");
java.lang.String password = getServlet().getServletConfig().getInitParameter("password");
if (!admin.getId().equals(id.trim()) || !admin.getPassword().equals(password.trim()))
{
errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("error.logon.fail"));
saveErrors(httpServletRequest, errors);
}
if (!errors.empty())
{
return new ActionForward(actionMapping.getInput());
} else
{
javax.servlet.http.HttpSession session = httpServletRequest.getSession();
session.setAttribute("id", admin.getId());
return actionMapping.findForward("success");
}
}
可以参考一下这个 这个是在struts 里写的
温馨提示:答案为网友推荐,仅供参考