首页 > 代码库 > JSP程序编写用户注册登录系统,登录成功后显示此用户的登录次数
JSP程序编写用户注册登录系统,登录成功后显示此用户的登录次数
刚来公司实习,老大让我们分别用JSP和servlet写一个用户的登录和注册系统。经过这两天的奋斗,终于算是搞定了(至少我这么认为······)
这一篇贴一下用JSP写的代码,下一篇贴一下用servlet写的~~
文件位置和类库位置如图:
代码如下:
login.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 4 <html> 5 <head> 6 <title>login</title> 7 </head> 8 9 <body> 10 <table height="62%" width="100%"> 11 <td align="center" valign="middle"> 12 <div align="center"> 13 <form method="post" action="CheckUser.jsp"> 14 <image src="http://www.mamicode.com/image/logo.gif"> 15 <br></br> 16 Username: <input type="text" name="username"><br> 17 Password: <input type="password" name="pwd"><br> 18 <br> 19 <input type="submit" name="submit" value="http://www.mamicode.com/submit"> 20 <input type="reset" name="reset" value="http://www.mamicode.com/reset"> 21 <input type="button" onClick="window.location.href=http://www.mamicode.com/‘register.jsp‘" value="http://www.mamicode.com/register"> 22 </form> 23 </div> 24 </td> 25 </table> 26 </body> 27 28 </html>
register.jsp (对html还不太熟,就用了一堆的<tr></tr>···)
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>register</title> 9 </head> 10 11 <body> 12 <form action="CheckRegister.jsp" method=post> 13 <table align="center"> 14 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 15 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 16 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 17 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 18 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 19 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 20 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 21 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 22 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 23 <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr> 24 <tr><td align="center"><font size=2>Username: </font></td> 25 <td><Input type="text" name="username1"></td> 26 </tr><tr></tr> 27 <tr><td align="center"><font size=2>Password: </font></td> 28 <td><Input type="password" name="password1"></td> 29 </tr><tr></tr> 30 <tr><td align="center"><font size=2>Comfirepassword: </font></td> 31 <td><Input type="password" name="password2"></td> 32 </tr><tr></tr> 33 </table> 34 <br> 35 <div align="center"> 36 <Input type="submit" name="reg" value="http://www.mamicode.com/register"> 37 <Input type="reset" name="reset" value="http://www.mamicode.com/reset"> 38 </div> 39 </form> 40 </body> 41 42 </html>
CheckUser.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <%@ page import="java.sql.*" %> 4 5 <% 6 /* 7 @ date 2014-05-22 8 @ author asif 9 @ 作用:检查用户登录信息 10 */ 11 %> 12 13 <% 14 //pass parameters 15 String username = request.getParameter("username"); 16 String password = request.getParameter("pwd"); 17 18 //out.println(username+password); 19 20 //如果输入的用户名为空或只有空格的话,看做恶性提交,刷新原页面 21 if(username.trim().equals("")) 22 response.sendRedirect("login.jsp"); 23 else 24 { 25 //连接数据库 26 final String name = "root"; 27 final String passwd = "123456"; 28 String str=""; 29 String url = "jdbc:mysql://127.0.0.1:3306/test"; 30 try 31 { 32 Class.forName("com.mysql.jdbc.Driver").newInstance(); 33 } 34 catch(Exception e) 35 { 36 e.getStackTrace(); 37 } 38 try 39 { 40 Connection conn = DriverManager.getConnection(url, name, passwd); 41 ResultSet res; 42 PreparedStatement pstmt; 43 String sql = "select * from user where username=?"; 44 pstmt = conn.prepareStatement(sql); 45 pstmt.setString(1, username); 46 res = pstmt.executeQuery(); 47 //flag的值为true代表用户名和密码匹配正确,值为false的话表示没有匹配 48 boolean flag = false; 49 while(res.next()) 50 { 51 String Upasswd = res.getString("password"); 52 if(Upasswd.equals(password)) 53 { 54 int total = res.getInt("totlogin"); 55 total++; 56 out.println("This is your " + total + " times login."); 57 sql = "update user set totlogin=? where username=?"; 58 pstmt = conn.prepareStatement(sql); 59 pstmt.setInt(1, total); 60 pstmt.setString(2, username); 61 pstmt.executeUpdate(); 62 flag = true; 63 break; 64 } 65 } 66 //登陆成功 67 if(flag) 68 str = "Login Success!\n"; 69 //登录失败,然后跳回登陆页面 70 else 71 response.sendRedirect("fail.html"); 72 conn.close(); 73 } 74 catch (SQLException e) 75 { 76 // TODO Auto-generated catch block 77 //e.printStackTrace(); 78 str = e.toString(); 79 } 80 out.print(str); 81 } 82 %> 83 84 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 85 86 <html> 87 <head> 88 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 89 <title>Insert title here</title> 90 </head> 91 92 <body> 93 94 </body> 95 96 </html>
CheckRegister.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <%@ page import="java.sql.*" %> 4 <%@ page import="java.util.Date" %> 5 <%@ page import="java.util.Calendar" %> 6 <%@ page import="java.text.SimpleDateFormat" %> 7 8 <% 9 /* 10 @ date 2014-05-22 11 @ author asif 12 @ 作用:检查用户注册输入的用户名,密码的合法性 13 */ 14 %> 15 16 <% 17 //传入用户输入的值,username1为用户名,password1为密码,password2为第二次输入的密码 18 String username = request.getParameter("username1"); 19 String password1 = request.getParameter("password1"); 20 String password2 = request.getParameter("password2"); 21 22 //如果用户名为空或只有空格,返回警告页面,然后自动调回注册页面 23 if(username.trim().equals("")) 24 { 25 response.sendRedirect("null.html"); 26 } 27 //如果两次输入的密码不相等,弹出警告页面,返回注册页面重新输入 28 else if(!password1.equals(password2)) 29 { 30 response.sendRedirect("WrongP.html"); 31 } 32 else 33 { 34 //合法的话,连接数据库 35 final String name = "root"; 36 final String passwd = "123456"; 37 String str=""; 38 String url = "jdbc:mysql://127.0.0.1:3306/test"; 39 try 40 { 41 Class.forName("com.mysql.jdbc.Driver").newInstance(); 42 } 43 catch(Exception e) 44 { 45 e.getStackTrace(); 46 } 47 try 48 { 49 Connection conn = DriverManager.getConnection(url, name, passwd); 50 ResultSet res; 51 PreparedStatement pstmt; 52 String sql = "select * from user where username=?"; 53 pstmt = conn.prepareStatement(sql); 54 pstmt.setString(1, username); 55 res = pstmt.executeQuery(); 56 boolean flag = false; 57 //判断用户名是否存在,如果存在的话,给出相应的信息,返回注册页面 58 while(res.next()) 59 { 60 flag = true; 61 } 62 //用户名已存在 63 if(flag) 64 response.sendRedirect("HaveU.html"); 65 //用户名不存在 66 else 67 { 68 sql = "insert user(username,password,totlogin,date) values(?,?,?,?)"; 69 pstmt = conn.prepareStatement(sql); 70 pstmt.setString(1, username); 71 pstmt.setString(2, password1); 72 pstmt.setInt(3, 0); 73 Date now = new Date(); 74 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 75 String strdate = dateFormat.format(now); 76 pstmt.setString(4, strdate); 77 pstmt.executeUpdate(); 78 response.sendRedirect("Rsuccess.html"); 79 } 80 conn.close(); 81 } 82 catch (SQLException e) 83 { 84 // TODO Auto-generated catch block 85 //e.printStackTrace(); 86 str = e.toString(); 87 } 88 } 89 %> 90 91 92 93 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 94 95 <html> 96 <head> 97 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 98 <title>Insert title here</title> 99 </head> 100 101 <body> 102 103 </body> 104 105 </html>
下面是几个提示页面:
fail.html
1 <html> 2 <head> 3 <meta http-equiv="Refresh" content="1;url= login.jsp"> 4 <title>Insert title here</title> 5 </head> 6 <body> 7 Username or Password wrong 8 </body> 9 </html>
HaveU.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Refresh" content="2;url= register.jsp"> 5 <title>Wait 2s</title> 6 </head> 7 <body> 8 亲,该用户名已被注册,还是重新选一个用户名注册吧 9 </body> 10 </html>
null.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Refresh" content="2;url= register.jsp"> 5 <title>Wait 2s</title> 6 </head> 7 <body> 8 Username is not null 9 </body> 10 </html>
Rsuccess.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Refresh" content="1;url= login.jsp"> 5 <title>Wait 2s</title> 6 </head> 7 <body> 8 恭喜您注册成功,马上为您跳转到登录页面 9 </body> 10 </html>
success.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <%@ import java.sql.* %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 Login successful! 12 </body> 13 </html>
WrongP.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Refresh" content="2;url= register.jsp"> 5 <title>Wait 2s</title> 6 </head> 7 <body> 8 两次密码不一样,请重新注册 9 </body> 10 </html>
运行时把login.jsp当做主页就可以了,然后图片的话,在图中相应位置上面新建一个image文件夹,里面随便放图片就好了。
数据库的话,我用的数据库名为test,表名为user。里面有四个段,如下
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。