Java Web 网络商城案例演示六(用户,登录,退出)

Java Web 网络商城案例演示六(用户,登录,退出)
QueryRunner的用法:
https://blog.csdn.net/qq_39240270/article/details/85249061

Ajax向服务器发送请求的几种方式
https://www.runoob.com/jquery/jquery-ajax-intro.html

jsp和jstl相关知识
https://www.runoob.com/jsp/jsp-tutorial.html

用户登录功能

实现用户登录页面跳转
在这里插入图片描述
*_/jsp/index.jsp 修改登录链接

 <li><a href="${pageContext.request.contextPath}/UserServlet?method=loginUI">登录</a></li>

在这里插入图片描述
UserServlet——》 loginUI方法
return “/jsp/login.jsp”;
在这里插入图片描述

	
	public String loginUI(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		return "/jsp/login.jsp";
	}
实现登录的原理分析

在这里插入图片描述

步骤实现:

1、准备工作
/jsp/login.jsp 设置form标签的action和method的属性
设置表单下input标签的name属性

<form class="form-horizontal" action="${pageContext.request.contextPath}/UserServlet?method=userLogin" method="post">
		<div class="form-group">
			<label for="username" class="col-sm-2 control-label">用户名</label>
					<div class="col-sm-6">
						<input type="text" name="username" class="form-control" id="username"
									placeholder="请输入用户名">
							</div>

UserServlet页面获取该请求
UserServlet -->userLogin
获取数据
调用业务层功能
成功session当中存放用户信息,重定向到首页
失败向request的session当中失败信息,转发到登录页面

// userLogin
	public String userLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		// 获取用户数据(账户/密码)
		User user = new User();
		MyBeanUtils.populate(user, request.getParameterMap());// 遍历表单数据将表达上对应的值放入到user对象当中
		// 调用业务层登录功能
		UserService userService = new UserServiceImpl();
		User user02 = null;
		// Alt + Shift + z 抛出异常
		try {
			// 在daoimpl要实现该语句select * from user where username = ? and password = ?
			user02 = userService.userLogin(user);// 返回登录的用户
			// 用户登录成功,将登录成功对应的用户的user02对象放入到session作用域当中
			request.getSession().setAttribute("loginUser", user02);
			response.sendRedirect("/store_v5/index.jsp");
			return null;
		} catch (Exception e) {
			// 用户登录失败
			String message = e.getMessage();// 获取到对应失败的信息
			System.out.println(message);
			//向 request放入登录失败的信息
			request.setAttribute("msg", message);
			return "/js/login.jsp";

		}

service层
自定义的异常向Servlet传递一些2种数据(密码不存在,用户未激活)

package cn.itzheng.store.service;
import java.sql.SQLException;
import cn.itzheng.store.domain.User;
public interface UserService {
	void userRegist(User user) throws SQLException;
	boolean userActive(String code) throws SQLException;
	User userLogin(User user) throws SQLException;
}

serviceimpl

@Override
	public User userLogin(User user) throws SQLException {
		// 此处:可以利用异常在模块当中传递一些数据
		UserDao userDao = new UserDaoImpl();
		// select * from user where username = ? and password =?
		User uu = userDao.userLogin(user);
		if (null == uu) {
			//在这里抛出对应的异常让其在Servlet里面捕获到对应的异常,并反馈到对应的页面上去
			throw new RuntimeException("密码有误!");
		} else if (uu.getState() == 0) {
			throw new RuntimeException("用户未激活!");
		} else {
			return uu;	
		}

	}

dao层

package cn.itzheng.store.dao;
import java.sql.SQLException;
import cn.itzheng.store.domain.User;
public interface UserDao {
	void userRegist(User user) throws SQLException;
	User userActive(String code) throws SQLException;
	void updateUser(User user) throws SQLException;
	User userLogin(User user) throws SQLException;

}

daoimpl

	@Override
	public User userLogin(User user) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "select * from user where username = ? and password = ?";
		QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanHandler<User>(User.class), user.getUsername(), user.getPassword());// 将查询到的内容反放到BeanHandler对象当中且其泛型是User
	}

/jsp/index.jsp当获取到用户的一些信息
引入jstl标签库

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:if test="${empty loginUser}">
<li><a href="${pageContext.request.contextPath}/UserServlet?method=loginUI">登录</a></li>
<li><a href="${pageContext.request.contextPath}/UserServlet?method=registUI">注册</a></li>
</c:if>
<c:if test="${not empty loginUser}">
<li>欢迎${loginUser.username}</li>
<li><a href="${pageContext.request.contextPath}/UserServlet?method=logOut">退出</a></li>
<li><a href="${pageContext.request.contextPath}/jsp/cart.jsp">购物车</a></li>
<li><a href="${pageContext.request.contextPath}/jsp/order_list.jsp">我的订单</a></li>
</c:if>

loginUser.username这里虽然只是username但是执行的是getusername

用户退出功能

原理:向服务端发送请求让session失效
在这里插入图片描述
1、准备工作:
/jsp/index.jsp 修改链接

<li><a href="${pageContext.request.contextPath}/UserServlet?method=logOut">退出</a></li>

在这里插入图片描述
2、UserServlet —> logOut
清除session
重新定向到首页
return null;
在这里插入图片描述

// logOut
	public String logOut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		//清除session 
		request.getSession().invalidate();//使session失效
		//重新定向到首页
		response.sendRedirect("/store_v5/index.jsp");
		//return null;
		return null;
	}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员猫爪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值