jsp10
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
1 package com.fyx.dao; 2 3 import java.sql.Connection; 4 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.ArrayList; 9 import java.util.List; 10 import com.qmt.entity.Msg; 11 12 public class MsgDao extends basedao{ 13 //插入邮件 14 15 //删除邮件 16 17 //修改邮件状态 18 19 //按照接收者查询全部邮件 20 public List<Msg> getMailByReceiver(String name){ 21 List<Msg> list=new ArrayList<Msg>(); 22 Connection conn=getConnection(); 23 String sql="select * from msg where sendto=?"; 24 PreparedStatement ps; 25 ResultSet rs; 26 try { 27 ps = conn.prepareStatement(sql); 28 ps.setString(1, name); 29 rs=ps.executeQuery(); 30 while(rs.next()){ 31 Msg m=new Msg(); 32 m.setMsgid(rs.getInt(1)); 33 m.setUsername(rs.getString(2)); 34 m.setTitle(rs.getString(3)); 35 m.setMsgcontent(rs.getString(4)); 36 m.setState(rs.getInt(5)); 37 m.setSendto(rs.getString(6)); 38 m.setMsg_create_date(rs.getDate(7)); 39 list.add(m); 40 } 41 42 closeAll(conn, ps, rs); 43 44 } catch (SQLException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 return list; 49 } 50 }
1 package com.fyx.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.Date; 9 import java.util.List; 10 11 import com.qmt.entity.Msg; 12 13 14 15 public class MsgDao extends BaseDao { 16 // 1,插入邮件 17 public void addMsg(Msg m) { 18 Connection con = getConnection(); 19 String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)"; 20 PreparedStatement ps = null; 21 try { 22 ps = con.prepareStatement(sql); 23 ps.setString(1, m.getUsername()); 24 ps.setString(2, m.getTitle()); 25 ps.setString(3, m.getMsgcontent()); 26 ps.setInt(4, 1); 27 ps.setString(5, m.getSendto()); 28 ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间 29 ps.executeUpdate(); 30 } catch (SQLException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } finally { 34 closeAll(con, ps, null); 35 } 36 37 } 38 39 // 2.删除邮件 40 public void delMail(int id) { 41 Connection conn = getConnection(); 42 String sql = "delete from msg where msgid=?"; 43 PreparedStatement ps = null; 44 try { 45 ps = conn.prepareStatement(sql); 46 ps.setInt(1, id); 47 ps.executeUpdate(); 48 } catch (SQLException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 }finally{ 52 closeAll(conn, ps, null); 53 } 54 55 } 56 //(测试删除邮件的代码是否编写成功) 57 // public static void main(String[] args) { 58 // MsgDao md=new MsgDao(); 59 // md.delMail(3); 60 //} 61 62 63 // 3.修改邮件状态 64 public void update(int id) { 65 Connection con = getConnection(); 66 String sql = "update msg set state='1' where msgid=?"; 67 PreparedStatement ps = null; 68 try { 69 ps = con.prepareStatement(sql); 70 ps.setInt(1, id); 71 ps.executeUpdate(); 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } finally { 75 closeAll(con, ps, null); 76 } 77 } 78 79 // 4.按照接收者查询全部邮件 80 public List<Msg> getMailByReceiver(String name) { 81 List<Msg> list = new ArrayList<Msg>(); 82 Connection con = getConnection(); 83 String sql = "select * from msg where sendto=?"; 84 PreparedStatement ps=null; 85 ResultSet rs=null; 86 try { 87 ps = con.prepareStatement(sql); 88 ps.setString(1, name); 89 rs = ps.executeQuery(); 90 while (rs.next()) { 91 Msg m = new Msg(); 92 m.setMsgid(rs.getInt("msgid")); 93 m.setUsername(rs.getString("username")); 94 m.setTitle(rs.getString("title")); 95 m.setMsgcontent(rs.getString("msgcontent")); 96 m.setState(rs.getInt("state")); 97 m.setSendto(rs.getString("sendto")); 98 m.setMsg_create_date(rs.getDate("msg_create_date")); 99 list.add(m); 100 } 101 } catch (SQLException e) { 102 e.printStackTrace(); 103 }finally{ 104 closeAll(con, ps, rs); 105 } 106 return list; 107 108 } 109 //5.实现阅读邮件功能 110 public Msg read(int id) { 111 Connection con = getConnection(); 112 String sql = "select msgid,username,sendto,title,msgcontent,msg_create_date from msg where msgid=?"; 113 PreparedStatement ps = null; 114 ResultSet rs = null; 115 try { 116 ps = con.prepareStatement(sql); 117 ps.setInt(1, id); 118 rs = ps.executeQuery(); 119 while (rs.next()) { 120 Msg m = new Msg(); 121 m.setMsgid(rs.getInt("msgid")); 122 m.setUsername(rs.getString("username")); 123 m.setTitle(rs.getString("title")); 124 m.setMsgcontent(rs.getString("msgcontent")); 125 m.setSendto(rs.getString("sendto")); 126 m.setMsg_create_date(rs.getDate("msg_create_date")); 127 return m; 128 } 129 130 } catch (SQLException e) { 131 e.printStackTrace(); 132 } finally { 133 closeAll(con, ps, rs); 134 } 135 return null; 136 } 137 138 139 }
1 package com.fyx.dao; 2 3 import java.sql.Connection; 4 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class UsersDao extends BaseDao { 10 11 // 关于用户的增删改查 12 13 // 1.登录 14 public boolean login(String uname, String upwd) { 15 boolean f = false; 16 Connection con = getConnection(); 17 String sql = "select * from users where uname=? and upwd=?"; 18 PreparedStatement ps = null; 19 ResultSet rs = null; 20 try { 21 ps = con.prepareStatement(sql); 22 ps.setString(1, uname); 23 ps.setString(2, upwd); 24 rs = ps.executeQuery(); 25 if (rs.next()) 26 f = true; 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 } finally { 30 closeAll(con, ps, rs); 31 } 32 return f; 33 } 34 35 // 2.注册 36 public int register(String uname, String upwd) { 37 Connection con = getConnection(); 38 PreparedStatement ps = null; 39 int x = 0; 40 try { 41 String sql = "insert into users(uname,upwd) values(?,?)"; 42 ps = con.prepareStatement(sql); 43 ps.setString(1, uname); 44 ps.setString(2, upwd); 45 x = ps.executeUpdate(); 46 } catch (SQLException e) { 47 e.printStackTrace(); 48 } finally { 49 closeAll(con, ps, null); 50 } 51 return x; 52 } 53 }
1 package com.fyx.entity; 2 3 import java.util.Date; 4 5 public class Msg { 6 private int msgid; 7 private String username; 8 private String title; 9 private String msgcontent; 10 private int state; 11 private String sendto; 12 Date msg_create_date; 13 14 public int getMsgid() { 15 return msgid; 16 } 17 18 public void setMsgid(int msgid) { 19 this.msgid = msgid; 20 } 21 22 public String getUsername() { 23 return username; 24 } 25 26 public void setUsername(String username) { 27 this.username = username; 28 } 29 30 public String getTitle() { 31 return title; 32 } 33 34 public void setTitle(String title) { 35 this.title = title; 36 } 37 38 public String getMsgcontent() { 39 return msgcontent; 40 } 41 42 public void setMsgcontent(String msgcontent) { 43 this.msgcontent = msgcontent; 44 } 45 46 public int getState() { 47 return state; 48 } 49 50 public void setState(int state) { 51 this.state = state; 52 } 53 54 public String getSendto() { 55 return sendto; 56 } 57 58 public void setSendto(String sendto) { 59 this.sendto = sendto; 60 } 61 62 public Date getMsg_create_date() { 63 return msg_create_date; 64 } 65 66 public void setMsg_create_date(Date msg_create_date) { 67 this.msg_create_date = msg_create_date; 68 } 69 70 71 public Msg(int msgid, String username, String title, String msgcontent, 72 int state, String sendto, Date msg_create_date) { 73 super(); 74 this.msgid = msgid; 75 this.username = username; 76 this.title = title; 77 this.msgcontent = msgcontent; 78 this.state = state; 79 this.sendto = sendto; 80 this.msg_create_date = msg_create_date; 81 } 82 83 84 85 public Msg() { 86 super(); 87 } 88 89 @Override 90 public String toString() { 91 return "Msg [msgid=" + msgid + ", username=" + username + ", sendto=" + sendto + ", title=" 92 + title + ", msg_create_date=" + msg_create_date + ", state=" + state + ", msgcontent=" 93 + msgcontent + "]"; 94 } 95 96 }
1 package com.fyx.entity; 2 3 public class Users { 4 5 private int id; 6 private String uname; 7 private String upwd; 8 9 public int getId() { 10 return id; 11 } 12 13 public void setId(int id) { 14 this.id = id; 15 } 16 17 public String getUname() { 18 return uname; 19 } 20 21 public void setUname(String uname) { 22 this.uname = uname; 23 } 24 25 public String getUpwd() { 26 return upwd; 27 } 28 29 public void setUpwd(String upwd) { 30 this.upwd = upwd; 31 } 32 33 }
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'reg.jsp' starting page</title> 6 7 </head> 8 9 <body> 10 <script type="text/javascript"> 11 function validate() { 12 if (registerForm.uname.value == "") { 13 alert("账号不能为空!"); 14 return; 15 } 16 if (registerForm.upwd.value == "") { 17 alert("密码不能为空!"); 18 return; 19 } 20 registerForm.submit(); 21 } 22 </script> 23 24 <form name="registerForm" action="doreg.jsp" method="post"> 25 用户名:<input type="text" name="uname"><br> 26 密 码: <input type="password" name="upwd"><br> 27 <input type="submit" value="注册"><hr> 28 </form><hr> 29 <a href="login.jsp">返回登录</a> 30 </body> 31 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ page import="com.qmt.dao.UsersDao"%> 3 <%@ page import="com.qmt.dao.MsgDao"%> 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>My JSP 'doreg.jsp' starting page</title> 8 9 </head> 10 11 <body> 12 13 <% 14 request.setCharacterEncoding("utf-8"); 15 String uname = request.getParameter("uname"); 16 String upwd = request.getParameter("upwd"); 17 18 UsersDao ud = new UsersDao(); 19 MsgDao md = new MsgDao(); 20 if (ud.register(uname, upwd) > 0) { 21 session.setAttribute("uname", uname); 22 request.getRequestDispatcher("login.jsp").forward(request, 23 response); 24 } else { 25 out.print("注册失败,请重新注册......."); 26 response.setHeader("refresh", "3;url=reg.jsp"); 27 } 28 %> 29 </body> 30 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'login.jsp' starting page</title> 6 7 </head> 8 9 <body> 10 <script type="text/javascript"> 11 function validate() { 12 if (loginForm.uname.value == "") { 13 alert("账号不能为空!"); 14 return; 15 } 16 if (loginForm.upwd.value == "") { 17 alert("密码不能为空!"); 18 return; 19 } 20 loginForm.submit(); 21 } 22 </script> 23 <form name="loginForm" action="dologin.jsp" method="post"> 24 用户名:<input type="text" name="uname" ><br> 25 密 码:<input type="password" name="upwd" ><br> 26 <input type="button" value="登录" onClick="validate()"> 27 </form> 28 <hr> 29 <a href="reg.jsp">返回注册</a> 30 </body> 31 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ page import="com.qmt.dao.UsersDao"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>My JSP 'dologin.jsp' starting page</title> 7 8 </head> 9 10 <body> 11 <% 12 request.setCharacterEncoding("utf-8"); 13 String uname = request.getParameter("uname"); 14 String upwd = request.getParameter("upwd"); 15 UsersDao ud = new UsersDao(); 16 if (ud.login(uname, upwd)) { 17 session.setAttribute("uname",uname); 18 request.getRequestDispatcher("main.jsp").forward( 19 request,response); 20 } else { 21 out.print("登录失败,即将跳回登录页......"); 22 response.setHeader("refresh", "3;url=login.jsp"); 23 } 24 %> 25 </body> 26 </html>