标签:gets reg 基于 strong getattr rom width 技术 tab
二、源程序代码
1 package com.jaovo.msg.model; 2 //User.java 3 public class User { 4 private String username; 5 private String password; 6 public String getUsername() { 7 return username; 8 } 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 }
package com.jaovo.msg.dao; import java.util.List; import com.jaovo.msg.model.User; //IUserDao.java public interface IUserDao { public User load(String userName); //查询 }
1 package com.jaovo.msg.dao; 2 import java.sql.Connection; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.util.List; 8 import com.jaovo.msg.Util.DBUtil; 9 import com.jaovo.msg.Util.UserException; 10 import com.jaovo.msg.model.User; 11 import com.mysql.jdbc.PreparedStatement; 12 public class UserDaoImpl implements IUserDao{ 13 @Override 14 public User load(String userName) { 15 Connection connection=DBUtil.getConnection(); 16 String sql="select * from t_user where username=?"; 17 PreparedStatement preparedStatement=null; 18 ResultSet resultSet=null; 19 User user=null; 20 try { 21 preparedStatement=(PreparedStatement) connection.prepareStatement(sql); 22 preparedStatement.setString(1, userName); 23 resultSet=preparedStatement.executeQuery(); 24 while(resultSet.next()) { 25 user=new User(); 26 user.setUsername(userName); 27 user.setPassword(resultSet.getString("password")); 28 //密码是否输出待考量 29 } 30 }catch(SQLException e) { 31 e.printStackTrace(); 32 }finally { 33 DBUtil.close(resultSet); 34 DBUtil.close(connection); 35 DBUtil.close(preparedStatement); 36 } 37 return user; 38 } 39 40 }
1 package com.jaovo.msg.Util; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import com.mysql.jdbc.Connection; 7 public class DBUtil { 8 public static Connection getConnection() { 9 //1加载驱动 10 try { 11 Class.forName("com.mysql.jdbc.Driver").newInstance(); 12 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } 16 String user ="root"; 17 String password="root"; 18 String url="jdbc:mysql://localhost:3306/jaovo_msg"; 19 //test 20 Connection connection=null; 21 try { 22 //2创建连接对象connection 23 connection=(Connection) DriverManager.getConnection(url, user, password); 24 } catch (SQLException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 return connection; 29 } 30 //关闭资源方法 31 public static void close(java.sql.Connection connection) { 32 try { 33 if(connection!=null) 34 { 35 connection.close(); 36 } 37 } catch (SQLException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 public static void close(PreparedStatement preparedStatement) { 43 try 44 {if(preparedStatement!=null) 45 { 46 preparedStatement.close(); 47 } 48 }catch(SQLException e) 49 { 50 e.printStackTrace(); 51 } 52 } 53 public static void close(ResultSet resultSet) { 54 try 55 {if(resultSet!=null) 56 { 57 resultSet.close(); 58 } 59 }catch(SQLException e) 60 { 61 e.printStackTrace(); 62 } 63 } 64 65 }
1 package com.jaovo.msg.Util; 2 public class UserException extends RuntimeException{ 3 public UserException() { 4 super(); 5 // TODO Auto-generated constructor stub 6 } 7 public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 8 super(message, cause, enableSuppression, writableStackTrace); 9 // TODO Auto-generated constructor stub 10 } 11 public UserException(String message, Throwable cause) { 12 super(message, cause); 13 // TODO Auto-generated constructor stub 14 } 15 public UserException(String message) { 16 super(message); 17 // TODO Auto-generated constructor stub 18 } 19 public UserException(Throwable cause) { 20 super(cause); 21 // TODO Auto-generated constructor stub 22 } 23 }
1 entry.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 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=UTF-8"> 8 <title>用户登录</title> 9 <tr align="center"> 10 <h2 style="color:blue;front-size:30px">用户登录</h2> 11 </tr> 12 </head> 13 <body> 14 <%=request.getAttribute("error") %> 15 <form action="register.jsp" method="get"> 16 <table align="center" border="1" width="500"> 17 <tr> 18 <td>用户名称: </td> 19 <td> 20 <input type="text" name="username"/> 21 </td> 22 </tr> 23 <tr> 24 <td>用户密码:</td> 25 <td> 26 <input type="password" name="password"/> 27 </td> 28 </tr> 29 <tr align="center"> 30 <td colspan="2"> 31 <input type="submit" value="登录"/> 32 </td> 33 </tr> 34 </table> 35 </form> 36 </body> 37 </html>
1 register.jsp 2 <%@page import="com.jaovo.msg.Util.UserException"%> 3 <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> 4 <%@page import="com.jaovo.msg.dao.IUserDao" %> 5 <%@page import="com.jaovo.msg.model.User"%> 6 <%@ page language="java" contentType="text/html; charset=UTF-8" 7 pageEncoding="UTF-8"%> 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";> 9 <html> 10 <% 11 //接收从客户端传递过来的参数 12 UserDaoImpl userDao=new UserDaoImpl(); 13 User user=new User(); 14 String username=request.getParameter("username"); 15 String password=request.getParameter("password"); 16 user=userDao.load(username); 17 if(username==null||"".equals(username.trim())) 18 { 19 request.setAttribute("error","用户名不能为空"); 20 %> 21 <jsp:forward page="entry.jsp"></jsp:forward> 22 <% 23 } 24 if(user==null) 25 { 26 request.setAttribute("error", "用户不存在"); 27 %> 28 <jsp:forward page="entry.jsp"></jsp:forward> 29 <% 30 } 31 if(user!=null&&!user.getPassword().contentEquals(password)); 32 { 33 request.setAttribute("error","输入密码错误"); 34 %> 35 <jsp:forward page="entry.jsp"></jsp:forward> 36 <% 37 } 38 %> 39 <body> 40 用户登录成功!!<br> 41 </body> 42 </html>
三、运行结果截图
四、希望与目标
标签:gets reg 基于 strong getattr rom width 技术 tab
原文地址:http://www.cnblogs.com/watm/p/7879425.html