userloginaction.java

来自「使用J2EE Struts开发的房地产信息咨询系统」· Java 代码 · 共 78 行

JAVA
78
字号
/**
 * 注册用户登录处理模块
 * UserLoginAction.java
 * @author usr
 */

package building;

import javax.sql.*;
import javax.servlet.ServletContext;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class UserLoginAction extends Action
{
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)
	throws Exception
	{
		DynaActionForm userLoginForm = (DynaActionForm)form;
		
		//提取表单信息
		String userName = (String)userLoginForm.get("username");
		String password = (String)userLoginForm.get("password");
		
		//字体编码转换
		userName = CharSet.GBK_ISO(userName);
		password = CharSet.GBK_ISO(password);
		password = MD5.comuteDigest(password);
		
		//连接数据库
		ServletContext context = servlet.getServletContext();
		DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
		DB db = new DB(dataSource);
		
		//Http Session会话
		HttpSession session = request.getSession(true);
				
		//页面跳转
		String PageForward = null;
		//错误信息提示页面
		ActionMessages errors = new ActionMessages();
		
		//验证用户的身份
		if (Users.checkUser(db,userName,password))
		{
			//验证成功
			PageForward = "toUserMain";
			session.setAttribute(Constants.LOGIN_USERNAME_KEY,userName);
			
			Users user = Users.userInfo(db,userName);
			session.setAttribute(Constants.USER_INFO_KEY,user);
		}//End of if
		else
		{
			//验证失败
			errors.add(ActionMessages.GLOBAL_MESSAGE,
					new ActionMessage("errors.UserLoginFail"));
			if (!errors.isEmpty())
			{
				saveErrors(request,errors);
			}//End of if
			
			PageForward = "toWrong"; 
		}//End of else
		
		//关闭数据库连接
		db.close();
		
		//返回跳转页面
		return(mapping.findForward(PageForward));
	}//End of execute
	
}//End of class UserLoginAction

⌨️ 快捷键说明

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