JSP第十一周练习
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
1 package com.wl.email.domain; 2 3 public class Email { 4 5 private Long id; 6 private String address; 7 private String sender; 8 private String tittle; 9 private String content; 10 private String sendDate ; 11 private String state; 12 13 public Email(Long id, String address, String sender, String tittle, 14 String content, String sendDate, String state) { 15 super(); 16 this.id = id; 17 this.address = address; 18 this.sender = sender; 19 this.tittle = tittle; 20 this.content = content; 21 this.sendDate = sendDate; 22 this.state = state; 23 } 24 public Email() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 public Long getId() { 29 return id; 30 } 31 public void setId(Long id) { 32 this.id = id; 33 } 34 public String getAddress() { 35 return address; 36 } 37 public void setAddress(String address) { 38 this.address = address; 39 } 40 public String getSender() { 41 return sender; 42 } 43 public void setSender(String sender) { 44 this.sender = sender; 45 } 46 public String getTittle() { 47 return tittle; 48 } 49 public void setTittle(String tittle) { 50 this.tittle = tittle; 51 } 52 public String getContent() { 53 return content; 54 } 55 public void setContent(String content) { 56 this.content = content; 57 } 58 public String getSendDate() { 59 return sendDate; 60 } 61 public void setSendDate(String sendDate) { 62 this.sendDate = sendDate; 63 } 64 public String getState() { 65 return state; 66 } 67 public void setState(String state) { 68 this.state = state; 69 } 70 @Override 71 public String toString() { 72 return "Email [id=" + id + ", address=" + address + ", sender=" 73 + sender + ", tittle=" + tittle + ", content=" + content 74 + ", sendDate=" + sendDate + ", state=" + state + "]"; 75 } 76 77 78 }
1 package com.wl.email.domain; 2 3 public class User { 4 5 private Long id; 6 private String username; 7 private String password; 8 9 public User(Long id, String username, String password) { 10 super(); 11 this.id = id; 12 this.username = username; 13 this.password = password; 14 } 15 public User() { 16 super(); 17 // TODO Auto-generated constructor stub 18 } 19 public Long getId() { 20 return id; 21 } 22 public void setId(Long id) { 23 this.id = id; 24 } 25 public String getUsername() { 26 return username; 27 } 28 public void setUsername(String username) { 29 this.username = username; 30 } 31 public String getPassword() { 32 return password; 33 } 34 public void setPassword(String password) { 35 this.password = password; 36 } 37 @Override 38 public String toString() { 39 return "Email [id=" + id + ", username=" + username + ", password=" 40 + password + "]"; 41 } 42 43 }
1 package com.wl.email.utils; 2 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import com.mysql.jdbc.Connection; 9 10 public class JDBCUtils { 11 private static Connection conn; 12 static { 13 try { 14 Class.forName("com.mysql.jdbc.Driver"); 15 String url = "jdbc:mysql:///wl_tale?SSL=false"; 16 String username = "root"; 17 String password = "root"; 18 conn = (Connection) DriverManager.getConnection(url, username, 19 password); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 24 } 25 26 public static Connection getConn() { 27 return conn; 28 } 29 30 public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) { 31 try { 32 if (rs != null) 33 rs.close(); 34 if (ps != null) 35 ps.close(); 36 if (conn != null) 37 conn.close(); 38 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 44 }
1 package com.wl.email.dao; 2 3 import java.util.List; 4 5 import com.wl.email.domain.Email; 6 7 public interface IEmailDAO { 8 void add(Email email); 9 void delById(Long id); 10 void update(Email email); 11 Email selOneById(Long id); 12 List<Email> selAll(); 13 14 }
1 package com.wl.email.dao; 2 3 import java.util.List; 4 5 import com.wl.email.domain.User; 6 7 public interface IUserDAO { 8 void add(User user); 9 void delById(Long id); 10 void update(User user); 11 User selOneByUser(User user); 12 List<User> selAll(); 13 14 }
1 package com.wl.email.dao.impl; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import com.mysql.jdbc.Connection; 9 import com.mysql.jdbc.PreparedStatement; 10 import com.wl.email.dao.IEmailDAO; 11 import com.wl.email.dao.IUserDAO; 12 import com.wl.email.domain.Email; 13 import com.wl.email.domain.User; 14 import com.wl.email.utils.JDBCUtils; 15 16 public class IEmailDAOImpl implements IEmailDAO{ 17 18 @Override 19 public void add(Email email) { 20 // TODO Auto-generated method stub 21 22 } 23 24 @Override 25 public void delById(Long id) { 26 // TODO Auto-generated method stub 27 28 } 29 30 @Override 31 public void update(Email email) { 32 // TODO Auto-generated method stub 33 34 } 35 36 @Override 37 public Email selOneById(Long id) { 38 // TODO Auto-generated method stub 39 return null; 40 } 41 42 @Override 43 public List<Email> selAll() { 44 Connection conn = null; 45 PreparedStatement ps = null; 46 ResultSet rs = null; 47 List<Email> listEmails = new ArrayList<Email>(); 48 try { 49 conn = JDBCUtils.getConn(); 50 String sql = "SELECT id,address,sender,tittle,content,sendDate,state FROM email"; 51 ps = (PreparedStatement) conn.prepareStatement(sql); 52 rs = ps.executeQuery(); 53 while (rs.next()) { 54 // 8.获取ResuleSet对象的元素 55 long id = rs.getLong("id"); 56 String address = rs.getString("address"); 57 String sender = rs.getString("sender"); 58 String tittle = rs.getString("tittle"); 59 String content = rs.getString("content"); 60 String sendDate = rs.getString("sendDate"); 61 String state = rs.getString("state"); 62 listEmails.add(new Email(id,address,sender,tittle,content,sendDate,state)); 63 } 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 }finally{ 67 JDBCUtils.closeAll(conn, ps, rs); 68 } 69 70 return listEmails; 71 } 72 73 74 }
1 package com.wl.email.dao.impl; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import com.mysql.jdbc.Connection; 8 import com.mysql.jdbc.PreparedStatement; 9 import com.wl.email.dao.IUserDAO; 10 import com.wl.email.domain.User; 11 import com.wl.email.utils.JDBCUtils; 12 13 public class IUserDAOImpl implements IUserDAO { 14 15 @Override 16 public void add(User user) { 17 Connection conn = null; 18 PreparedStatement ps = null; 19 try { 20 conn = JDBCUtils.getConn(); 21 String sql = "INSERT INTO user(username,password) VALUES(?,?); "; 22 ps = (PreparedStatement) conn.prepareStatement(sql); 23 ps.setString(1, user.getUsername()); 24 ps.setString(2, user.getPassword()); 25 int row = ps.executeUpdate(); 26 System.out.println((row > 0)?"添加成功":"添加失败"); 27 } catch (SQLException e) { 28 29 e.printStackTrace(); 30 }finally{ 31 JDBCUtils.closeAll(conn, ps, null); 32 } 33 34 } 35 36 @Override 37 public void delById(Long id) { 38 // TODO Auto-generated method stub 39 40 } 41 42 @Override 43 public void update(User user) { 44 // TODO Auto-generated method stub 45 46 } 47 48 @Override 49 public User selOneByUser(User user) { 50 Connection conn = null; 51 PreparedStatement ps = null; 52 ResultSet rs = null; 53 User u = new User(); 54 try { 55 conn = JDBCUtils.getConn(); 56 String sql = "select * from user where username=? and password=?"; 57 ps = (PreparedStatement) conn.prepareStatement(sql); 58 ps.setString(1, user.getUsername()); 59 ps.setString(2, user.getPassword()); 60 rs = ps.executeQuery(); 61 if(rs.next()){ 62 long id = rs.getLong("id"); 63 String userName = rs.getString("username"); 64 String pwd = rs.getString("password"); 65 u = new User(id,userName,pwd); 66 } 67 } catch (SQLException e) { 68 69 e.printStackTrace(); 70 }finally{ 71 JDBCUtils.closeAll(conn, ps, rs); 72 } 73 return u; 74 } 75 76 @Override 77 public List<User> selAll() { 78 // TODO Auto-generated method stub 79 return null; 80 } 81 82 }
1 package com.wl.email.service; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 6 import com.mysql.jdbc.Connection; 7 import com.mysql.jdbc.PreparedStatement; 8 import com.wl.email.dao.IUserDAO; 9 import com.wl.email.dao.impl.IUserDAOImpl; 10 import com.wl.email.domain.User; 11 import com.wl.email.utils.JDBCUtils; 12 13 public class EmailService { 14 15 16 public boolean login(String username, String password) { 17 IUserDAO iuDAO = new IUserDAOImpl(); 18 User user = iuDAO.selOneByUser(new User(new Long(100),username,password)); 19 if(user != null){ 20 return true; 21 } 22 return false; 23 24 } 25 26 public void register(String username, String password) { 27 IUserDAO iuDAO = new IUserDAOImpl(); 28 iuDAO.add(new User(new Long(100),username,password)); 29 30 31 } 32 }
3.登陆,注册,登陆后显示全部邮件
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <title>用户注册</title> 5 <script type="text/javascript"> 6 function ok() { 7 if (registerForm.username.value == "") { 8 alert("账号不能为空!"); 9 return; 10 } 11 if (registerForm.password.value == "") { 12 alert("密码不能为空!"); 13 return; 14 } 15 registerForm.submit(); 16 17 } 18 </script> 19 </head> 20 <body> 21 22 <form name="registerForm" action="AddService.jsp" method="post"> 23 <table> 24 <tr> 25 <th align="center">请注册</th> 26 </tr> 27 <tr> 28 <td>用户名:</td> 29 <td><input type="text" name="username"></input></td> 30 </tr> 31 <tr> 32 <td>密 码:</td> 33 <td><input type="password" name="password"></input></td> 34 </tr> 35 <tr> 36 <td rowspan="2"><input type="button" value="完成注册" 37 onClick="ok()"> 38 </td> 39 </tr> 40 </table> 41 </form> 42 </body> 43 </html>
1 <%@page import="com.wl.email.service.EmailService"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>My JSP 'Register.jsp' starting page</title> 7 </head> 8 9 <body> 10 <% 11 EmailService ese = new EmailService(); 12 request.setCharacterEncoding("utf-8"); 13 String username = request.getParameter("username"); 14 String password = request.getParameter("password"); 15 ese.register(username, password); 16 request.getSession().setAttribute("USER_IN_SESSION", username); 17 request.getRequestDispatcher("Login.jsp") 18 .forward(request, response); 19 %> 20 21 </body> 22 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <html> 4 <head> 5 <title>登录</title> 6 </head> 7 <script type="text/javascript"> 8 function validate() { 9 if (loginForm.username.value == "") { 10 alert("账号不能为空!"); 11 return; 12 } 13 if (loginForm.password.value == "") { 14 alert("密码不能为空!"); 15 return; 16 } 17 loginForm.submit(); 18 } 19 </script> 20 <body> 21 <form name="loginForm" action="Home.jsp" method="post"> 22 <table> 23 <tr> 24 <td> 25 <th align="center">欢迎登录</th> 26 </td> 27 </tr> 28 <tr> 29 <td>用户名:</td> 30 <td><input type="text" name="username"></input> 31 </td> 32 </tr> 33 <tr> 34 <td>密 码:</td> 35 <td><input type="password" name="password"></input> 36 </td> 37 </tr> 38 <tr> 39 <td><input type="button" value="登录" onClick="validate()"> 40 </td> 41 <td><a href="Register.jsp">注册</a></td> 42 </tr> 43 </table> 44 </form> 45 46 <br> 47 </body> 48 </html>
1 <%@page import="com.wl.email.dao.impl.IEmailDAOImpl"%> 2 <%@page import="com.wl.email.domain.Email"%> 3 <%@page import="com.wl.email.service.EmailService"%> 4 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme() + "://" 8 + request.getServerName() + ":" + request.getServerPort() 9 + path + "/"; 10 %> 11 12 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 13 <html> 14 <head> 15 <base href="<%=basePath%>"> 16 <title>我的邮箱</title> 17 <% 18 EmailService ese = new EmailService(); 19 request.setCharacterEncoding("utf-8"); 20 String username = request.getParameter("username"); 21 String password = request.getParameter("password"); 22 if (ese.login(username, password)) { 23 request.getSession().setAttribute("USER_IN_SESSION", username); 24 /* request.getRequestDispatcher("Home.jsp").forward( 25 request, response); */ 26 } else { 27 %> 28 <script type="text/javascript"> 29 alert("账号或密码错误"); 30 </script> 31 <% 32 response.setHeader("refresh", "0;url=Login.jsp"); 33 } 34 %> 35 </head> 36 <body> 37 <h1>欢迎你:${USER_IN_SESSION}</h1> 38 <a href="#">发送邮件</a> 39 <a href="#">删除邮件</a> 40 <a href="#">查看未读邮件</a> 41 <h3>收件箱</h3> 42 <% 43 IEmailDAOImpl ieDAO = new IEmailDAOImpl(); 44 45 List<Email> list = ieDAO.selAll(); 46 %> 47 <table border="1"> 48 <tr> 49 <th>发件人</th> 50 <th>标题</th> 51 <th>状态</th> 52 <th>时间</th> 53 </tr> 54 <% 55 for (int i = 0; i < list.size(); i++) { 56 %> 57 <tr> 58 <td> 59 <% 60 out.print(list.get(i).getSender() + "<br>"); 61 %> 62 </td> 63 <td> 64 <% 65 out.print(list.get(i).getTittle() + "<br>"); 66 %> 67 </td> 68 <td> 69 <% 70 out.print(list.get(i).getState() + "<br>"); 71 %> 72 </td> 73 <td> 74 <% 75 out.print(list.get(i).getSendDate() + "<br>"); 76 %> 77 </td> 78 </tr> 79 <% 80 } 81 %> 82 </table> 83 84 </body> 85 </html>