期刊论文在线投稿审稿系统①(简介+登录注册功能的实现)
一、简介
二、登录注册功能的实现
测试过后,打算还是继续将这个系统进行完善,所以将该系统更新在博客上。
今天更新的是登录注册模块
(一)效果如下
1、登录界面
2、注册界面
(二)建表
login
1 SET FOREIGN_KEY_CHECKS=0; 2 3 -- ---------------------------- 4 -- Table structure for `login` 5 -- ---------------------------- 6 DROP TABLE IF EXISTS `login`; 7 CREATE TABLE `login` ( 8 `id` int(11) NOT NULL AUTO_INCREMENT, 9 `name` varchar(255) DEFAULT NULL, 10 `password` varchar(255) DEFAULT NULL, 11 `identity` varchar(255) DEFAULT NULL, 12 `status` varchar(255) DEFAULT NULL, 13 PRIMARY KEY (`id`) 14 ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; 15 16 -- ---------------------------- 17 -- Records of login 18 -- ---------------------------- 19 INSERT INTO `login` VALUES ('1', 'admin', '1', '系统管理员', '1'); 20 INSERT INTO `login` VALUES ('2', 'zb', '1', '总编', '1'); 21 INSERT INTO `login` VALUES ('3', 'sgr', '1', '审稿人', '1'); 22 INSERT INTO `login` VALUES ('4', 'jgr', '1', '校稿人', '1'); 23 INSERT INTO `login` VALUES ('5', 'tgr', '1', '投稿人', '1'); 24 INSERT INTO `login` VALUES ('6', 'yh', '1', '普通用户', '1'); 25 INSERT INTO `login` VALUES ('7', 'sgr1', '1', '审稿人', '1'); 26 INSERT INTO `login` VALUES ('8', 'sgr2', '1', '审稿人', '1'); 27 INSERT INTO `login` VALUES ('9', 'sgr3', '1', '审稿人', '1'); 28 INSERT INTO `login` VALUES ('10', 'sgr4', '1', '审稿人', '1'); 29 INSERT INTO `login` VALUES ('11', 'tgr1', '1', '投稿人', '1');
user(其中电话,邮箱均为随意输入的一些数据)
1 SET FOREIGN_KEY_CHECKS=0; 2 3 -- ---------------------------- 4 -- Table structure for `user` 5 -- ---------------------------- 6 DROP TABLE IF EXISTS `user`; 7 CREATE TABLE `user` ( 8 `id` int(11) NOT NULL AUTO_INCREMENT, 9 `name` varchar(255) DEFAULT NULL, 10 `password` varchar(255) DEFAULT NULL, 11 `identity` varchar(255) DEFAULT NULL, 12 `sex` varchar(255) DEFAULT NULL, 13 `phone` varchar(255) DEFAULT NULL, 14 `email` varchar(255) DEFAULT NULL, 15 `address` varchar(255) DEFAULT NULL, 16 `status` varchar(255) DEFAULT NULL, 17 PRIMARY KEY (`id`) 18 ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; 19 20 -- ---------------------------- 21 -- Records of user 22 -- ---------------------------- 23 INSERT INTO `user` VALUES ('1', 'admin', '1', '系统管理员', '男', '15694313452', '1234561932@qq.com', '中国', '1'); 24 INSERT INTO `user` VALUES ('2', 'zb', '1', '总编', '女', '19812345827', '2738472937@qq.com', '中国', '1'); 25 INSERT INTO `user` VALUES ('3', 'sgr', '1', '审稿人', '女', '19212345782', '27364891638@qq.com', '河北', '1'); 26 INSERT INTO `user` VALUES ('4', 'jgr', '1', '校稿人', '女', '17283649262', '239474957@qq.com', '山西', '1'); 27 INSERT INTO `user` VALUES ('5', 'tgr', '1', '投稿人', '男', '18293456789', '283940279@qq.com', '河北', '1'); 28 INSERT INTO `user` VALUES ('6', 'yh', '1', '普通用户', '男', '17283947373', '283945759@qq.com', '河南', '1'); 29 INSERT INTO `user` VALUES ('7', 'sgr1', '1', '审稿人', '女', '15623453452', '1234561932@qq.com', '中国', '1'); 30 INSERT INTO `user` VALUES ('8', 'sgr2', '1', '审稿人', '女', '15612343452', '1234561932@qq.com', '中国', '1'); 31 INSERT INTO `user` VALUES ('9', 'sgr3', '1', '审稿人', '女', '15667893452', '1234561932@qq.com', '中国', '1'); 32 INSERT INTO `user` VALUES ('10', 'sgr4', '1', '审稿人', '女', '12345313452', '1234561932@qq.com', '中国', '1'); 33 INSERT INTO `user` VALUES ('11', 'tgr1', '1', '投稿人', '男', '15678313452', '1234561932@qq.com', '中国', '1');
(三)代码
在src目录下新建四个包,分别为dao、database、servlet、util
在database下新建一个Database.java文件,用于连接数据库
Database.java (periodical为数据库名)
1 package databse; 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 9 public class Database { 10 public static final String url="jdbc:mysql://localhost:3306/periodical";//URL 11 public static final String user="root";//用户名 12 public static final String password="123";//密码 13 14 public static void main(String[] args) { 15 getConnection(); 16 } 17 18 /** 19 * 连接数据库 20 * @return 21 */ 22 public static Connection getConnection(){ 23 Connection conn=null; 24 try { 25 Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动 26 conn=DriverManager.getConnection(url, user, password); 27 System.out.println("数据库连接成功!"); 28 }catch(Exception e) { 29 e.printStackTrace(); 30 } 31 return conn; 32 } 33 34 /** 35 * 关闭数据库 36 */ 37 public static void close(Connection conn,PreparedStatement pstm) { 38 39 System.out.println("关闭SQL(conn,pstm)"); 40 if(pstm!=null) { 41 try { 42 pstm.close(); 43 }catch(SQLException e) { 44 e.printStackTrace(); 45 } 46 } 47 48 if(conn!=null) { 49 try { 50 conn.close(); 51 }catch(SQLException e) { 52 e.printStackTrace(); 53 } 54 } 55 56 } 57 58 public static void close(Connection conn,PreparedStatement pstm,ResultSet rs) { 59 60 System.out.println("关闭SQL(conn,pstm,rs)"); 61 if(pstm!=null) { 62 try { 63 pstm.close(); 64 }catch(SQLException e) { 65 e.printStackTrace(); 66 } 67 } 68 69 if(conn!=null) { 70 try { 71 conn.close(); 72 }catch(SQLException e) { 73 e.printStackTrace(); 74 } 75 } 76 77 if(rs!=null) { 78 try { 79 rs.close(); 80 }catch(SQLException e) { 81 e.printStackTrace(); 82 } 83 } 84 85 } 86 }
util包下新建Login.java及User.java两个文件
Login.java
1 package util; 2 3 public class Login { 4 private String name; 5 private String password; 6 private String identity; 7 private String status; 8 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public String getPassword() { 16 return password; 17 } 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 public String getStatus() { 22 return status; 23 } 24 public void setStatus(String status) { 25 this.status = status; 26 } 27 28 public Login() {} 29 public Login(String name,String password,String identity,String status) { 30 super(); 31 this.name=name; 32 this.password=password; 33 this.identity=identity; 34 this.status=status; 35 } 36 public String getIdentity() { 37 return identity; 38 } 39 public void setIdentity(String identity) { 40 this.identity = identity; 41 } 42 }
User.java
1 package util; 2 3 public class User { 4 private String name; 5 private String password; 6 private String identity; 7 private String sex; 8 private String phone; 9 private String email; 10 private String address; 11 private String status; 12 public User() { 13 super(); 14 } 15 public User(String name,String password,String identity,String sex,String phone,String email,String address,String status) { 16 super(); 17 this.name=name; 18 this.password=password; 19 this.sex=sex; 20 this.identity=identity; 21 this.phone=phone; 22 this.email=email; 23 this.address=address; 24 this.status=status; 25 } 26 public User(String name, String identity, String sex, String phone, String email, String address, 27 String status) { 28 // TODO Auto-generated constructor stub 29 super(); 30 this.name=name; 31 this.sex=sex; 32 this.identity=identity; 33 this.phone=phone; 34 this.email=email; 35 this.address=address; 36 this.status=status; 37 } 38 public User(String name) { 39 // TODO Auto-generated constructor stub 40 this.name=name; 41 } 42 public User(String name, String identity, String sex, String phone, String email, String address) { 43 // TODO Auto-generated constructor stub 44 this.name=name; 45 this.sex=sex; 46 this.identity=identity; 47 this.phone=phone; 48 this.email=email; 49 this.address=address; 50 } 51 public String getName() { 52 return name; 53 } 54 public void setName(String name) { 55 this.name = name; 56 } 57 public String getPassword() { 58 return password; 59 } 60 public void setPassword(String password) { 61 this.password = password; 62 } 63 public String getIdentity() { 64 return identity; 65 } 66 public void setIdentity(String identity) { 67 this.identity = identity; 68 } 69 public String getSex() { 70 return sex; 71 } 72 public void setSex(String sex) { 73 this.sex = sex; 74 } 75 public String getEmail() { 76 return email; 77 } 78 public void setEmail(String email) { 79 this.email = email; 80 } 81 public String getPhone() { 82 return phone; 83 } 84 public void setPhone(String phone) { 85 this.phone = phone; 86 } 87 public String getAddress() { 88 return address; 89 } 90 public void setAddress(String address) { 91 this.address = address; 92 } 93 public String getStatus() { 94 return status; 95 } 96 public void setStatus(String status) { 97 this.status = status; 98 } 99 }
index.jsp(即登录界面)
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>登录界面</title> 8 <script src=".//layui/layui.js"></script> 9 <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 10 <meta name="renderer" content="webkit"> 11 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 12 <meta name="viewport" 13 content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"> 14 <link rel="stylesheet" href=".//layui/css/layui.css" media="all"> 15 <link rel="stylesheet" href=".//style/admin.css" media="all"> 16 <link rel="stylesheet" href=".//style/login.css" media="all"> 17 </head> 18 <body> 19 <form action="LoginServlet" method="post"> 20 <div class="layadmin-user-login layadmin-user-display-show" 21 id="LAY-user-login" style="display: none;"> 22 23 <div class="layadmin-user-login-main"> 24 25 <div class="layadmin-user-login-box layadmin-user-login-header"> 26 <h2>期刊论文在线投稿审稿</h2> 27 </div> 28 29 <div 30 class="layadmin-user-login-box layadmin-user-login-body layui-form"> 31 32 <div class="layui-form-item"> 33 <label 34 class="layadmin-user-login-icon layui-icon layui-icon-username" 35 for="LAY-user-login-username"></label> <input type="text" 36 name="username" id="username" lay-verify="required" 37 placeholder="用户名" class="layui-input"> 38 </div> 39 <div class="layui-form-item"> 40 <label 41 class="layadmin-user-login-icon layui-icon layui-icon-password" 42 for="LAY-user-login-password"></label> <input type="password" 43 name="password" id="password" lay-verify="required" 44 placeholder="密码" class="layui-input"> 45 </div> 46 <div class="layui-form-item"> 47 <label class="layui-form-label">身份</label> 48 <div class="layui-input-inline"> 49 <select id="identity" name="identity"> 50 <option value="">请选择</option> 51 <option value="系统管理员">系统管理员</option> 52 <option value="总编">总编</option> 53 <option value="审稿人">审稿人</option> 54 <option value="校稿人">校稿人</option> 55 <option value="投稿人">投稿人</option> 56 <option value="普通用户">普通用户</option> 57 </select> 58 </div> 59 </div> 60 <div class="layui-form-item" style="margin-bottom: 20px;"> 61 <input type="checkbox" name="remember" lay-skin="primary" 62 title="记住密码"> <a href="#" 63 class="layadmin-user-jump-change layadmin-link" 64 style="margin-top: 7px;">忘记密码?</a> 65 </div> 66 <div class="layui-form-item"> 67 <input class="layui-btn layui-btn-fluid" type="submit" value="登录"> 68 </div> 69 <div class="layui-trans layui-form-item layadmin-user-login-other"> 70 <a href="Zhuce.jsp" 71 class="layadmin-user-jump-change layadmin-link">注册帐号</a> 72 </div> 73 </div> 74 </div> 75 </div> 76 </form> 77 <script> 78 layui.use('form', function() { 79 var form = layui.form; 80 form.render(); 81 }); 82 </script> 83 </body> 84 </html>
Zhuce.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>注册界面</title> 8 <script src=".//layui/layui.js"></script> 9 <meta name="renderer" content="webkit"> 10 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 11 <meta name="viewport" 12 content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"> 13 <link rel="stylesheet" href=".//layui/css/layui.css" media="all"> 14 <link rel="stylesheet" href=".//style/admin.css" media="all"> 15 <link rel="stylesheet" href=".//style/login.css" media="all"> 16 </head> 17 <body> 18 <form action="ZhuceServlet" method="post"> 19 <div class="layadmin-user-login layadmin-user-display-show" 20 id="LAY-user-login" style="display: none;"> 21 <div class="layadmin-user-login-main"> 22 <div class="layadmin-user-login-box layadmin-user-login-header"> 23 <h2>新用户注册界面</h2> 24 </div> 25 <div 26 class="layadmin-user-login-box layadmin-user-login-body layui-form"> 27 <div class="layui-form-item"> 28 <label class="layadmin-user-login-icon layui-icon layui-icon-username" 29 for="LAY-user-login-name"></label> <input type="text" name="name" 30 id="LAY-user-login-nickname" lay-verify="name" placeholder="用户名" 31 class="layui-input"> 32 </div> 33 <div class="layui-form-item"> 34 <label 35 class="layadmin-user-login-icon layui-icon layui-icon-password" 36 for="LAY-user-login-password"></label> <input type="password" 37 name="password" id="LAY-user-login-password" 38 lay-verify="password" placeholder="密码" class="layui-input"> 39 </div> 40 <div class="layui-form-item"> 41 <label 42 class="layadmin-user-login-icon layui-icon layui-icon-password" 43 for="LAY-user-login-repass"></label> <input type="password" 44 name="repass" id="LAY-user-login-repass" lay-verify="required" 45 placeholder="确认密码" class="layui-input"> 46 </div> 47 <div class="layui-input-item"> 48 <lable class="layui-form-label">性别</lable> 49 <input type="radio" name="sex" value="男" title="男" checked=""> 50 <input type="radio" name="sex" value="女" title="女"> 51 </div> 52 <div class="layui-form-item"> 53 <label class="layui-form-label">身份</label> 54 <div class="layui-input-inline"> 55 <select id="identity" name="identity"> 56 <option value="">请选择</option> 57 <option value="系统管理员">系统管理员</option> 58 <option value="总编">总编</option> 59 <option value="审稿人">审稿人</option> 60 <option value="校稿人">校稿人</option> 61 <option value="投稿人">投稿人</option> 62 <option value="普通用户">普通用户</option> 63 </select> 64 </div> 65 </div> 66 <div class="layui-form-item"> 67 <label 68 class="layadmin-user-login-icon layui-icon layui-icon-cellphone" 69 for="LAY-user-login-phone"></label> <input type="text" 70 name="phone" id="LAY-user-login-phone" lay-verify="phone" 71 placeholder="电话号码" class="layui-input"> 72 </div> 73 <div class="layui-form-item"> 74 <label 75 class="layadmin-user-login-icon layui-icon layui-icon-email" 76 for="LAY-user-login-email"></label> <input type="text" 77 name="email" id="LAY-user-login-email" lay-verify="email" 78 placeholder="电子邮箱" class="layui-input"> 79 </div> 80 <div class="layui-form-item"> 81 <label 82 class="layadmin-user-login-icon layui-icon layui-icon-location" 83 for="LAY-user-login-address"></label> <input type="text" 84 name="address" id="LAY-user-login-address" lay-verify="address" 85 placeholder="家庭住址" class="layui-input"> 86 </div> 87 <div class="layui-form-item"> 88 <input type="checkbox" name="agreement" lay-skin="primary" 89 title="同意用户协议" checked> 90 </div> 91 <div class="layui-form-item"> 92 <button class="layui-btn layui-btn-fluid" lay-submit 93 lay-filter="LAY-user-reg-submit">注 册</button> 94 </div> 95 <div class="layui-trans layui-form-item layadmin-user-login-other"> 96 <a href="index.jsp" 97 class="layadmin-user-jump-change layadmin-link layui-hide-xs">用已有帐号登入</a> 98 <a href="index.jsp" 99 class="layadmin-user-jump-change layadmin-link layui-hide-sm layui-show-xs-inline-block">登入</a> 100 </div> 101 </div> 102 </div> 103 </div> 104 </form> 105 </body> 106 </html>
后端登录注册功能的实现
在servlet包下新建LoginServlet.java、ZhuceServlet.java文件
LoginServlet.java
1 package servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import javax.servlet.http.HttpSession; 10 11 import dao.LoginDao; 12 13 /** 14 * Servlet implementation class LoginServlet 15 */ 16 @WebServlet("/LoginServlet") 17 public class LoginServlet extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public LoginServlet() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 LoginDao loginDao = new LoginDao(); 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 32 * response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) 35 throws ServletException, IOException { 36 // TODO Auto-generated method stub 37 // response.getWriter().append("Served at: ").append(request.getContextPath()); 38 doPost(request, response); 39 } 40 41 /** 42 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 43 * response) 44 */ 45 protected void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 // TODO Auto-generated method stub 48 // doGet(request, response); 49 50 request.setCharacterEncoding("utf-8"); 51 response.setCharacterEncoding("utf-8"); 52 53 System.out.println("进入LoginServlet"); 54 String username = request.getParameter("username"); 55 String password = request.getParameter("password"); 56 String identity = request.getParameter("identity"); 57 58 // 使用request对象的getSession()获取session,如果session不存在则创建一个 59 HttpSession session = request.getSession(); 60 // 将数据存储到session中 61 session.setAttribute("username", username); 62 session.setAttribute("identity", identity); 63 // 将数据取出 64 String name = (String) session.getAttribute("username"); 65 // 输出数据 66 System.out.println("session name;" + name); 67 68 if (identity.equals("系统管理员")) { 69 if (loginDao.login(username, password, identity, "1")) { 70 System.out.println("验证成功"); 71 request.getRequestDispatcher("Admin.jsp").forward(request, response); 72 } else { 73 System.out.println("验证失败,请重新登录!"); 74 request.getRequestDispatcher("index.jsp").forward(request, response); 75 } 76 } else if (identity.equals("总编")) { 77 if (loginDao.login(username, password, identity, "1")) { 78 System.out.println("验证成功"); 79 request.getRequestDispatcher("ChiefEditor.jsp").forward(request, response); 80 } else { 81 System.out.println("验证失败,请重新登录!"); 82 request.getRequestDispatcher("index.jsp").forward(request, response); 83 } 84 } else if (identity.equals("审稿人")) { 85 if (loginDao.login(username, password, identity, "1")) { 86 System.out.println("验证成功"); 87 request.getRequestDispatcher("Reviewer.jsp").forward(request, response); 88 } else { 89 System.out.println("验证失败,请重新登录!"); 90 request.getRequestDispatcher("index.jsp").forward(request, response); 91 } 92 } else if (identity.equals("校稿人")) { 93 if (loginDao.login(username, password, identity, "1")) { 94 System.out.println("验证成功"); 95 request.getRequestDispatcher("Proofread.jsp").forward(request, response); 96 } else { 97 System.out.println("验证失败,请重新登录!"); 98 request.getRequestDispatcher("index.jsp").forward(request, response); 99 } 100 } else if (identity.equals("投稿人")) { 101 if (loginDao.login(username, password, identity, "1")) { 102 System.out.println("验证成功"); 103 request.getRequestDispatcher("Contribute.jsp").forward(request, response); 104 } else { 105 System.out.println("验证失败,请重新登录!"); 106 request.getRequestDispatcher("index.jsp").forward(request, response); 107 } 108 } else if (identity.equals("普通用户")) { 109 if (loginDao.login(username, password, identity, "1")) { 110 System.out.println("验证成功"); 111 request.getRequestDispatcher("User.jsp").forward(request, response); 112 } else { 113 System.out.println("验证失败,请重新登录!"); 114 request.getRequestDispatcher("index.jsp").forward(request, response); 115 } 116 } else { 117 System.out.println("Error!"); 118 request.getRequestDispatcher("index.jsp").forward(request, response); 119 } 120 } 121 122 }
示例:Admin.jsp
ZhuceServlet.java
1 package servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import dao.ZhuceDao; 11 import util.User; 12 13 /** 14 * Servlet implementation class ZhuceServlet 15 */ 16 @WebServlet("/ZhuceServlet") 17 public class ZhuceServlet extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public ZhuceServlet() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 ZhuceDao dao = new ZhuceDao(); 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) 34 throws ServletException, IOException { 35 // TODO Auto-generated method stub 36 // response.getWriter().append("Served at: ").append(request.getContextPath()); 37 doPost(request, response); 38 } 39 40 /** 41 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 42 * response) 43 */ 44 protected void doPost(HttpServletRequest request, HttpServletResponse response) 45 throws ServletException, IOException { 46 // TODO Auto-generated method stub 47 // doGet(request, response); 48 49 request.setCharacterEncoding("utf-8"); 50 response.setCharacterEncoding("utf-8"); 51 52 System.out.println("进入ZhuceServlet"); 53 String name = request.getParameter("name"); 54 String password = request.getParameter("password"); 55 String identity = request.getParameter("identity"); 56 String sex = request.getParameter("sex"); 57 String phone = request.getParameter("phone"); 58 String email = request.getParameter("email"); 59 String address = request.getParameter("address"); 60 61 User user = new User(name, password, identity, sex, phone, email, address, "1"); 62 63 if (dao.Zhuce(user)) { 64 request.getRequestDispatcher("index.jsp").forward(request, response); 65 } else { 66 request.getRequestDispatcher("Zhuce.jsp").forward(request, response); 67 } 68 69 } 70 }
dao包下面新建LoginDao.java、ZhuceDao.java文件
LoginDao.java
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 import databse.Database; 8 import util.Login; 9 10 public class LoginDao { 11 public boolean login(String name,String password,String identity,String status) { 12 Connection conn=null; 13 PreparedStatement pstm=null; 14 ResultSet rs=null; 15 try { 16 conn=Database.getConnection(); 17 String sql="select * from login where name=? and password=? and identity=? and status=?"; 18 System.out.println(sql); 19 pstm=conn.prepareStatement(sql); 20 pstm.setString(1, name); 21 pstm.setString(2, password); 22 pstm.setString(3, identity); 23 pstm.setString(4, status); 24 rs=pstm.executeQuery(); 25 Login login=new Login(); 26 while(rs.next()) { 27 login.setName(rs.getString("name")); 28 login.setPassword(rs.getString("password")); 29 login.setIdentity(rs.getString("identity")); 30 login.setStatus(rs.getString("status")); 31 return true; 32 } 33 }catch(Exception e) { 34 e.printStackTrace(); 35 }finally { 36 //SQL执行完成后释放相关资源 37 Database.close(conn,pstm,rs); 38 } 39 return false; 40 } 41 }
ZhuceDao.java
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 6 import databse.Database; 7 import util.User; 8 9 public class ZhuceDao { 10 public boolean Zhuce(User user) { 11 Connection conn=null; 12 PreparedStatement pstm=null; 13 PreparedStatement ps=null; 14 boolean judge=false; 15 try { 16 conn=Database.getConnection(); 17 String sql="insert into user(name,password,identity,sex,phone,email,address,status) values(?,?,?,?,?,?,?,?)"; 18 String loginsql="insert into login(name,password,identity,status) values(?,?,?,?)"; 19 System.out.println(sql); 20 System.out.println(loginsql); 21 pstm=conn.prepareStatement(sql); 22 ps=conn.prepareStatement(loginsql); 23 24 pstm.setString(1, user.getName()); 25 pstm.setString(2, user.getPassword()); 26 pstm.setString(3, user.getIdentity()); 27 pstm.setString(4, user.getSex()); 28 pstm.setString(5, user.getPhone()); 29 pstm.setString(6, user.getEmail()); 30 pstm.setString(7, user.getAddress()); 31 pstm.setString(8, user.getStatus()); 32 33 ps.setString(1, user.getName()); 34 ps.setString(2, user.getPassword()); 35 ps.setString(3, user.getIdentity()); 36 ps.setString(4, user.getStatus()); 37 38 //执行插入操作 39 int num=pstm.executeUpdate(); 40 int num1=ps.executeUpdate(); 41 if(num>0&&num1>0) { 42 System.out.println("注册成功"); 43 judge=true; 44 }else { 45 System.out.println("注册失败"); 46 judge=false; 47 } 48 }catch(Exception e) { 49 e.printStackTrace(); 50 }finally { 51 //SQL执行完成后释放相关资源 52 Database.close(conn,pstm); 53 } 54 return judge; 55 } 56 }
完成以上步骤,该系统的登录注册功能就实现啦!