源码和数据库下载地址:http://download.csdn.net/detail/biexiansheng/9759722
1:首先需要设计好数据库和数据表,这里简单截图说明我创建的字段和类型。
2:进入正题,开始开发前台和后台代码。首先创建一个动态web工程,然后先创建一个实体类。
1 package com.bie.po; 2 /** 3 * @author BieHongLi 4 * @version 创建时间:2017年2月21日 上午9:59:03 5 * 用户的实体类 6 */ 7 public class User { 8 9 private Integer id; 10 private String name; 11 private String password; 12 private String email; 13 private String phone; 14 public Integer getId() { 15 return id; 16 } 17 public void setId(Integer id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public String getPassword() { 27 return password; 28 } 29 public void setPassword(String password) { 30 this.password = password; 31 } 32 public String getEmail() { 33 return email; 34 } 35 public void setEmail(String email) { 36 this.email = email; 37 } 38 public String getPhone() { 39 return phone; 40 } 41 public void setPhone(String phone) { 42 this.phone = phone; 43 } 44 45 //重写toString 方法 46 @Override 47 public String toString() { 48 return "User [id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", phone=" + phone 49 + "]"; 50 } 51 52 53 }
3:然后创建login.jsp登陆页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户登陆页面</title> 8 <style type="text/css"> 9 h1{text-align:left;} 10 h4{text-align:left;color:red;} 11 body{background:url(images/1.png)} 12 a{text-decoration:none;font-size:20px;color:black;} 13 a:hover{text-decoration:underline;font-size:24px;color:red;} 14 </style> 15 16 </head> 17 <body> 18 <form action="user/userlogin" method="post"> 19 <h1>用户登陆页面</h1> 20 <h4>装饰中......</h4> 21 <hr/> 22 <table align="left"> 23 <tr> 24 <td>账号:</td> 25 <td><input type="text" name="name" id="name"></td> 26 </tr> 27 <tr> 28 <td>密码:</td> 29 <td><input type="password" name="password" id="password"></td> 30 </tr> 31 <tr> 32 <td colspan="1"> 33 </td> 34 <td> 35 <input type="submit" value="登陆"/> 36 <input type="reset" value="重置"/> 37 <a href="register.jsp" target="_blank">注册</a> 38 </td> 39 </tr> 40 </table> 41 </form> 42 </body> 43 </html>
4:再创建注册页面register.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>注册的页面</title> 8 <style type="text/css"> 9 h1{text-align:center;} 10 h4{text-align:right;color:red;} 11 body{background:url(images/2.png)} 12 </style> 13 14 <script type="text/javascript" src="js/jquery.min.js"></script> 15 <script type="text/javascript"> 16 $(document).ready(function(){ 17 //alert("测试jQuery是否能用"); 18 $("#form1").submit(function(){ 19 var name=$("#name").val();//获取提交的值 20 if(name.length==0){//进行判断,如果获取的值为0那么提示账号不能为空 21 //alert("aa");//测试使用 22 $("#nameError").html("账号不能为空"); 23 return false; 24 } 25 26 //密码进行验证不能为空 27 var password=$("#password").val();//获取提交的密码的值 28 if(password.length==0){ 29 $("#passwordError").html("密码不能为空"); 30 return false; 31 } 32 33 //确认密码进行验证 34 var relpassword=$("#relpassword").val();//获取提交的确认密码的值 35 if(relpassword.length==0){ 36 $("#relpasswordError").html("确认密码不能为空哦"); 37 return false; 38 } 39 40 if(password!=relpassword){ 41 $("#relpasswordError").html("确认密码输入不正确,请重新输入"); 42 return false; 43 } 44 }); 45 46 }); 47 </script> 48 </head> 49 <body> 50 <form action="user/userregister" method="post" id="form1"> 51 <h1>用户注册页面</h1> 52 <h4>装饰中......</h4> 53 <hr/> 54 <table align="center"> 55 <tr> 56 <td>账 号:</td> 57 <td> 58 <input type="text" name="name" id="name"/> 59 <div id="nameError" style="display:inline;color:red;"></div> 60 </td> 61 </tr> 62 <tr> 63 <td>密 码:</td> 64 <td> 65 <input type="password" name="password" id="password"> 66 <div id="passwordError" style="display:inline;color:red;"></div> 67 </td> 68 </tr> 69 <tr> 70 <td>确认密码:</td> 71 <td> 72 <input type="password" name="relpassword" id="relpassword"> 73 <div id="relpasswordError" style="display:inline;color:red;"></div> 74 </td> 75 </tr> 76 <tr> 77 <td>电话号码:</td> 78 <td><input type="text" name="phone" id="phone"></td> 79 </tr> 80 <tr> 81 <td>电子邮件:</td> 82 <td><input type="text" name="email" id="email"></td> 83 </tr> 84 <tr> 85 <td colspan="1"> 86 </td> 87 <td> 88 <input type="submit" value="注册"/> 89 <input type="reset" value="重置"/> 90 <a href="login.jsp" target="_blank">登陆</a> 91 </td> 92 </tr> 93 </table> 94 </form> 95 </body> 96 </html>
5:再创建utils层,公共的工具层BaseDao.java;
1 package com.bie.utils; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.ResourceBundle; 9 10 11 /** 12 * @author BieHongLi 13 * @version 创建时间:2017年2月21日 上午10:01:14 14 * 数据交互层dao层 15 */ 16 public class BaseDao { 17 18 private static String driver;//数据库驱动 19 private static String url;//数据库路径,找到对应的数据库 20 private static String user;//数据库账号 21 private static String password;//数据库密码 22 23 static{ 24 driver=ResourceBundle.getBundle("db").getString("driver"); 25 url=ResourceBundle.getBundle("db").getString("url"); 26 user=ResourceBundle.getBundle("db").getString("user"); 27 password=ResourceBundle.getBundle("db").getString("password"); 28 } 29 /*private static String driver="com.mysql.jdbc.Driver"; 30 private static String url="jdbc:mysql:///test"; 31 private static String user="root"; 32 private static String password="123456";*/ 33 34 /*** 35 * 连接数据库的方法 36 * @return 37 * @throws ClassNotFoundException 38 * @throws SQLException 39 */ 40 public static Connection getCon() throws ClassNotFoundException, SQLException{ 41 Class.forName(driver);//加载数据库驱动 42 System.out.println("测试加载数据库成功"); 43 Connection con=DriverManager.getConnection(url, user, password); 44 System.out.println("测试数据库链接成功"); 45 return con; 46 } 47 48 /*** 49 * 关闭数据库的方法 50 * @param con 51 * @param ps 52 * @param rs 53 */ 54 public static void close(Connection con,PreparedStatement ps,ResultSet rs){ 55 if(rs!=null){//关闭资源,避免出现异常 56 try { 57 rs.close(); 58 } catch (SQLException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 } 63 if(ps!=null){ 64 try { 65 ps.close(); 66 } catch (SQLException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 70 } 71 if(con!=null){ 72 try { 73 con.close(); 74 } catch (SQLException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } 78 } 79 } 80 81 /*** 82 * 同意增删改的方法 83 * @param sql 84 * @param arr 85 * @return 86 */ 87 public static boolean addUpdateDelete(String sql,Object[] arr){ 88 Connection con=null; 89 PreparedStatement ps=null; 90 try { 91 con=BaseDao.getCon();//第一步 :连接数据库的操作 92 ps=con.prepareStatement(sql);//第二步:预编译 93 //第三步:设置值 94 if(arr!=null && arr.length!=0){ 95 for(int i=0;i<arr.length;i++){ 96 ps.setObject(i+1, arr[i]); 97 } 98 } 99 int count=ps.executeUpdate();//第四步:执行sql语句 100 if(count>0){ 101 return true; 102 }else{ 103 return false; 104 } 105 } catch (ClassNotFoundException e) { 106 // TODO Auto-generated catch block 107 e.printStackTrace(); 108 } catch (SQLException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 } 112 return false; 113 } 114 115 /*public static void main(String[] args) { 116 try { 117 BaseDao.getCon(); 118 System.out.println("测试数据库链接成功"); 119 } catch (ClassNotFoundException e) { 120 // TODO Auto-generated catch block 121 e.printStackTrace(); 122 } catch (SQLException e) { 123 // TODO Auto-generated catch block 124 e.printStackTrace(); 125 } 126 }*/ 127 128 129 }
6:然后创建dao层数据交互层的接口和实现类
1 package com.bie.dao; 2 3 import com.bie.po.User; 4 5 /** 6 * @author BieHongLi 7 * @version 创建时间:2017年2月21日 上午10:38:40 8 * 创建一个接口用于声明用户登陆注册的方法 9 */ 10 public interface UserDao { 11 12 /*** 13 * 用户登陆的方法声明 14 * @param user 15 * @return 16 */ 17 public User login(User user); 18 19 /*** 20 * 用户注册的方法声明 21 * @param user 22 * @return 23 */ 24 public boolean register(User user); 25 }
1 package com.bie.dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.bie.dao.UserDao; 11 import com.bie.po.User; 12 import com.bie.utils.BaseDao; 13 14 /** 15 * @author BieHongLi 16 * @version 创建时间:2017年2月21日 上午10:38:56 17 * 18 */ 19 public class UserDaoImpl implements UserDao{ 20 21 @Override 22 public User login(User user) { 23 Connection con=null; 24 PreparedStatement ps=null; 25 ResultSet rs=null; 26 try { 27 con=BaseDao.getCon();//1:获取数据库的连接 28 //2:书写sql语句 29 String sql="select * from user where name=? and password=? "; 30 ps=con.prepareStatement(sql);//3:预编译 31 //4:设置值 32 ps.setString(1, user.getName()); 33 ps.setString(2, user.getPassword()); 34 rs=ps.executeQuery();//5:执行sql语句 35 User users=null; 36 if(rs.next()){ 37 users=new User(); 38 //从数据库中获取值设置到实体类的setter方法中 39 users.setId(rs.getInt("id")); 40 users.setName(rs.getString("name")); 41 users.setPassword(rs.getString("password")); 42 users.setEmail(rs.getString("email")); 43 users.setPhone(rs.getString("phone")); 44 45 return user; 46 }else{ 47 return null; 48 } 49 50 } catch (ClassNotFoundException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } catch (SQLException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 return null; 58 } 59 60 /*** 61 * 插入的方法,即注册 62 */ 63 @Override 64 public boolean register(User user) { 65 String sql="insert into user values(0,?,?,?,?) "; 66 List<Object> list=new ArrayList<Object>(); 67 list.add(user.getName()); 68 list.add(user.getPassword()); 69 list.add(user.getEmail()); 70 list.add(user.getPhone()); 71 72 boolean flag=BaseDao.addUpdateDelete(sql,list.toArray()); 73 if(flag){ 74 return true; 75 }else{ 76 return false; 77 } 78 } 79 80 81 }
7:再创建servlet层进行登陆的业务逻辑处理(由于省去了service层,所以在servlet层进行业务逻辑处理了)
1 package com.bie.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.bie.dao.UserDao; 12 import com.bie.dao.impl.UserDaoImpl; 13 import com.bie.po.User; 14 15 16 /** 17 * @author BieHongLi 18 * @version 创建时间:2017年2月21日 上午10:49:44 19 * 20 */ 21 @WebServlet("/user/userlogin") 22 public class UserLoginServlet extends HttpServlet{ 23 24 private static final long serialVersionUID = 1L; 25 26 @Override 27 protected void doGet(HttpServletRequest request, HttpServletResponse response) 28 throws ServletException, IOException { 29 this.doPost(request, response); 30 } 31 32 @Override 33 protected void doPost(HttpServletRequest request, HttpServletResponse response) 34 throws ServletException, IOException { 35 User user=new User(); 36 //获取login.jsp页面提交的账号和密码 37 String name=request.getParameter("name"); 38 String password=request.getParameter("password"); 39 //测试数据 40 System.out.println(name+" "+password); 41 //获取login.jsp页面提交的账号和密码设置到实体类User中 42 user.setName(name); 43 user.setPassword(password); 44 45 //引入数据交互层 46 UserDao dao=new UserDaoImpl(); 47 User us=dao.login(user); 48 //测试返回的值 49 System.out.println(us); 50 if(us!=null){ 51 request.setAttribute("info", "登陆成功"); 52 }else{ 53 request.setAttribute("info", "登录失败"); 54 } 55 56 request.getRequestDispatcher("/index/info.jsp").forward(request, response); 57 } 58 59 60 61 }
8:再创建servlet层进行注册的业务逻辑处理(由于省去了service层,所以在servlet层进行业务逻辑处理了)
1 package com.bie.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.bie.dao.UserDao; 12 import com.bie.dao.impl.UserDaoImpl; 13 import com.bie.po.User; 14 15 /** 16 * @author BieHongLi 17 * @version 创建时间:2017年2月21日 下午1:34:17 18 * 注册的servlet 19 */ 20 @WebServlet("/user/userregister") 21 public class UserRegisterServlet extends HttpServlet{ 22 23 private static final long serialVersionUID = 1L; 24 25 @Override 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 this.doPost(request, response); 29 } 30 31 @Override 32 protected void doPost(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 User user=new User(); 35 //获取login.jsp页面提交的账号和密码 36 String name=request.getParameter("name"); 37 String password=request.getParameter("password"); 38 String email=request.getParameter("email"); 39 String phone=request.getParameter("phone"); 40 41 //获取register.jsp页面提交的账号和密码设置到实体类User中 42 user.setName(name); 43 user.setPassword(password); 44 user.setEmail(email); 45 user.setPhone(phone); 46 47 //引入数据交互层 48 UserDao dao=new UserDaoImpl(); 49 boolean flag=dao.register(user); 50 if(flag){ 51 request.setAttribute("info", "注册成功"); 52 }else{ 53 request.setAttribute("info", "注册失败"); 54 } 55 56 request.getRequestDispatcher("/index/info.jsp").forward(request, response); 57 } 58 59 }
9:为了避免乱码,使用过滤器进行过滤。
1 package com.bie.utils; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.annotation.WebFilter; 12 import javax.servlet.http.HttpServletRequest; 13 14 /** 15 * @author BieHongLi 16 * @version 创建时间:2017年2月21日 上午11:08:49 17 * 18 */ 19 @WebFilter("/*") 20 public class UTFFilter implements Filter{ 21 22 @Override 23 public void destroy() { 24 // TODO Auto-generated method stub 25 26 } 27 28 @Override 29 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 30 FilterChain filterChain)throws IOException, ServletException { 31 //将servletRequest转发为HttpServletRequest 32 HttpServletRequest request=(HttpServletRequest)servletRequest; 33 request.setCharacterEncoding("utf-8"); 34 filterChain.doFilter(servletRequest, servletResponse); 35 } 36 37 @Override 38 public void init(FilterConfig arg0) throws ServletException { 39 // TODO Auto-generated method stub 40 41 } 42 43 44 }
效果如下所示: