首页 > 代码库 > servlet编写用户注册登录系统,登录成功后显示此用户的登录次数
servlet编写用户注册登录系统,登录成功后显示此用户的登录次数
承接上一篇···
代码如下:
CheckUser.java
1 package com.wyd.www; 2 import java.io.*; 3 import java.sql.*; 4 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 8 /** 9 * 10 * @date 2014-05-21 11 * @author asif 12 * @检测用户名哥密码的合法性 13 * 14 */ 15 16 @SuppressWarnings("serial") 17 public class CheckUser extends HttpServlet{ 18 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 19 { 20 //HttpSession session = request.getSession(); 21 response.setContentType("Text/html;charset=GB2312"); 22 request.setCharacterEncoding("GB2312"); 23 String name = request.getParameter("username"); 24 String passwd = request.getParameter("pwd"); 25 int totlogin = 0; 26 //获得输出流 27 PrintWriter out = response.getWriter(); 28 User account = new User(); 29 account.SetPassword(passwd); 30 account.SetUsername(name); 31 //判断用户名是否为空 32 if(name.trim().equals("")) 33 { 34 out.println("<html>"); 35 out.println("<head>"); 36 out.println("<meta http-equiv=\"Refresh\" content=\"2;url= login.jsp\">"); 37 out.println("<title>Wait 2s</title>"); 38 out.println("</head>"); 39 out.println("<body>用户名不能为空</body>"); 40 out.println("</html>"); 41 } 42 else 43 { 44 //连接数据库查看用户名是否存在和其密码是否正确 45 Connection conn = MysqlConnection(); 46 PreparedStatement pstmt = null; 47 ResultSet rset = null; 48 String sql = "select * from user where username=?"; 49 try { 50 pstmt = conn.prepareStatement(sql); 51 pstmt.setString(1, name); 52 rset = pstmt.executeQuery(); 53 } catch (SQLException e1) { 54 // TODO Auto-generated catch block 55 e1.printStackTrace(); 56 } 57 //flag标记用户名和密码是否对应成功,如果对应正确为true,否则为false 58 boolean flag = true; 59 try { 60 if(rset == null) 61 { 62 //out.close(); 63 //conn.close(); 64 /*out.println("<html>"); 65 out.println("<head>"); 66 out.println("<meta http-equiv=\"Refresh\" content=\"2;url= login.jsp\">"); 67 out.println("<title>Wait 2s</title>"); 68 out.println("</head>"); 69 out.println("<body>用户名不存在</body>"); 70 out.println("</html>");*/ 71 } 72 else 73 { 74 while(rset.next()) 75 { 76 String Passwordtemp = rset.getString("password"); 77 if(passwd.equals(Passwordtemp)) 78 { 79 totlogin = rset.getInt("totlogin"); 80 totlogin++; 81 account.SetTotlogin(totlogin); 82 //登录次数加一 83 sql = "update user set totlogin=? where username=?"; 84 pstmt = conn.prepareStatement(sql); 85 pstmt.setInt(1, totlogin); 86 pstmt.setString(2, name); 87 pstmt.executeUpdate();//更新完成 88 flag = false; 89 break; 90 } 91 } 92 } 93 conn.close(); 94 } catch (SQLException e) { 95 // TODO Auto-generated catch block 96 e.printStackTrace(); 97 } 98 //根据上面得到的flag的值判断是否正确,并向不同的页面跳转 99 if(!flag) 100 { 101 out.println("<html>"); 102 out.println("<head>"); 103 out.println("<title>login successful</title>"); 104 out.println("</head>"); 105 out.println("<body>登陆成功!这是你的第 "+totlogin+" 次登录</body>"); 106 out.println("</html>"); 107 } 108 else 109 { 110 out.println("<html>"); 111 out.println("<head>"); 112 out.println("<meta http-equiv=\"Refresh\" content=\"2;url= login.jsp\">"); 113 out.println("<title>Wait 2s</title>"); 114 out.println("</head>"); 115 out.println("<body>用户名或密码错误</body>"); 116 out.println("</html>"); 117 } 118 } 119 out.close(); 120 } 121 //连接数据库函数 122 public static Connection MysqlConnection() 123 { 124 String username = "root"; 125 String userpasswd = "123456"; 126 String url = "jdbc:mysql://127.0.0.1:3306/test"; 127 try 128 { 129 Class.forName("com.mysql.jdbc.Driver").newInstance(); 130 try 131 { 132 Connection contemp = DriverManager.getConnection(url, username, userpasswd); 133 return contemp; 134 } 135 catch (SQLException e) 136 { 137 // TODO Auto-generated catch block 138 e.printStackTrace(); 139 } 140 } 141 catch (InstantiationException e) 142 { 143 // TODO Auto-generated catch block 144 e.printStackTrace(); 145 } 146 catch (ClassNotFoundException e) { 147 // TODO Auto-generated catch block 148 e.printStackTrace(); 149 } catch (IllegalAccessException e1) { 150 // TODO Auto-generated catch block 151 e1.printStackTrace(); 152 } 153 return null; 154 } 155 }
User.java
1 package com.wyd.www; 2 3 /** 4 * 5 * @date 2014-05-21 6 * @author asif 7 * 用户登录系统-用户账户类 8 * 9 */ 10 11 public class User { 12 private static String username = ""; 13 private static String password = ""; 14 private static int totlogin = 0; 15 //获得密码 16 public String GetPassword() 17 { 18 return password; 19 } 20 //传入密码 21 @SuppressWarnings("static-access") 22 public void SetPassword(String password) 23 { 24 this.password = password; 25 } 26 //获取用户名 27 public String GetUsername() 28 { 29 return username; 30 } 31 //传入用户名 32 @SuppressWarnings("static-access") 33 public void SetUsername(String username) 34 { 35 this.username = username; 36 } 37 //获取登录次数 38 public int GetTotlogin() 39 { 40 return totlogin; 41 } 42 //传入用户的登录次数 43 @SuppressWarnings("static-access") 44 public void SetTotlogin(int totlogin) 45 { 46 this.totlogin = totlogin; 47 } 48 }
CheckRegister.java
1 package com.yqr.www; 2 3 import java.io.*; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.text.SimpleDateFormat; 10 import java.util.*; 11 12 import javax.servlet.*; 13 import javax.servlet.http.*; 14 15 /** 16 * 17 * @date 2014-05-23 18 * @author asif 19 * 检测用户注册信息是否合法 20 * 21 */ 22 23 @SuppressWarnings("serial") 24 public class CheckRegister extends HttpServlet implements Servlet{ 25 public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException 26 { 27 response.setContentType("Text/html;charset=GB2312"); 28 request.setCharacterEncoding("GB2312"); 29 String name = request.getParameter("username1"); 30 String passwd1 = request.getParameter("password1"); 31 String passwd2 = request.getParameter("password2"); 32 int totlogin = 0; 33 //获得输出流 34 PrintWriter out = response.getWriter(); 35 if(name.trim().equals("")) 36 { 37 out.println("<html>"); 38 out.println("<head>"); 39 out.println("<meta http-equiv=\"Refresh\" content=\"2;url= login.jsp\">"); 40 out.println("<title>Wait 2s</title>"); 41 out.println("</head>"); 42 out.println("<body>用户名不能为空</body>"); 43 out.println("</html>"); 44 } 45 else if(!passwd1.equals(passwd2)) 46 { 47 out.println("<html>"); 48 out.println("<head>"); 49 out.println("<meta http-equiv=\"Refresh\" content=\"2;url= register.jsp\">"); 50 out.println("<title>Wait 2s</title>"); 51 out.println("</head>"); 52 out.println("<body>两次密码不一样,请重新注册</body>"); 53 out.println("</html>"); 54 } 55 else 56 { 57 //连接数据库查看用户名是否存在和其密码是否正确 58 Connection conn = MysqlConnection(); 59 PreparedStatement pstmt = null; 60 ResultSet rset = null; 61 String sql = "select * from user where username=?"; 62 try { 63 pstmt = conn.prepareStatement(sql); 64 pstmt.setString(1, name); 65 rset = pstmt.executeQuery(); 66 } catch (SQLException e1) { 67 // TODO Auto-generated catch block 68 e1.printStackTrace(); 69 } 70 try { 71 if(!rset.next()) 72 { 73 sql = "insert user(username,password,totlogin,date) values(?,?,?,?)"; 74 pstmt = conn.prepareStatement(sql); 75 try { 76 pstmt.setString(1, name); 77 pstmt.setString(2, passwd1); 78 pstmt.setInt(3, totlogin); 79 Date now = new Date(); 80 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 81 String strdate = dateFormat.format(now); 82 pstmt.setString(4, strdate); 83 pstmt.executeUpdate(); 84 } catch (SQLException e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } 88 //关闭数据库 89 conn.close(); 90 out.println("<html>"); 91 out.println("<head>"); 92 out.println("<meta http-equiv=\"Refresh\" content=\"1;url= login.jsp\">"); 93 out.println("<title>Wait 1s</title>"); 94 out.println("</head>"); 95 out.println("<body>恭喜您注册成功,马上为您跳转到登陆页面</body>"); 96 out.println("</html>"); 97 } 98 else 99 { 100 out.println("<html>"); 101 out.println("<head>"); 102 out.println("<meta http-equiv=\"Refresh\" content=\"2;url= register.jsp\">"); 103 out.println("<title>Wait 2s</title>"); 104 out.println("</head>"); 105 out.println("<body>亲,用户名已存在,您还是换一个进行注册吧</body>"); 106 out.println("</html>"); 107 } 108 } catch (SQLException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 } 112 } 113 } 114 115 //连接数据库函数 116 public static Connection MysqlConnection() 117 { 118 String username = "root"; 119 String userpasswd = "123456"; 120 String url = "jdbc:mysql://127.0.0.1:3306/test"; 121 try 122 { 123 Class.forName("com.mysql.jdbc.Driver").newInstance(); 124 try 125 { 126 Connection contemp = DriverManager.getConnection(url, username, userpasswd); 127 return contemp; 128 } 129 catch (SQLException e) 130 { 131 // TODO Auto-generated catch block 132 e.printStackTrace(); 133 } 134 } 135 catch (InstantiationException e) 136 { 137 // TODO Auto-generated catch block 138 e.printStackTrace(); 139 } 140 catch (ClassNotFoundException e) { 141 // TODO Auto-generated catch block 142 e.printStackTrace(); 143 } catch (IllegalAccessException e1) { 144 // TODO Auto-generated catch block 145 e1.printStackTrace(); 146 } 147 return null; 148 } 149 }
login.jsp
1 <!-- 2 /** 3 * 4 * @date 2014-05-21 5 * @author asif 6 * @login 7 * 8 * 9 /--> 10 11 <%@ page contentType="text/html;charset=gb2312" language="java" %> 12 <%@ page import="java.sql.*" %> 13 <%@ page import="java.util.*" %> 14 15 <html> 16 17 <head> 18 <title>Login</title> 19 </head> 20 21 <body> 22 <table height="62%" width="100%"> 23 <td align="center" valign="middle"> 24 <div align="center"> 25 <form method="post" action="login"> 26 <image src="http://www.mamicode.com/image/logo.gif"> 27 <br></br> 28 Username: <input type="text" name="username"><br> 29 Password: <input type="password" name="pwd"><br> 30 <br> 31 <input type="submit" name="submit" value="http://www.mamicode.com/submit"> 32 <input type="reset" name="reset" value="http://www.mamicode.com/reset"> 33 <input type="button" onClick="window.location.href=http://www.mamicode.com/‘register.jsp‘" value="http://www.mamicode.com/register"> 34 </form> 35 </div> 36 </td> 37 </table> 38 </body> 39 40 </html>
register.jsp
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="register" 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>
web.xml //这个很重要,路径绝对不能错
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>ServletTest</display-name> 4 <welcome-file-list> 5 <welcome-file>login.jsp</welcome-file> 6 </welcome-file-list> 7 8 <servlet> 9 <servlet-name>login</servlet-name> 10 <servlet-class>com.wyd.www.CheckUser</servlet-class> 11 </servlet> 12 13 <servlet> 14 <servlet-name>register</servlet-name> 15 <servlet-class>com.yqr.www.CheckRegister</servlet-class> 16 </servlet> 17 18 <servlet-mapping> 19 <servlet-name>login</servlet-name> 20 <url-pattern>/login</url-pattern> 21 </servlet-mapping> 22 23 <servlet-mapping> 24 <servlet-name>register</servlet-name> 25 <url-pattern>/register</url-pattern> 26 </servlet-mapping> 27 28 29 </web-app>
本来想用一个User类记录用户的,后面写着写着没用到······稍微修改一下CheckUser.java应该就可以去掉
个人感觉JSP和servlet的表面差别就是JSP把java程序放到网页文件里面了,而servlet则是分开的~~
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。