⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logonaction.java

📁 asp制作的在线考试系统
💻 JAVA
字号:
package cn.hxex.exam.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.hxex.exam.dao.DAOFactory;
import cn.hxex.exam.dao.StudentDAO;
import cn.hxex.exam.dao.TeacherDAO;
import cn.hxex.exam.dao.UserDAO;
import cn.hxex.exam.form.UserForm;
import cn.hxex.exam.model.Student;
import cn.hxex.exam.model.Teacher;
import cn.hxex.exam.model.User;
import cn.hxex.exam.struts.BaseAction;
import cn.hxex.exam.struts.StrutsConstants;

/**
 * The action about user login
 * 
 * @struts.action name="userForm" path="/logon" scope="request"
 *                input="/logon.jsp" validate="false" parameter="p"
 *                roles="unspecified,logon"
 * 
 * @struts.action-forward name="admin" path="/admin.jsp"
 * 
 * @struts.action-forward name="student" path="/student.jsp"
 * 
 * @struts.action-forward name="teacher" path="/teacher.jsp"
 * 
 * @struts.action-forward name="password" path="/manage/password.jsp"
 * 
 * @author Administrator
 * 
 */
public class LogonAction extends BaseAction
{
	protected final Log log = LogFactory.getLog(getClass());

	/**
	 * 缺省的处理方法 其功能是跳转到登录页面
	 */
	@Override
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		return mapping.getInputForward();
	}

	/**
	 * 用户登录功能
	 */
	public ActionForward logon(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		// 得到用户提交的参数
		UserForm user = (UserForm) form;

		// 检索用户信息
		UserDAO dao = DAOFactory.getDao(UserDAO.class);
		User u = dao.getUserByName(user.getName());
		if (u != null && u.getPassword().equals(user.getPassword()))
		{
			// 登录成功,保存用户信息到Session中
			HttpSession session = request.getSession();
			session.setAttribute(StrutsConstants.SESSION_USER, u);
			return mapping.findForward(u.getUserType());
		}

		// 登录失败,设置提示信息,返回登录页面
		addMessage(request, "logon.failed");

		return mapping.getInputForward();
	}

	/**
	 * 用户退出登录功能
	 */
	public ActionForward logoff(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		HttpSession session = request.getSession();
		session.invalidate();

		return mapping.getInputForward();
	}

	/**
	 * 进入用户修改密码页面
	 */
	public ActionForward passwordin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		return mapping.findForward("password");
	}

	/**
	 * 用户修改密码功能
	 */
	public ActionForward password(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		// 得到用户提交的信息
		UserForm user = (UserForm) form;

		// 得到当前用户的信息
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute(StrutsConstants.SESSION_USER);

		// 取得用户输入的旧密码
		String oldpassword = request.getParameter("oldpassword");
		if (u.getPassword().equals(oldpassword))
		{
			// 修改用户密码
			if (u.isAdmin())
			{
				UserDAO dao = DAOFactory.getDao(UserDAO.class);
				User us = dao.findById(u.getId(), true);
				us.setPassword(user.getPassword());
				session.setAttribute(StrutsConstants.SESSION_USER, us);
			}
			else if (u.isTeacher())
			{
				TeacherDAO dao = DAOFactory.getDao(TeacherDAO.class);
				Teacher t = dao.findById(u.getId(), true);
				t.setPassword(user.getPassword());
				session.setAttribute(StrutsConstants.SESSION_USER, t);
			}
			else
			{
				StudentDAO dao = DAOFactory.getDao(StudentDAO.class);
				Student s = dao.findById(u.getId(), true);
				s.setPassword(user.getPassword());
				session.setAttribute(StrutsConstants.SESSION_USER, s);
			}

			// 设置修改成功的提示信息
			addMessage(request, "user.msg.pwdsuccess");
		}
		else
		{
			// 设置修改失败的提示信息
			addMessage(request, "user.msg.pwdfail");
		}
		return mapping.findForward("password");
	}

	/**
	 * 跳转到用户首页的方法
	 */
	public ActionForward homepage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		// 得到当前登录用户的信息
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute(StrutsConstants.SESSION_USER);

		// 跳转到用户的首页
		return mapping.findForward(u.getUserType());
	}
}

⌨️ 快捷键说明

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