实现网站用户登录业务功能
一、业务步骤:
1,用户在页面上点击登陆链接--login.html
2,用户输入用户名和密码点击登入
3,服务端确认是否正确,响应登录成功和失败的页面
二、代码步骤:
1,创建页面html,
1.1 login.html页面,注意action指定值
1.2 输入信息有误导致的登入失败的页面 login_info_fail.html
1.3 用户输入用户名和密码用户,导致失败的页面 login_fail.html
1.4 用户登录成功页面 login_success.html
2,UserController类里定义登入方法
2.1 首先定义String变量和获取用户的用户名 username和密码password
String username=request.getParameter("username");
String password=request.getParameter("password");
2.2 判断用户名username和password验证是否为空isEmtity, false则sendredirect 失败login_info_fail.html页面
if(username.isEmtity || password.isEmpity){
response.sendRedirect("login_info_error.html")
return;
}
2.3首先定位File user的位置,然后反序列化ObjectInputStream。验证username(是否存在用户名)和password是否跟userlist清单里的输入一样
FileInputStream fis=new FileInputStream(userFile);
ObjectInputStream os=new ObjectInputSteam(fis);
User u=(User)os.readObject();
if(userFile.exists){
if(user.getpassword.equals(password)){
response.sendRedirect("/login_success.html");
return;
}
true则login_success.html页面;false则login_fail.html页面
2.4 由于可能FileInputStream ,ObjectInputStream 流存在异常,需要try catch