![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UserDao { /** * 登录方法 * 接收service层传递用户名和密码 * 查询数据表,获取结果集 * 结果集封装User对象,返回业务层 */ public User login(String username,String password) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); //拼写登录查询语句 String sql = "select * from user where username=? and password=?"; User user = qr.query(sql,new BeanHandler<User>(User.class),username,password); return user; } /** * 注册方法 * 接收service传递JavaBean * 取出JavaBean数据,写入数据表 */ public void register(User user) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); //拼写添加数据SQL语句 String sql = "insert into user values(?,?,?,?,?,?,?)"; //创建数组,存储User对象的数据 Object[] params = {null,user.getUsername(),user.getPassword(),user.getEmail(), user.getName(),user.getGender(),user.getBirthday()}; //执行SQL qr.update(sql,params); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UserService { /** * 登录方法 * 接收Web层传递用户名和密码 * 参数传递dao层(数据验证) * 获取dao返回值,User * 结果,返回web层boolean */ public boolean login(String username,String password){ UserDao dao = new UserDao(); User user = null; try { user = dao.login(username,password); } catch (SQLException e) { e.printStackTrace(); } //判断user对象,是null,登录失败返回false if(user==null) return false; return true; } /** * 注册方法 * 接收web层传递JavaBean对象 * 将JavaBean传递dao层 */ public void register(User user){ UserDao dao = new UserDao(); try { dao.register(user); } catch (SQLException e) { e.printStackTrace(); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@WebServlet(urlPatterns = "/login") public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 实现登录Servlet * 接收客户端提交用户名密码 * 提交参数传递业务层 * 接收业务层返回结果 (boolean) * 判断结果: true 登录成功 * 页面重定向到首页 * * 判断结果: false 登录失败 * 错误数据存储到request域中 * 转发回原有的登录页面 */ String username = request.getParameter("username"); String password = request.getParameter("password"); //调用业务层方法,获取返回值,传递用户名密码 UserService service = new UserService(); boolean flag = service.login(username, password); //判断flag值,是true,登录成功,重定向首页 if(flag){ response.sendRedirect( request.getContextPath() ); }else{ //flag值是false,登录失败,错误信息存储到request域 request.setAttribute("msg","用户名或密码错误"); //转发回登录页面 request.getRequestDispatcher("/login.jsp").forward(request,response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@WebServlet(urlPatterns = "/register") public class RegisterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 获取客户端提交的数据 Map * 数据封装到JavaBean对象中 (User) * 调用业务层的方法,传递User * 页面重定向到登录页面 */ request.setCharacterEncoding("utf-8"); //获取客户端提交的数据 Map Map<String,String[]> map = request.getParameterMap(); //集合中数据封装到User中 User user = new User(); //apache 开发工具类,简化JavaBean开发 //BeanUtils类,静态方法 populate(JavaBean,map集合) try { BeanUtils.populate(user, map); }catch (Exception ex){ex.printStackTrace();} //调用业务层方法,传递JavaBean,完成注册 UserService service = new UserService(); service.register(user); //注册完成,页面到达登录页面,cdx response.sendRedirect( request.getContextPath()+"/login.jsp" ); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private int uid; private String password; private String email; private String username; private String name; private String gender; private String birthday;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class C3P0Utils { private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection() throws SQLException{ return dataSource.getConnection(); } public static void close(ResultSet rs,Statement stat,Connection con){ if(rs!=null) try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } if(stat!=null) try{ stat.close(); }catch(Exception ex){} if(con!=null) try{ con.close(); }catch(Exception ex){} } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <!-- property标签,配置数据库连接四大信息 name属性,要配置什么,标签体,配置的实际内容 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/web04</property> <property name="user">root</property> <property name="password">root</property> </default-config> </c3p0-config>