1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
CREATE TABLE euser ( uid INT PRIMARY KEY auto_increment, uname VARCHAR (10), upwd VARCHAR (10) ) CREATE TABLE msg ( eid INT PRIMARY KEY auto_increment, sender VARCHAR (10), addressee VARCHAR (10), title VARCHAR (10), content VARCHAR (10), send_time datetime, state INT ) INSERT INTO msg () VALUES ( 101, '大哥', '小弟', '学习资料', 'JSP', '2022-5-11 12:12:12', 1 ); INSERT INTO msg () VALUES ( 102, '大哥', '二弟', '学习资料', 'Java', '2022-5-11 12:21:21', 1 ); INSERT INTO msg () VALUES ( 103, '大哥', '小弟', '学习资料', 'JSP', '2022-5-11 12:00:12', 0 ); INSERT INTO msg () VALUES ( 104, '大哥老婆', '大哥', '学习资料', 'Java', '2022-5-11 12:21:55', 1 );
package Dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class baseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/class", "root", "root"); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
package Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class dao extends baseDao{ public boolean login(String name,String pwd){ boolean f=false; Connection conn=getConnection(); String sql="select * from euser where uname=? and upwd=?"; PreparedStatement ps; try { ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); ResultSet rs=ps.executeQuery(); if(rs.next()) f=true; closeAll(conn, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return f; } public void reg(String uname, String upwd) { Connection conn = getConnection(); PreparedStatement ps = null; try { String sql = "insert into euser(uname,upwd) values(?,?)"; // 4.执行SQL语句 ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } } }
package Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class msgDao extends baseDao{ public List<Msg> getMailByReceiver(String name){ List<Msg> list=new ArrayList<Msg>(); Connection con=getConnection(); String sql="select * from msg where username=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps = con.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ Msg m=new Msg(); m.setMsgid(rs.getInt(1)); m.setUsername(rs.getString(2)); m.setTitle(rs.getString(3)); m.setMsgcontent(rs.getString(4)); m.setState(rs.getInt(5)); m.setSendto(rs.getString(6)); m.setMsg_create_date(rs.getDate(7)); list.add(m); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return list; } }
package Dao; import java.util.Date; public class Msg { private int msgid; private String username; private String title; private String msgcontent; private int state; private String sendto; private Date msg_create_date; @Override public String toString() { return "Msg [msgid=" + msgid + ", username=" + username + ", title=" + title + ", msgcontent=" + msgcontent + ", state=" + state + ", sendto=" + sendto + ", msg_create_date=" + msg_create_date + "]"; } public int getMsgid() { return msgid; } public void setMsgid(int msgid) { this.msgid = msgid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMsgcontent() { return msgcontent; } public void setMsgcontent(String msgcontent) { this.msgcontent = msgcontent; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getSendto() { return sendto; } public void setSendto(String sendto) { this.sendto = sendto; } public Date getMsg_create_date() { return msg_create_date; } public void setMsg_create_date(Date msg_create_date) { this.msg_create_date = msg_create_date; } }
package Dao; public class User { int id; String uname; String upwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } }
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> function validate() { if (loginForm.uname.value == "") { alert("账号不能为空!"); return; } if (loginForm.upwd.value == "") { alert("密码不能为空!"); return; } loginForm.submit(); } </script> <form name="loginForm" action="dologin.jsp" method="post"> <b>用户名:</b> <input type="text" name="uname"><br> <b>密码:</b> <input type="password" name="upwd"><br> <input type="button" value="登录" onClick="validate()"> <a href="reg.jsp">注册</a> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="Dao.dao"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String name = new String(uname.getBytes("ISO-8859-1"), "utf-8"); dao ud=new dao(); if(ud.login(name, upwd)){ session.setAttribute("uname", name); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ out.print("登陆失败,三面后跳回登陆页..."); response.setHeader("refresh", "3;url=login.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> <%@ page import="Dao.msgDao"%> <%@ page import="Dao.Msg"%> <style type="text/css"> table { border: 1px black solid; width: 500px; } tr{ border: 1px black solid; } </style> </head> <body> <% String name = (String) session.getAttribute("uname"); %> <% out.println(name); %>的邮箱: <br> <% msgDao m = new msgDao(); List<Msg> list = m.getMailByReceiver(name); %> <table> <tr> <td>发件人</td> <td>主题</td> <td>状态</td> <td>时间</td> </tr> <% for (int i = 0; i < list.size(); i++) { %> <tr> <td><%=list.get(i).getUsername()%></td> <td><%=list.get(i).getTitle()%></td> <td> <% if (list.get(i).getState() == 0) { %><img src="image/weidu.png" /> <% } else { %> <img src="image/yidu.png" /> <% } %> </td> <td><%=list.get(i).getMsg_create_date()%></td> <% } %> </table> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="doreg.jsp" method="post"> <b>用户名</b> <input type="text" name="uname" /> <br /> <b>密码</b> <input type="text" name="upwd" /> <br /> <input type="submit" value="注册" /> <a href="login.jsp">登录</a> </form> </body> </html>