最近初学jsp,顺便编写了一个基于MVC开发模式的登录注册
1、util包用于连接数据库操作
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DbUtil { 8 9 private String url="jdbc:mysql://localhost:3306/test"; 10 private String user="root"; 11 private String password="root"; 12 private String driver="com.mysql.jdbc.Driver"; 13 14 public Connection getCon() throws Exception{ 15 Class.forName(driver); 16 Connection con=DriverManager.getConnection(url, user, password); 17 return con; 18 } 19 20 public static void getClose(Connection con) throws SQLException{ 21 if(con!=null){ 22 con.close(); 23 } 24 } 25 26 /*public static void main(String[] args) { 27 DbUtil db=new DbUtil(); 28 try { 29 db.getCon(); 30 System.out.println("测试连接数据库,连接成功"); 31 } catch (Exception e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 System.out.println("测试连接数据库,连接失败"); 35 } 36 37 }*/ 38 }
2、model层对于成员变量的get set方法控制
1 package com.model; 2 3 import java.sql.DriverManager; 4 5 import com.mysql.jdbc.Connection; 6 import com.mysql.jdbc.PreparedStatement; 7 8 public class User { 9 10 private int id; 11 private String username; 12 private String password; 13 14 15 public User() { 16 super(); 17 } 18 public User(String username, String password) { 19 super(); 20 this.username = username; 21 this.password = password; 22 } 23 public int getId() { 24 return id; 25 } 26 public void setId(int id) { 27 this.id = id; 28 } 29 public String getUsername() { 30 return username; 31 } 32 public void setUsername(String username) { 33 this.username = username; 34 } 35 public String getPassword() { 36 return password; 37 } 38 public void setPassword(String password) { 39 this.password = password; 40 } 41 42 43 }
3.dao层用于操作数据库的增删改查方法
1 package com.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 9 import com.ningmeng.model.User; 10 11 public class UserDao { 12 13 public User login(Connection con,User user) throws SQLException{ 14 User resultUser=null; 15 String sql="select * from user where name=? and password=?"; 16 PreparedStatement ps=con.prepareStatement(sql);// 17 ps.setString(1, user.getUsername()); 18 ps.setString(2, user.getPassword()); 19 ResultSet rs=ps.executeQuery(); 20 if(rs.next()){ 21 resultUser=new User(); 22 resultUser.setUsername(rs.getString("name")); 23 resultUser.setPassword(rs.getString("password")); 24 } 25 return resultUser; 26 } 27 public boolean insert(Connection con,User user) 28 { 29 String sql = "INSERT INTO user(name,password) VALUES (?,?)"; //添加的SQL语句 30 boolean flag=false; 31 try{ 32 PreparedStatement psta=con.prepareStatement(sql);// 33 psta.setString(1, user.getUsername()); 34 psta.setString(2, user.getPassword()); 35 flag=psta.executeUpdate()>0; 36 }catch(Exception e){ 37 e.printStackTrace(); 38 } 39 try { 40 con.close(); 41 } catch (SQLException e) { 42 43 e.printStackTrace(); 44 } 45 return flag; 46 } 47 48 //通过id查找 49 public User selectById(Connection con,int id){ 50 51 PreparedStatement psta=null; 52 ResultSet rs=null; 53 User user=null; 54 String sql="select * from user where id=?"; 55 try{ 56 psta=con.prepareStatement(sql); 57 psta.setInt(1, id); 58 rs=psta.executeQuery(); 59 if(rs.next()){ 60 user=new User(); 61 user.setId(rs.getInt(1)); 62 user.setUsername(rs.getString(2)); 63 user.setPassword(rs.getString(3)); 64 65 } 66 }catch(Exception e){ 67 e.printStackTrace(); 68 } 69 try{ 70 con.close(); 71 }catch(Exception e){ 72 e.printStackTrace(); 73 } 74 return user; 75 } 76 //更新方法(修改数据)(改) 77 public boolean update(Connection con,User user){ 78 PreparedStatement psta=null; 79 String sql="update user set name=?,password=? where id=?"; 80 boolean flag=false; 81 try{ 82 psta=con.prepareStatement(sql); 83 psta.setString(1, user.getUsername()); 84 psta.setString(2, user.getPassword()); 85 psta.setInt(3,user.getId()); 86 flag=psta.executeUpdate()>0; 87 }catch(Exception e){ 88 e.printStackTrace(); 89 } 90 try{ 91 con.close(); 92 }catch(Exception e){ 93 e.printStackTrace(); 94 } 95 return flag; 96 } 97 98 public boolean deleteAdmin(Connection con,int id){ //删除 99 String sql = "delete from user where id = ?"; //删除的SQL语句,根据ID删除 100 try { 101 PreparedStatement pst = con.prepareStatement(sql); 102 pst.setInt(1, id); 103 int count = pst.executeUpdate(); 104 pst.close(); 105 return count>0?true:false; //是否删除的判断 106 } catch (SQLException e) { 107 e.printStackTrace(); 108 } 109 return false; 110 } 111 112 }
4、Servlet控制连接前台界面
1 package com.web; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 import com.ningmeng.dao.UserDao; 13 import com.ningmeng.model.User; 14 import com.ningmeng.util.DbUtil; 15 16 public class LoginServlet extends HttpServlet{ 17 18 DbUtil db=new DbUtil(); 19 UserDao userDao=new UserDao(); 20 /** 21 * 22 */ 23 private static final long serialVersionUID = 1L; 24 25 @Override 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 this.doPost(request, response); 28 } 29 30 @Override 31 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 String username=request.getParameter("username"); 33 String password=request.getParameter("password"); 34 Connection con=null; 35 try { 36 User user=new User(username,password); 37 con=db.getCon(); 38 User currentUser=userDao.login(con, user); 39 if(currentUser==null){ 40 //System.out.println("no"); 41 request.setAttribute("error", "用户名或者密码错误"); 42 request.setAttribute("username", username); 43 request.setAttribute("password", password); 44 request.getRequestDispatcher("login.jsp").forward(request, response); 45 }else{ 46 //System.out.println("yes"); 47 HttpSession session=request.getSession(); 48 session.setAttribute("currentUser",currentUser); 49 response.sendRedirect("main.jsp"); 50 } 51 } catch (Exception e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 56 } 57 58 59 }
1 package com.web; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.ningmeng.dao.UserDao; 13 import com.ningmeng.model.User; 14 import com.ningmeng.util.DbUtil; 15 16 /** 17 * Servlet implementation class RegistServlet 18 */ 19 @WebServlet("/RegistServlet") 20 public class RegistServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 DbUtil db=new DbUtil(); 24 UserDao userDao=new UserDao(); 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public RegistServlet() { 29 super(); 30 // TODO Auto-generated constructor stub 31 } 32 33 /** 34 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 35 */ 36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 String username=request.getParameter("username"); 38 String password=request.getParameter("password"); 39 Connection con=null; 40 41 User user=new User(username,password); 42 try { 43 con=db.getCon(); 44 } catch (Exception e) { 45 46 e.printStackTrace(); 47 } 48 boolean current=userDao.insert(con, user); 49 System.out.print(current); 50 System.out.print(user.getUsername()); 51 if(current){ 52 request.setAttribute("error", "注册成功"); 53 54 }else{ 55 request.setAttribute("error", "注册失败"); 56 request.getRequestDispatcher("Regist.jsp").forward(request, response); 57 } 58 59 } 60 61 /** 62 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 63 */ 64 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 65 // TODO Auto-generated method stub 66 doGet(request, response); 67 } 68 69 }
5、修改web.xml对应的servlet
6、前台页面展示
1 <%@ page language="java" contentType="text/html; charset=gbk" 2 pageEncoding="gbk"%> 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=gbk"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="login" method="post"> 11 <table> 12 <tr> 13 <th colspan="2">登录界面</th> 14 </tr> 15 <tr> 16 <td>账号</td> 17 <td><input type="text" id="username" name="username" value="${username}"></td> 18 </tr> 19 <tr> 20 <td>密码:</td> 21 <td><input type="text" id="password" name="password" value="${password}"></td> 22 </tr> 23 <tr> 24 <td><input type="submit" value="提交"></td> 25 <td><font color="red">${error}</font></td> 26 </tr> 27 </table> 28 </form> 29 </body> 30 </html>
1 <%@ page language="java" contentType="text/html; charset=gbk" 2 pageEncoding="gbk"%> 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=gbk"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="login" method="post"> 11 <table> 12 <tr> 13 <th colspan="2">注册界面</th> 14 </tr> 15 <tr> 16 <td>账号</td> 17 <td><input type="text" id="username" name="username" ></td> 18 </tr> 19 <tr> 20 <td>密码:</td> 21 <td><input type="text" id="password" name="password" ></td> 22 </tr> 23 <tr> 24 <td><input type="submit" value="提交"></td> 25 <td><font color="red">${error}</font></td> 26 </tr> 27 </table> 28 </form> 29 </body> 30 </html>
1 <%@ page language="java" contentType="text/html; charset=gbk" 2 pageEncoding="gbk"%> 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=gbk"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <p>登录成功</p> 11 当前用户:${currentUser.username}<br/> 12 当前密码:${currentUser.password}<br/> 13 </body> 14 </html>