MVC框架实现用户的登录注册功能

MVC是什么?

MVC,全称model view controller—模型,视图,控制器
model是将数据库中的表到Java中变成实体类
view是编写JSP页面
controller是用来业务逻辑的处理

MVC框架实现登录注册功能

项目框架结构在这里插入图片描述

工具类Util

package com.ishopn.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DBHelper {
	private static String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
	private static String user = "root";
	private static String password = "1234";
	
	/**
	 *  统一的获取数据库连接对象的方法
	 * @param url 数据库连接路径
	 * @param user 用户名
	 * @param password 密码
	 * @return 数据库连接对象
	 */
	public  static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 统一的关闭数据库连接方法
	 * @param conn 数据库的连接对象
	 */
	public static void closeConnection(Connection conn) {
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 *  统一的sql查询方法
	 * @param conn 连接对象
	 * @param sql	需要执行的sql语句
	 * @param param	sql语句中需要替换的参数列表
	 * @return ResultSet 资源结果集
	 */
	public static ResultSet execteQuery(Connection conn,String sql,List param) {
		ResultSet rs = null;
		try {
			 PreparedStatement ps = conn.prepareStatement(sql);
			 if(param!=null && param.size()>0) {
				 for(int i=0;i<param.size();i++) {
					 ps.setObject(i+1, param.get(i));
				 }
			 }
			rs =  ps.executeQuery();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * 统一的sql语句更新方法
	 * @param conn 连接对象
	 * @param sql 需要执行的sql语句
	 * @param param sql语句中需要替换的参数列表
	 * @return  受影响行数
	 */
	public static int executeUpdate(Connection conn,String sql,List param) {
		int line = 0;
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			if(param!=null && param.size()>0) {
				for(int i=0;i<param.size();i++) {
					ps.setObject(i+1, param.get(i));
				}
			}
			line = ps.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return line;
	}
}

dao层的实现类

package com.ishopn.dao.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.ishopn.dao.CustomerDao;
import com.ishopn.model.Customer;
import com.ishopn.util.DBHelper;

public class CustomerDaoimpl implements CustomerDao{
	@Override
	//该方法用来验证用户的名称和手机号是否正确
	public int getcountCustmerByNameAndPhone(Customer cu) {
		int count = 0;
		try {
			//连接数据库  通过DBHelper类中的方法
			Connection conn = DBHelper.getConnection();
			String sql = "select count(*) from customer where cu_name=? and cu_phone=?";
			//将Controller层从页面中获取的参数添加到集合中
			List param = new ArrayList();
			param.add(cu.getCu_name());
			param.add(cu.getCu_phone());
			//调用DBHelper中的execteQuery方法 获得资源结果集
			ResultSet rs = DBHelper.execteQuery(conn, sql, param);
			rs.next();
			count = rs.getInt(1);
			
			//关闭数据库连接
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	//这个方法用来更新数据库的数据  例如实现用户注册功能
	@Override
	public int istCustomer(Customer cu) {
		int line = 0;  //line为数据库中 受影响的行数
		try {
			//连接数据库
			Connection conn = DBHelper.getConnection();
			String sql = "insert into customer (cu_id,cu_name,cu_phone,cu_gender,cu_address) values (?,?,?,?,?)";
			List param = new ArrayList();
			//将Controller层传入的数据保存到集合中 并调用数据库
			param.add(cu.getCu_id());
			param.add(cu.getCu_name());
			param.add(cu.getCu_phone());
			param.add(cu.getCu_gender());
			param.add(cu.getCu_address());
			line = DBHelper.executeUpdate(conn, sql, param);
			
			//关闭数据库连接
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return line;
	}
}

Controller层 业务逻辑处理

package com.ishopn.controller;

import com.ishopn.dao.CustomerDao;
import com.ishopn.dao.impl.CustomerDaoimpl;
import com.ishopn.model.Customer;

public class CustomerController {
	private CustomerDao customerDao;
	//这个类中需要用到Dao层的实现类,因此在构造方法里面实例化Dao对象
	public CustomerController() {
		customerDao = new CustomerDaoimpl();
	}
	//此方法将页面中传入的用户名和用户手机号拿到数据库中的数据进行select查询
	public int getcountCustmerByNameAndPhone(String cu_name,String cu_phone) {
		Customer cu = new Customer(); //实例化一个用户,将页面中的参数保存
		cu.setCu_name(cu_name);
		cu.setCu_phone(cu_phone);
		//调用Dao层方法,取链接数据库并查询是否存在该用户
		return customerDao.getcountCustmerByNameAndPhone(cu);
	}
	//该方法用于用户的注册
	public int istCustomer(String cu_id,String cu_name,String cu_phone,Integer cu_gender,String cu_address) {
	//实例化用户对象,将用户的注册信息保存到用户对象中
		Customer cu = new Customer();
		cu.setCu_id(cu_id);
		cu.setCu_name(cu_name);
		cu.setCu_phone(cu_phone);
		cu.setCu_gender(cu_gender);
		cu.setCu_address(cu_address);
		//调用Dao层去连接并更新数据库  返回受影响行数
		return customerDao.istCustomer(cu);
	}
}

Servlet层

//登录的Servlet
//登录的Servlet
package com.ishopn.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ishopn.controller.CustomerController;

public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		//获取页面中的用户名和用户手机号
		String cu_name = req.getParameter("cu_name");
		String cu_phone = req.getParameter("cu_phone");
		//实例化Controller层对象
		CustomerController cc = new CustomerController();
		//调用Controller层对象的方法 判断输入的用户名和手机号是否存在数据库中
		int count = cc.getcountCustmerByNameAndPhone(cu_name, cu_phone);
		if(count>0) {
			//假如存在该用户,则设置Cookie和session  并进行页面的跳转
			Cookie cookie = new Cookie("cu_name",cu_name);
			resp.addCookie(cookie);
			HttpSession session = req.getSession();
			session.setAttribute("cu_name", cu_name);
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			resp.sendRedirect("/web06");
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}
}
//注册的Servlet
//注册的Servlet
package com.ishopn.servlet;

import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ishopn.controller.CustomerController;

//将普通类继承HttpServlet变成Servlet
public class RegistServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");		
		//与登录一样,获取用户在页面输入的信息
		String cu_id =  UUID.randomUUID().toString();
		String cu_name = req.getParameter("cu_name");
		String cu_phone = req.getParameter("cu_phone");
		Integer cu_gender = new Integer(req.getParameter("cu_gender"));
		String cu_address = req.getParameter("cu_address");	
		//实例化Controller层对象 并调用注册方法
		CustomerController cc = new CustomerController();
		//该方法返回一个受影响行数 ,假如行数大于1,说明数据在数据库中插入成功
		int line = cc.istCustomer(cu_id, cu_name, cu_phone, cu_gender, cu_address);
//		System.out.println("我要跳转了!!!!");
		if(line > 0) {
		//受影响行数大于1  则注册成功 页面惊醒跳转
			resp.sendRedirect("/web06");
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}	
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值