jsp之简单的用户登录系统(纯jsp)
来源:韩顺平之jsp视频教程
知识点:
1:如何在jsp页面间跳转
2:jsp如何去操作数据库
3:jsp中如何显示数据/如何分页
1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'login.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body bgcolor="green"><!-- 背景色为绿色 --> 26 <center> 27 用户登录<br> 28 <hr/> 29 <form action="loginCl.jsp" method="post"> 30 用户名:<input type="text" name="username"><br/> 31 <!-- 空格 --> 32 密 码:<input type="password" name="passwd"><br/> 33 <input type="submit" value="登录"> 34 <input type="reset" value="重置"> 35 </form> 36 </center> 37 </body> 38 </html>
1 <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'loginCl.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <% 27 //接受用户名和密码,完成对用户的验证 28 String u=request.getParameter("username"); 29 String p=request.getParameter("passwd"); 30 31 //到数据库中去验证用户 32 //1.加载驱动 33 Class.forName("com.mysql.jdbc.Driver"); 34 //2.得到链接 35 Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/spdb1","root","lszget"); //建立数据库链接 36 37 //3.创建Statement 38 Statement stmt=ct.createStatement(); 39 40 //4.查询 41 ResultSet rs=stmt.executeQuery("select passwd from users where username='"+u+"'"); 42 43 //根据结果判断 44 if(rs.next()){ 45 //说明用户存在 46 if(rs.getString(1).equals(p)){ 47 48 //一定合法 49 response.sendRedirect("wel.jsp?user="+u); 50 51 }else{ 52 //密码错误 53 response.sendRedirect("login.jsp?errNo=1"); 54 } 55 }else{ 56 //说明用户名错了 57 response.sendRedirect("login.jsp?errNo=2"); 58 } 59 60 61 62 63 /* 64 //验证 65 if(u.equals("shunping")&&p.equals("123")){ 66 67 //合法,跳转wel.jsp 68 //如何而将loginCl.jsp得到的数据传给下一个页面 69 //1.cookie 2.session 3.response.sendRedirect 70 response.sendRedirect("wel.jsp?user="+u); 71 }else{ 72 //不合法,跳转到login.jsp 73 response.sendRedirect("login.jsp"); 74 } 75 */ 76 77 %> 78 79 </body> 80 </html>
1 <%@page import="javax.sound.midi.SysexMessage"%> 2 <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP 'wel.jsp' starting page</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 26 <body> 27 28 登陆成功!!欢迎<%=request.getParameter("user")%><br/> 29 <a href="login.jsp">返回重新登录</a> 30 <hr/> 31 <h1> 用户信息列表</h1> 32 33 <% 34 //定义四个分页变量,作用域在整个页面 35 int pageSize=3;//每页显示多少条记录 36 int pageNow=1;//默认显示第一页 37 int pageCount=0;//该值是通过pageSize和rowCount计算 38 int rowCount=0;//该值从数据库中查询 39 40 //接受用户希望显示的页数(pageNow) 41 String s_pageNow=request.getParameter("pageNow"); 42 if(s_pageNow!=null){ 43 //接受到pageNow 44 pageNow =Integer.parseInt(s_pageNow); 45 System.out.println("pageNow =Integer.parseInt(s_pageNow);"); 46 } 47 48 //查询得到rowCount 49 //1.加载驱动 50 Class.forName("com.mysql.jdbc.Driver"); 51 //2.得到链接 52 Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/spdb1","root","lszget"); 53 54 //3.创建Statement 55 Statement stmt=ct.createStatement(); 56 57 //4.查询 58 ResultSet rs=stmt.executeQuery("select count(*) from users "); 59 //请注意一定要rs.next() 60 if(rs.next()){ 61 62 rowCount=rs.getInt(1); 63 64 65 } 66 //计算pageCount 67 if(rowCount%pageSize==0){ 68 pageCount=rowCount/pageSize; 69 }else{ 70 71 pageCount=rowCount/pageSize+1; 72 73 } 74 75 //查询出需要显示的记录 76 /*实现类似top的功能,在SQL Server中和MySQL中使用到的SQL语句是不同的 77 1、在SQL Server中,我们使用 select top N * from tablename来查询tablename表中前N条记录 78 select * from users limit "+pageSize*(pageNow-1)+" ,"+pageSize+" 79 2、在MySQL中,则使用select * from users limit M,N ,M表示从第M+1条记录开始,N表示返回N条记录 80 rs=stmt.executeQuery("select "+pageSize+" * from users where userId not in (select top " 81 +pageSize*(pageNow-1)+" userId from users)"); 82 数据库数据成倍增加 83 INSERT INTO users(username,passwd,email,grade)SELECT username,passwd,email,grade FROM users 84 */ 85 86 rs=stmt.executeQuery("select * from users limit "+pageSize*(pageNow-1)+" ,"+pageSize+" "); 87 88 //显示 89 %> 90 91 92 <table border="1"> 93 <!-- TableRow 对象代表一个 HTML 表格行 --> 94 <tr><td>用户id</td><td>用户名字</td><td>用户密码</td><td>电子邮件</td><td>用户级别</td></tr> 95 96 97 98 <% 99 while(rs.next()){ 100 %> 101 102 <tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td> 103 <td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td> 104 <td><%=rs.getInt(5) %></td></tr> 105 <% 106 107 } 108 109 %> 110 </table> 111 112 <% 113 //上一页 114 if(pageNow!=1){ 115 out.println("<a href=wel.jsp?pageNow="+(pageNow-1)+">[上一页]</a>"); 116 } 117 118 //显示超链接 119 for(int i=1;i<=pageCount;i++){ 120 //韩顺平代码: out.println("<a href=#wel.jsp?pageNow="+i+" >["+i+"]</a>"); 121 //加#有问题,??? 122 out.println("<a href=wel.jsp?pageNow="+i+" >["+i+"]</a>"); 123 124 125 } 126 //下一页 127 if(pageNow!=pageCount){ 128 out.println("<a href=wel.jsp?pageNow="+(pageNow+1)+">[下一页]</a>"); 129 } 130 131 %> 132 133 134 135 </body> 136 </html>
努力,生活总会有改变