登录和注销、基于Session的购物车案例、验证码的使用、防止表单重复提交

本文详细介绍了JavaWeb应用中登录注销的实现,包括登录界面、商品列表的跳转以及注销功能。此外,讲解了基于Session的购物车案例,阐述了商品列表和购物车对象的使用。同时,探讨了验证码在登录过程中的应用,以及如何防止表单重复提交的问题,通过MVC思想和流程图进行了清晰的说明。
摘要由CSDN通过智能技术生成

一:登录和注销:

登录login.jsp界面

	<body>
			<%
				//销毁session
				session.invalidate();
			%>		
			<h3>用户登录</h3>
				<apan style="color:red">${
   errorMsg}</apan>
				<form action="/login" method="post">
				账号:<input type="text" name="username" required/><br/>
				密码:<input type="text" name="password" required/><br/>
					<input type="submit" value=" 朕要登录 "/>			
				</form>
	</body>

设置登录商品列表的servlet

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
   

	private static final long serialVersionUID = 1L;
	
	private IUserDAO dao;
	
	public void init() throws ServletException {
   
		dao = new UserDAOImpl();
	}
	
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		req.setCharacterEncoding("UTF-8");
		//接收请求参数
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		//调用业务方法处理请求
		User user = dao.getUserByUsername(username);
		//控制界面跳转
		if(user == null){
   
			req.setAttribute("errorMsg","亲,"+username+"该账号不存在或者被禁言,请联系管理员!");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return ;
		}
		//检测当前的账户密码是否正确
		if(!user.getPassword().equals(password)){
   
			req.setAttribute("errorMsg", "亲,"+username+"该账号或密码不正确");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		//把当前登录用户存储到Session中
		req.getSession().setAttribute("USER_IN_SESSION",user);
		resp.sendRedirect("/product");
	}
}

跳转到商品列表的servlet

@WebServlet("/product")
public class ProductServlet extends HttpServlet {
   

	private static final long serialVersionUID = 1L;

	private IProductDAO dao;

	public void init() throws ServletException {
   
		dao = new ProductDAOImpl();
	}

	//http://localhost/product 进入service方法,到底是保存,删除,查询
	//http://localhost/product?cmd=save //保存操作
	//http://localhost/product?cmd=delete //保存操作
	//http://localhost/product?cmd=edit //编辑操作
	//http://localhost/product //列表操作
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   		
		//--------------------------------------------
		//检查用户是否已经登录,判断session中是否存在USER_IN_SESSION
		Object user = req.getSession().getAttribute("USER_IN_SESSION");
		if(user == null){
   
			resp.sendRedirect("/login.jsp");
			return ;
		}
		//--------------------------------------------
		req.setCharacterEncoding("UTF-8");
		String cmd = req.getParameter("cmd");
		if ("save".equals(cmd)) {
   
			this.saveOrUpdate(req, resp);
		} else if ("edit".equals(cmd)) {
   
			this.edit(req, resp);
		} else if ("delete".equals(cmd)) {
   
			this.delete(req, resp);
		} else {
   
			this.list(req, resp);
		}
	}

	//列表操作
	protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		//1:接受请求参数,封装对象
		//2:调用业务方法处理请求
		List<Product> list = dao.list();
		req.setAttribute("p", list);
		//3:控制界面跳转
		req.getRequestDispatcher("/WEB-INF/views/product/product.jsp").forward(req, resp);
	}

	//编辑操作
	protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		//1:接受请求参数,封装对象
		String sid = req.getParameter("id");
		if (haslength(sid)) {
   
			Long id = Long.valueOf(sid);
			//2:调用业务方法处理请求
			Product product = dao.get(id);
			req.setAttribute("p", product);
		}
		//3:控制界面跳转
		req.getRequestDispatcher("/WEB-INF/views/product/edit.jsp").forward(req, resp);
	}

	//删除操作
	protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		Long id = Long.valueOf(req.getParameter("id"));
		dao.delete(id);
		resp.sendRedirect(req.getContextPath()+"/product");
	}

	//新增或更新操作
	protected void saveOrUpdate(HttpServletRequest req
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用jsp内置对象实现登录注销功能可以通过以下步骤实现: 1. 创建一个登录页面,包含用户名和密码的输入框以及登录按钮。 2. 在登录页面中使用form表单提交数据到一个jsp页面。 3. 在jsp页面中使用request对象获取用户名和密码,并进行验证。 4. 如果验证通过,将用户信息保存到session对象中。 5. 在其他需要登录才能访问的页面中,使用session对象判断用户是否已经登录,如果没有登录则跳转到登录页面。 6. 在注销功能中,使用session对象清除保存的用户信息。 示例代码如下: 登录页面: ``` <form action="login.jsp" method="post"> <label>用户名:</label> <input type="text" name="username"><br> <label>密码:</label> <input type="password" name="password"><br> <input type="submit" value="登录"> </form> ``` login.jsp页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = request.getParameter("username"); String password = request.getParameter("password"); if("admin".equals(username) &amp;&amp; "123456".equals(password)){ session.setAttribute("username", username); response.sendRedirect("index.jsp"); }else{ out.println("用户名或密码错误!"); } %> ``` 其他需要登录才能访问的页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = (String)session.getAttribute("username"); if(username == null){ response.sendRedirect("login.jsp"); } %> <html> <head> <title>需要登录才能访问的页面</title> </head> <body> <h1>欢迎访问需要登录才能访问的页面!</h1> <a href="logout.jsp">注销</a> </body> </html> ``` 注销页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% session.invalidate(); response.sendRedirect("login.jsp"); %> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值