用户登录退出实现
- 步骤分析
- 实现登录
- 实现退出
1)步骤分析
1、在index.jsp上点击登录跳转到login.jsp
- 可以在UserServlet中进行请求转发
2、修改页面上的form表单 method 每个标签添加name属性
- action:/store/user/login
3、 登录操作:
- 获取用户名和密码
- 调用service根据用户名和密码获取一个user
- 判断用户是否为空
若为空:则账号或密码错误
若不为空:继续判断是否激活,只有激活的时候才把用户放入到session中
4、页面重定向到首页上
展示:用户名/退出 我的订单
5、点击退出
- url:/store/user/logout
- 干掉session
- 页面重定向
2)登录实现
① 点击登录到登录界面
修改index.jsp上的登录按钮的跳转地址:
<li><a href="${path}/user/toLogin">登录</a></li>
在UserServlet中添加对路径的处理:
else if(uri.endsWith("/toLogin")) {
toLogin(request,response);
}
toLogin方法请求转发到登录界面
/**
* 请求转发到登录界面
*/
private void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
}
② 修改login.jsp
略
③ Servlet实现登录
- 定义一个Constant常量接口,定义一个常量代表激活状态
/**
* 常量接口
*/
public interface Constant {
/**
* 用户激活状态
*/
int USER_IS_ACTIVE=1;
}
- 处理/login路径,添加login方法
else if(uri.endsWith("/login")) {
login(request,response);
}
- 获取用户名和密码
- 调用service完成登录操作,返回user
- 判断用户
- 将user放入到session中 重定向
/**
* 实现登录
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取用户名和密码
String username=request.getParameter("username");
String password=request.getParameter("password");
//密码加密
password=MD5Utils.md5(password);
//2、调用service完成登录操作 返回user
User user=userService.login(username,password);
//3、判断用户
if(user==null) {
//用户名密码不匹配
request.setAttribute("msg", "用户名或密码错误");
//请求转发会login.jsp
request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
return;
}else {
//继续判断用户状态是否激活
if(Constant.USER_IS_ACTIVE!=user.getState()) {
//没有激活
request.setAttribute("msg", "用户未激活");
request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
return;
}
}
//4、将user放入到session中,重定向到首页
request.getSession().setAttribute("user", "user");
response.sendRedirect(request.getContextPath()+"/");
}
④ 实现service
UserService接口:
/**
* 用户登录
* @param username
* @param password
* @return
*/
User login(String username, String password) throws Exception;
UserServiceImpl实现类:
@Override
public User login(String username, String password) throws Exception {
return userDao.getUserByUsernameAndPwd(username,password);
}
⑤ 实现dao
UserDao接口:
/**
* 根据用户名和密码进行登录
* @param username
* @param password
* @return
*/
User getUserByUsernameAndPwd(String username, String password) throws SQLException;
UserDaoImpl实现类:
/**
* 根据用户名和密码进行登录
*/
@Override
public User getUserByUsernameAndPwd(String username, String password) throws SQLException {
String sql="select * from `user` where username=? and password=? limit 1";
return qr.query(sql, new BeanHandler<User>(User.class),username,password);
}
⑥ 更改登录之后的显示
登录成功,更改index.xml中的登录状态显示:
...
<div class="col-md-3" style="padding-top:20px">
<ol class="list-inline">
<c:if test="${empty user }">
<li><a href="${path}/user/toLogin">登录</a></li>
<li><a href="${path}/user/toRegister">注册</a></li>
</c:if>
<c:if test="${not empty user }">
${user.username }:您好
<li><a href="${path}/user/">退出</a></li>
<li><a href="${path}/user/">我的订单</a></li>
</c:if>
<li><a href="cart.htm">购物车</a></li>
</ol>
</div>
...
登录失败,显示msg:
...
<font>会员登录</font> ${msg }
...
⑦ 登录测试
新注册账号zs,密码123,登录:
去激活之后登录:
如果账号或密码错误:
3)退出实现
① 更改退出的链接地址
<li><a href="${path}/user/logout">退出</a></li>
② 在UserServlet中干掉session对象,重定向到首页
private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
//1、干掉session
request.getSession().invalidate();
//2、页面重定向
response.sendRedirect(request.getContextPath()+"/");
}
③ 测试退出