LoginAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:2k
- package StudyBbs;
- import javax.sql.DataSource;
- import java.sql.Connection;
- import javax.servlet.ServletContext;
- 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.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionMessage;
- import org.apache.struts.action.ActionMessages;
- public final class LoginAction extends Action{
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- LoginUserForm userform = (LoginUserForm) form;
- String name = userform.getUsername();
- String psw = userform.getPassword();
-
- ServletContext context = servlet.getServletContext();
- DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
-
- DB db = new DB(dataSource);
- HttpSession session = request.getSession();
- session.setMaxInactiveInterval(365*24*3600);
- String PageForward;
- ActionMessages errors = new ActionMessages();
-
- if ((!name.equals("guest"))&&(!User.checkUser(db,name,psw))) { //登陆成功
- errors.add(ActionMessages.GLOBAL_MESSAGE,
- new ActionMessage("errors.loginFail"));
- if (!errors.isEmpty()) {
- saveErrors(request, errors);
- }
- PageForward="toWrong";
- }
- else{ //登陆成功
- session.setAttribute(Constants.SORT_LIST_KEY,Sort.Search(db));
- session.setAttribute(Constants.USERNAME_KEY,name);
- PageForward="LoginSuccessed";
- }
-
- db.close();
- return (mapping.findForward(PageForward));
- }
- }