09_用户登录退出实现

用户登录退出实现

  • 步骤分析
  • 实现登录
  • 实现退出

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实现登录
  1. 定义一个Constant常量接口,定义一个常量代表激活状态

    /**
     * 常量接口
     */
    public interface Constant {
    	/**
    	 * 用户激活状态
    	 */
    	int USER_IS_ACTIVE=1;
    }
    
  2. 处理/login路径,添加login方法

    else if(uri.endsWith("/login")) {
    	login(request,response);
    }
    
  3. 获取用户名和密码

  4. 调用service完成登录操作,返回user

  5. 判断用户

  6. 将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()+"/");
}
③ 测试退出
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

robona

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值