简单的利用Layui来实现登录功能 您所在的位置:网站首页 用layui做一个注册页面 简单的利用Layui来实现登录功能

简单的利用Layui来实现登录功能

2024-04-07 17:41| 来源: 网络整理| 查看: 265

“管理子系统”效果如下:

主页:

 

 管理员功能页:

 

注册界面(此界面仅是一个界面,暂无功能的实现)

今天只写了这些效果,具体代码情况如下:

前端目录结构如下:

代码如下:

1 3 4 5 6 7 登录界面 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 管理子系统 24 25 26 27 28 29 30 31 32 33 34 35 36 身份: 37 38 39 40 41 42 43 44 忘记密码? 45 46 47 48 49 50 注册帐号 51 52 53 54 55 56 57 layui.use('form',function(){ 58 var form = layui.form; 59 form.render(); 60 }); 61 62 63

Zhuce.jsp

1 3 4 5 6 7 注册界面 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 新用户注册界面 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 注 册 42 43 44 用已有帐号登入 45 登入 46 47 48 49 50 51 52

Admin.jsp

1 3 4 5 6 7 管理员功能页 8 9 10 11 12 13 //JavaScript代码区域 14 layui.use('element', function(){ 15 var element = layui.element; 16 }); 17 18 19 20 21 22 23 24 25 29 30 31 32 33 34 35 36 增加 37 删除 38 39 40 41 42 43 45 46 47 48 49 50 51 52 53 54

后端目录结构:

 

代码如下:

Admin.java

1 public class Admin { 2 3 private String name; 4 private String password; 5 public String getName() { 6 return name; 7 } 8 public void setName(String name) { 9 this.name = name; 10 } 11 public String getPassword() { 12 return password; 13 } 14 public void setPassword(String password) { 15 this.password = password; 16 } 17 18 }

DBUtil.java

1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 public class DBUtil { 8 public static final String url="jdbc:mysql://localhost:3306/manager";//URL,manager为新建的数据库名 9 public static final String user="用户名";//用户名 10 public static final String password="密码";//密码 11 12 /** 13 * 连接数据库 14 * @return 15 */ 16 public static Connection getConnection(){ 17 Connection conn=null; 18 try { 19 Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动 20 conn=DriverManager.getConnection(url, user, password); 21 System.out.println("数据库连接成功!"); 22 }catch(Exception e) { 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 28 /** 29 * 关闭数据库 30 */ 31 public static void close(Connection conn,PreparedStatement pstm) { 32 33 System.out.println("关闭SQL(conn,pstm)"); 34 if(pstm!=null) { 35 try { 36 pstm.close(); 37 }catch(SQLException e) { 38 e.printStackTrace(); 39 } 40 } 41 42 if(conn!=null) { 43 try { 44 conn.close(); 45 }catch(SQLException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 } 51 52 public static void close(Connection conn,PreparedStatement pstm,ResultSet rs) { 53 54 System.out.println("关闭SQL(conn,pstm,rs)"); 55 if(pstm!=null) { 56 try { 57 pstm.close(); 58 }catch(SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 if(conn!=null) { 64 try { 65 conn.close(); 66 }catch(SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 71 if(rs!=null) { 72 try { 73 rs.close(); 74 }catch(SQLException e) { 75 e.printStackTrace(); 76 } 77 } 78 79 } 80 81 public static void main(String[] args) { 82 getConnection(); 83 } 84 }

LoginServlet.java

1 import java.io.IOException; 2 import javax.servlet.ServletException; 3 import javax.servlet.annotation.WebServlet; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import javax.servlet.http.HttpSession; 8 9 import dao.LoginDao; 10 import util.Admin; 11 import util.Student; 12 import util.Teacher; 13 14 15 /** 16 * Servlet implementation class LoginServlet 17 */ 18 @WebServlet("/LoginServlet") 19 public class LoginServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public LoginServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 LoginDao loginDao = new LoginDao(); 31 32 /** 33 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) 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 response) 43 */ 44 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 // TODO Auto-generated method stub 46 //doGet(request, response); 47 request.setCharacterEncoding("utf-8"); 48 response.setCharacterEncoding("utf-8"); 49 System.out.println("进入LoginServlet"); 50 String username=request.getParameter("username"); 51 String password=request.getParameter("password"); 52 String entity=request.getParameter("entity"); 53 System.out.println(username+"的密码是:"+password+",身份是:"+entity); 54 55 //使用request对象的getSession()获取session,如果session不存在则创建一个 56 HttpSession session = request.getSession(); 57 //将数据存储到session中 58 session.setAttribute("username", username); 59 //将数据取出 60 String name=(String)session.getAttribute("username"); 61 //输出数据 62 System.out.println("session name;"+name); 63 64 if(entity.equals("管理员")) { 65 Admin admin=new Admin(); 66 admin=loginDao.LoginAdmin(username, password); 67 System.out.println("验证成功"); 68 System.out.println(admin.getName()+":"+admin.getPassword()); 69 request.getRequestDispatcher("Admin.jsp").forward(request,response); 70 }else { 71 System.out.println("entity Error!"); 72 request.getRequestDispatcher("Login.jsp").forward(request,response); 73 } 74 } 75 }

LoginDao.java

1 import java.sql.Connection; 2 import java.sql.PreparedStatement; 3 import java.sql.ResultSet; 4 5 import db.DBUtil; 6 import util.Admin; 7 import util.Student; 8 import util.Teacher; 9 10 public class LoginDao { 11 public Admin LoginAdmin(String name,String password) { 12 Connection conn=null; 13 PreparedStatement pstm=null; 14 ResultSet rs=null; 15 try { 16 conn=DBUtil.getConnection(); 17 String sql="select * from s_admin where name=? and password=?";//其中s_admin为数据库的表名 18 System.out.println(sql); 19 pstm=conn.prepareStatement(sql); 20 pstm.setString(1, name); 21 pstm.setString(2, password); 22 rs=pstm.executeQuery(); 23 Admin admin=new Admin(); 24 while(rs.next()) { 25 admin.setName(rs.getString("name")); 26 admin.setPassword(rs.getString("password")); 27 System.out.println("Admin-Name:"+admin.getName()); 28 System.out.println("Admin-Password:"+admin.getPassword()); 29 return admin; 30 } 31 }catch(Exception e) { 32 e.printStackTrace(); 33 }finally { 34 //SQL执行完成后释放相关资源 35 DBUtil.close(conn,pstm,rs); 36 } 37 return null; 38 } 39 }

其中所需要的Layui包、style包及mysql包均可在网上下载



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有