JSP第九次作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
CREATE TABLE euser ( uid INT auto_increment PRIMARY KEY, uname VARCHAR (10), upwd VARCHAR (10) ) CREATE TABLE mail ( eid INT auto_increment PRIMARY KEY, sender VARCHAR (10), addressee VARCHAR (10), title VARCHAR (10), content VARCHAR (10), sending_time datetime, state INT ) INSERT INTO mail () VALUES ( 2, 'mm', 'dd', '资料', 'JSP', '2022-05-15 ', 1 );
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.Date; import java.util.List; import entity.Msg; public class MsgDao extends BaseDao { public List<Msg> getMailByReceiver(String name) { List<Msg> list = new ArrayList<Msg>(); Connection conn = getConnection(); String sql = "select * from mail where addressee=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); rs = ps.executeQuery(); while (rs.next()) { Msg m = new Msg(); int eid = rs.getInt("eid"); String sender = rs.getString("sender"); String addressee = rs.getString("addressee"); String title = rs.getString("title"); String content = rs.getString("content"); Date sending_time = rs.getDate("sending_time"); int state = rs.getInt("state"); m.setEid(eid); m.setSender(sender); m.setAddressee(addressee); m.setTitle(title); m.setContent(content); m.setSending_time(sending_time); m.setState(state); list.add(m); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return list; } } package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UsersDao 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); } } }
<%@ 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> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="dao.UsersDao"%> <!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"); UsersDao ud=new UsersDao(); ud.reg(uname, upwd); request.getRequestDispatcher("login.jsp").forward(request, response); %> </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> <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.UsersDao"%> <!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"); UsersDao ud=new UsersDao(); if(ud.login(uname, upwd)){ session.setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ out.print("登陆失败,即将跳回登陆页....."); response.setHeader("refresh", "5;url=login.jsp"); } %> </body> </html> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="dao.MsgDao"%> <%@ page import="entity.Msg"%> <%@ page import="java.util.List"%> <%@ page import="java.util.ArrayList"%> <!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> <style> table { border-collapse: collapse; width:300px; } table, table tr th, table tr td { border: 1px solid #000000; } table td{ text-align:center; } img{ width:30px; } </style> </head> <body> <%String uname=(String)session.getAttribute("uname"); %> 首页!!欢迎你<%=uname %> <table border="1"> <tr> <td>发件人</td><td>主题</td><td>状态</td><td>时间</td> <% MsgDao md = new MsgDao(); List<Msg> list = md.getMailByReceiver(uname); for (int i = 0; i < list.size(); i++) { out.print("<tr><td>" + list.get(i).getSender() + "</td>"); out.print("<td>" + list.get(i).getTitle() + "</td>"); if (list.get(i).getState() == 1) { out.print("<td><img src=\"img/wd.png\"/></td>"); } else { out.print("<td><img src=\"img/yd.png\"/></td>"); } out.print("<td>" + list.get(i).getSending_time() + "</td></tr>"); } %> </tr> </table> </body> </html>