jsp第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
1.建库,建表2个
#建users表 create table users( uid int(20) auto_increment primary key, uname varchar(20) , upwd varchar(20) ); #为users表插入数据 insert into users(uid,uname,upwd) values('1','zk','123'); insert into users(uid,uname,upwd) values('2','gl','456'); insert into users(uid,uname,upwd) values('3','dxy','789'); #建email表 create table email ( eid int(20) not null auto_increment primary key, ename varchar(20) not null , etitle varchar(50) not null, econtent varchar(100) not null, estate int(20) not null, efa varchar(20) not null, etime datetime not null ); #为email表插入数据 insert into email(eid,ename,etitle,econtent,estate,efa,etime) values('1','zk','通知','有一个非常紧急的通知','0','gl,'2022-5-1'); insert into email(eid,ename,etitle,econtent,estate,efa,etime) values('2','zk','作业','作业已完成','0',dxy','2022-5-3');
2.建model层、entity,dao包
package Email.entityPackage; public class Users { private int uid; private String uname; private String upwd; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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; } }
package Email.entityPackage; import java.util.Date; public class Email { private int eid; //id private String ename; //收件人 private String etitle; //标题 private String econtent; //内容 private int estate; //状态 private String efa; //发件人 private Date etime; //时间 public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getEtitle() { return etitle; } public void setEtitle(String etitle) { this.etitle = etitle; } public String getEcontent() { return econtent; } public void setEcontent(String econtent) { this.econtent = econtent; } public int getEstate() { return estate; } public void setEstate(int estate) { this.estate = estate; } public String getEfa() { return efa; } public void setEfa(String efa) { this.efa = efa; } public Date getEtime() { return etime; } public void setEtime(Date etime) { this.etime = etime; } }
package Email.daoPackage; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class BaseDao { //连接数据库 public Connection getConnection(){ Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); } catch (Exception e) { e.printStackTrace(); } return con; } //关闭数据库 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 (Exception e) { e.printStackTrace(); } } }
package Email.daoPackage; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class UsersDao extends BaseDao{ //登录 public boolean DengLu(String name,String pwd) { boolean f=false; Connection con=getConnection(); String sql="select * from users where uname=? and upwd=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs=ps.executeQuery(); if(rs.next()) f=true; } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return f; } //注册 public int ZhuCe(String name,String pwd){ int i=-1; Connection con=getConnection(); String sql="insert into users(uname,upwd) values(?,?)"; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); i=ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } return i; } }
package Email.daoPackage; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import Email.entityPackage.Email; public class EmailDao extends BaseDao{ // 1,插入邮件 // 2.删除邮件 // 3.修改邮件状态 // 4.按照接收者查询全部邮件 public List<Email> getAllEmail(String name){ List<Email> EmailList=new ArrayList<Email>(); Connection con=getConnection(); String sql="select * from email where ename=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ Email em=new Email(); em.setEid(rs.getInt(1)); em.setEname(rs.getString(2)); em.setEtitle(rs.getString(3)); em.setEcontent(rs.getString(4)); em.setEstate(rs.getInt(5)); em.setEfa(rs.getString(6)); em.setEtime(rs.getDate(7)); EmailList.add(em); } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return EmailList; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Email1.jsp</title> </head> <body> <form action="panduan1.jsp" method="post" name="one"> 请输入用户名:<input name="name" type="text"><br> 请输入密码: <input name="password" type="password"><br> <input type="button" value="登录" onclick="denglu()"> <a href="Email2.jsp">注册新用户 </a> <script type="text/javascript"> function denglu() { if (one.name.value == "") { alert("用户名不能为空!"); return; } if (one.password.value == "") { alert("密码不能为空!"); return; } one.submit(); } </script> </form> </body> </html>
<%@page import="Email.daoPackage.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>panduan1.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("name"); String upassword = request.getParameter("password"); UsersDao ud = new UsersDao(); if (ud.DengLu(uname, upassword)) { session.setAttribute("user", uname); request.getRequestDispatcher("main.jsp").forward(request,response); } else { out.print("登陆失败,3秒后返回登录页!"); response.setHeader("refresh", "3;url=Email1.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Email2.jsp</title> </head> <body> <form action="panduan2.jsp" method="post" name="two"> 请输入用户名:<input name="name" type="text"><br> 请输入密码: <input name="password1" type="password"><br> 再次输入密码:<input name="password2" type="password"><br> <input type="button" value="注册" onclick="zhuce()"> <a href="Email1.jsp">返回登录</a> <script type="text/javascript"> function zhuce() { if (two.name.value == "") { alert("用户名不能为空!"); return; } if (two.password1.value == "") { alert("密码不能为空!"); return; } if (two.password2.value == "") { alert("再次输入密码不能为空!"); return; } if (two.password1.value != two.password2.value) { alert("密码不一致,请重新输入!"); return; } two.submit(); } </script> </form> </body> </html>
<%@page import="Email.daoPackage.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>panduan2.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("name"); String upassword1 = request.getParameter("password1"); UsersDao ud = new UsersDao(); int i = ud.ZhuCe(uname, upassword1); if (i > 0) { out.print("注册成功,3秒后跳转登录页!"); response.setHeader("refresh", "3;url=Email1.jsp"); } else { out.print("注册失败,3秒后返回注册页!"); response.setHeader("refresh", "3;url=Email2.jsp"); } %> </body> </html>
<%@page import="Email.entityPackage.Email"%> <%@page import="Email.daoPackage.EmailDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>main.jsp</title> </head> <body> <% EmailDao mail=new EmailDao(); String user=(String) session.getAttribute("user"); List<Email> MailList=mail.getAllEmail(user); %> 欢迎你:<%=user %> <br><br> <table border="1px" cellpadding="15px" align="center"> <tr> <td>编号</td> <td>收件人</td> <td>标题</td> <td>内容</td> <td>状态</td> <td>发件人</td> <td>时间</td> </tr> <%for(int i=0;i<MailList.size();i++){ %> <tr> <td><%out.print(MailList.get(i).getEid()); %></td> <td><%out.print(MailList.get(i).getEname());%></td> <td><%out.print(MailList.get(i).getEtitle()); %></td> <td><%out.print(MailList.get(i).getEcontent()); %></td> <td><%out.print(MailList.get(i).getEstate()); %></td> <td><%out.print(MailList.get(i).getEfa()); %></td> <td><%out.print(MailList.get(i).getEtime()); %></td> </tr> <%} %> </table> </body> </html>