注册登录的简单逻辑

下面是用户登录注册流程图

登陆界面

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  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 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.     <script type="text/javascript">  
  22.       function change(){  
  23.           var img =document.getElementById("verify");  
  24.           img.src="VerifyCodeServlet?a="+new Date().getTime();  
  25.       }  
  26.     </script>  
  27.   </head>  
  28.     
  29.   <body>  
  30.   <center>  
  31. <div>  
  32. <h1>欢迎登陆</h1>  
  33. <form action="LoginServlet" method="post">  
  34.     <table>  
  35.     <tr>  
  36.     <td width="66" align="right"><font size="3">帐号:</font></td><td colspan="2"><input type="text" name="username" value="${username }" style="width:200;height:25;"/></td>  
  37.     </tr>  
  38.     <tr>  
  39.     <td align="right"><font size="3">密码:</font></td><td colspan="2"><input type="text" name="password"  style="width:200;height:25;"/></td>  
  40.     </tr>  
  41.     <tr>  
  42.     <td align="right"><font size="3">验证码:</font></td>  
  43.     <td width="108" valign="middle"><input type="text" name="verifycode" style="width:100;height:25;"/></td>  
  44.     <td width="90" valign="middle"><a href="javascript:change()"><img src="VerifyCodeServlet" id="verify" border="0"></a></td>  
  45.     </tr>  
  46.     <tr><td colspan="3" align="center"><input type="submit" value="登录" style="width:130;height:30;"/></td></tr>  
  47.     </table>  
  48.     </form>  
  49.     <a href="regist.jsp"><font size="2"><i>没有帐号?点击注册</i></font></a>  
  50. <font color="red" size="2"> ${msg }</font>  
  51. </div>  
  52. </center>  
  53.   </body>  
  54. </html>  

注册界面

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  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 'regist.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.   <center>  
  27.     <div>  
  28.     <h1>注册</h1>  
  29.     <form action="RegistServlet" method="post">  
  30.     请输入帐号:<input type="text" name="username"><br/>  
  31.     请输入密码:<input type="password" name="password"><br/>  
  32.     请确认密码:<input type="password" name="rpsw"><br/>  
  33.     <input type="submit" value="注册">  
  34.     </form>  
  35.    <font color="red" size="2"> ${msg }</font>  
  36.     </div>  
  37.     </center>  
  38.   </body>  
  39. </html>  
欢迎界面

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  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 'welcome.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.     <h1>这是主页</h1>  
  27.     <h2>${msg }</h2>  
  28.   </body>  
  29. </html>  
LoginServlet

[html]  view plain  copy
  1. import java.io.IOException;  
  2. import javax.servlet.ServletException;  
  3. import javax.servlet.http.HttpServlet;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. import com.wzc.loginDao.UserDao;  
  8.   
  9. public class LoginServlet extends HttpServlet {  
  10.   
  11.     private static final long serialVersionUID = 1L;  
  12.   
  13.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.         request.setCharacterEncoding("utf-8");  
  16.         response.setContentType("text/html;charset=utf-8");  
  17.         String username = request.getParameter("username");  
  18.         String password = request.getParameter("password");  
  19.         String verifyc  = request.getParameter("verifycode");<span style="font-family: Arial, Helvetica, sans-serif;">//得到表单输入的内容</span>  
  20.         String svc =(String) request.getSession().getAttribute("sessionverify");  
  21.         String psw =new UserDao().findUsername(username);  
  22.         if(!svc.equalsIgnoreCase(verifyc)){  
  23.             request.setAttribute("msg", "验证码错误!");  
  24.             request.getRequestDispatcher("/index.jsp").forward(request, response);  
  25.             return;  
  26.         }  
  27.         if(psw ==null){  
  28.             request.setAttribute("msg", "没有这个用户!");  
  29.             request.getRequestDispatcher("/index.jsp").forward(request, response);  
  30.             return;  
  31.         }  
  32.         if(psw!=null&&!psw.equals(password)){  
  33.             request.setAttribute("msg", "密码错误请重新输入!");  
  34.             request.getRequestDispatcher("/index.jsp").forward(request, response);  
  35.             return;  
  36.         }  
  37.         if(psw.equals(password)){  
  38.             request.setAttribute("msg", "用户:"+username+",欢迎访问");  
  39.             request.getRequestDispatcher("/welcome.jsp").forward(request, response);  
  40.             //response.setHeader("Refresh","1;url=welcome.jsp");  
  41.         }  
  42.           
  43.     }  
  44.   
  45. }  
RegistServlet

[html]  view plain  copy
  1. import java.io.IOException;  
  2. import javax.servlet.ServletException;  
  3. import javax.servlet.http.HttpServlet;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. import com.wzc.loginDao.UserDao;  
  8.   
  9. public class RegistServlet extends HttpServlet {  
  10.   
  11.     private static final long serialVersionUID = 1L;  
  12.   
  13.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.   
  16.         request.setCharacterEncoding("utf-8");  
  17.         response.setContentType("text/html;charset=utf-8");  
  18.         String username = request.getParameter("username");  
  19.         String password = request.getParameter("password");  
  20.         String rpsw = request.getParameter("rpsw");//得到表单输入的内容  
  21.         if(username==null||username.trim().isEmpty()){  
  22.             request.setAttribute("msg", "帐号不能为空");  
  23.             request.getRequestDispatcher("/regist.jsp").forward(request, response);  
  24.             return;  
  25.         }  
  26.         if(password==null||password.trim().isEmpty()){  
  27.             request.setAttribute("msg", "密码不能为空");  
  28.             request.getRequestDispatcher("/regist.jsp").forward(request, response);  
  29.             return;  
  30.         }  
  31.         if(!password.equals(rpsw)){  
  32.             request.setAttribute("msg", "两次输入的密码不同");  
  33.             request.getRequestDispatcher("/regist.jsp").forward(request, response);  
  34.             return;  
  35.         }  
  36.         UserDao u = new UserDao();  
  37.         u.addUser(username,password);//调用addUser()方法  
  38.         request.setAttribute("msg", "恭喜:"+username+",注册成功");  
  39.         request.getRequestDispatcher("/index.jsp").forward(request, response);  
  40.   
  41.     }  
  42.   
  43. }  

VerifyCodeServlet

[html]  view plain  copy
  1. import java.awt.image.BufferedImage;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.   
  8. import com.wzc.utils.VerifyCode;  
  9.   
  10.   
  11. public class VerifyCodeServlet extends HttpServlet {  
  12.   
  13.     private static final long serialVersionUID = 1L;  
  14.   
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.         VerifyCode vc = new VerifyCode();  
  18.         BufferedImage image = vc.getImage(85,23);//设置验证码图片大小  
  19.         request.getSession().setAttribute("sessionverify", vc.getText());//将验证码文本保存到session域  
  20.         VerifyCode.output(image, response.getOutputStream());  
  21.   
  22.     }  
  23.   
  24. }  

VerifyCode

[html]  view plain  copy
  1. public class VerifyCode {  
  2.   
  3.   
  4.     public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',  
  5.             '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',  
  6.             'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };  
  7.   
  8.     public static Random random = new Random();  
  9.   
  10.     public String getRandomString() {  
  11.         StringBuffer buffer = new StringBuffer();  
  12.         for (int i = 0; i < 4; i++) {  
  13.             buffer.append(CHARS[random.nextInt(CHARS.length)]);  
  14.         }  
  15.         return buffer.toString();  
  16.     }  
  17.   
  18.     public  Color getRandomColor() {  
  19.         return new Color(random.nextInt(255), random.nextInt(255), random  
  20.                 .nextInt(255));  
  21.     }  
  22.   
  23.     public  Color getReverseColor(Color c) {  
  24.         return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c  
  25.                 .getBlue());  
  26.     }  
  27.     String text = getRandomString();  
  28.     public String getText() {  
  29.         return text;  
  30.     }  
  31.   
  32.     public BufferedImage getImage(int width,int height ){  
  33.         Color color = getRandomColor();  
  34.         Color reverse = getReverseColor(color);  
  35.   
  36.         BufferedImage bi = new BufferedImage(width, height,  
  37.                 BufferedImage.TYPE_INT_RGB);  
  38.         Graphics2D g = bi.createGraphics();  
  39.         g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));  
  40.         g.setColor(color);  
  41.         g.fillRect(0, 0, width, height);  
  42.         g.setColor(reverse);  
  43.         g.drawString(text, 10, 22);  
  44.         for (int i = 0n = random.nextInt(80); i < n; i++) {  
  45.             g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);  
  46.         }  
  47.         return bi;  
  48.           
  49.     }  
  50.     public static void output(BufferedImage image, OutputStream out) throws IOException{  
  51.         ImageIO.write(image, "JPEG", out);  
  52.     }  
  53.   
  54. }  

UserDao

[html]  view plain  copy
  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 UserDao {  
  8.     public String findUsername(String username){  
  9.         String psw = null;  
  10.         Connection con =null;  
  11.         PreparedStatement pstmt =null;  
  12.         ResultSet rs = null;  
  13.         try {  
  14.             String driver ="com.mysql.jdbc.Driver";  
  15.             String url ="jdbc:mysql://localhost:3306/zhuce";  
  16.             String user ="root";  
  17.             String password ="root";<span style="font-family: Arial, Helvetica, sans-serif;">//改为自己的用户名密码和数据库名</span>  
  18.             Class.forName(driver);  
  19.             con = DriverManager.getConnection(url, user, password);  
  20.             String sql = "select * from zc where name=?";  
  21.             pstmt = con.prepareStatement(sql);  
  22.             pstmt.setString(1, username);  
  23.             rs = pstmt.executeQuery();  
  24.             if(rs==null){  
  25.                 return null;  
  26.             }  
  27.             if(rs.next()){  
  28.                 psw=rs.getString("password");  
  29.             }else{  
  30.                 psw=null;  
  31.             }  
  32.         } catch (ClassNotFoundException e) {  
  33.             e.printStackTrace();  
  34.         } catch (SQLException e) {  
  35.             e.printStackTrace();  
  36.         }finally {  
  37.             try {  
  38.                 if(pstmt!=null)pstmt.close();  
  39.                 if(con!=null)con.close();  
  40.                 }   
  41.             catch (SQLException e) {          
  42.                                     }  
  43.         }  
  44.         return psw;  
  45.     }  
  46.     public void addUser(String username,String psw){  
  47.         Connection con =null;  
  48.         PreparedStatement pstmt =null;  
  49.         try {  
  50.             String driver ="com.mysql.jdbc.Driver";  
  51.             String url ="jdbc:mysql://localhost:3306/zhuce";  
  52.             String user ="root";  
  53.             String password ="root";//改为自己的用户名密码和数据库名  
  54.             Class.forName(driver);  
  55.             con = DriverManager.getConnection(url, user, password);  
  56.             String sql = "INSERT INTO ZC VALUES(?,?)";  
  57.             pstmt = con.prepareStatement(sql);  
  58.             pstmt.setString(1, username);  
  59.             pstmt.setString(2, psw);  
  60.             pstmt.executeUpdate();  
  61.         } catch (ClassNotFoundException e) {  
  62.             e.printStackTrace();  
  63.         } catch (SQLException e) {  
  64.             e.printStackTrace();  
  65.         }finally {  
  66.             try {  
  67.                 if(pstmt!=null)pstmt.close();  
  68.                 if(con!=null)con.close();  
  69.                 }   
  70.             catch (SQLException e) {          
  71.                                     }  
  72.         }  
  73.     }  
  74.     //单独测试使用  
  75.     //public static void main(String[] args) {  
  76.         //String psw =new UserDao().findUsername("345");  
  77.         //System.out.println(psw);  
  78.         //UserDao u = new UserDao();  
  79.         //u.addUser("345", "345");  
  80.     //}  
  81.       
  82. }  

创建的数据库的user表有两个属性name,password 类型为varchar
点击下载源码