JSP第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
1 CREATE TABLE euser ( 2 uid INT PRIMARY KEY auto_increment, 3 uname VARCHAR (10), 4 upwd VARCHAR (10) 5 ) 6 CREATE TABLE msg ( 7 eid INT PRIMARY KEY auto_increment, 8 sender VARCHAR (10), 9 addressee VARCHAR (10), 10 title VARCHAR (10), 11 content VARCHAR (10), 12 send_time datetime, 13 state INT 14 ) 15 INSERT INTO msg () 16 VALUES 17 ( 18 101, 19 '大哥', 20 '小弟', 21 '学习资料', 22 'JSP', 23 '2022-5-11 12:12:12', 24 1 25 ); 26 INSERT INTO msg () 27 VALUES 28 ( 29 102, 30 '大哥', 31 '二弟', 32 '学习资料', 33 'Java', 34 '2022-5-11 12:21:21', 35 1 36 ); 37 INSERT INTO msg () 38 VALUES 39 ( 40 103, 41 '大哥', 42 '小弟', 43 '学习资料', 44 'JSP', 45 '2022-5-11 12:00:12', 46 0 47 ); 48 INSERT INTO msg () 49 VALUES 50 ( 51 104, 52 '大哥老婆', 53 '大哥', 54 '学习资料', 55 'Java', 56 '2022-5-11 12:21:55', 57 1 58 );
2.建model层
entity,dao包
1 package Dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.List; 9 10 import javax.naming.Context; 11 import javax.naming.InitialContext; 12 import javax.naming.NamingException; 13 import javax.sql.DataSource; 14 15 public class baseDao { 16 //获取连接 17 protected Connection getConnection(){ 18 Connection conn=null; 19 try { 20 Class.forName("com.mysql.jdbc.Driver"); 21 // 2.建立连接 22 conn = DriverManager.getConnection( 23 "jdbc:mysql://localhost:3306/class", "root", "root"); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 return conn; 28 } 29 //关闭连接 30 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ 31 try { 32 if(rs != null) 33 rs.close(); 34 if(ps != null) 35 ps.close(); 36 if(con != null) 37 con.close(); 38 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 44 }
1 package Dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public class dao extends baseDao{ 9 10 public boolean login(String name,String pwd){ 11 boolean f=false; 12 Connection conn=getConnection(); 13 String sql="select * from euser where uname=? and upwd=?"; 14 PreparedStatement ps; 15 try { 16 ps = conn.prepareStatement(sql); 17 ps.setString(1, name); 18 ps.setString(2, pwd); 19 ResultSet rs=ps.executeQuery(); 20 if(rs.next()) 21 f=true; 22 closeAll(conn, ps, rs); 23 24 25 } catch (SQLException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 return f; 30 } 31 32 public void reg(String uname, String upwd) { 33 Connection conn = getConnection(); 34 PreparedStatement ps = null; 35 try { 36 String sql = "insert into euser(uname,upwd) values(?,?)"; 37 // 4.执行SQL语句 38 ps = conn.prepareStatement(sql); 39 ps.setString(1, uname); 40 ps.setString(2, upwd); 41 ps.executeUpdate(); 42 } catch (SQLException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } finally { 46 closeAll(conn, ps, null); 47 } 48 49 } 50 51 52 }
1 package 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.List; 9 10 public class msgDao extends baseDao{ 11 12 public List<Msg> getMailByReceiver(String name){ 13 List<Msg> list=new ArrayList<Msg>(); 14 Connection con=getConnection(); 15 String sql="select * from msg where username=?"; 16 PreparedStatement ps=null; 17 ResultSet rs=null; 18 try { 19 ps = con.prepareStatement(sql); 20 ps.setString(1, name); 21 rs=ps.executeQuery(); 22 while(rs.next()){ 23 Msg m=new Msg(); 24 m.setMsgid(rs.getInt(1)); 25 m.setUsername(rs.getString(2)); 26 m.setTitle(rs.getString(3)); 27 m.setMsgcontent(rs.getString(4)); 28 m.setState(rs.getInt(5)); 29 m.setSendto(rs.getString(6)); 30 m.setMsg_create_date(rs.getDate(7)); 31 list.add(m); 32 } 33 34 } catch (SQLException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 }finally{ 38 closeAll(con, ps, rs); 39 } 40 return list; 41 } 42 43 }
1 package Dao; 2 import java.util.Date; 3 4 public class Msg { 5 private int msgid; 6 private String username; 7 private String title; 8 private String msgcontent; 9 private int state; 10 private String sendto; 11 private Date msg_create_date; 12 13 @Override 14 public String toString() { 15 return "Msg [msgid=" + msgid + ", username=" + username + ", title=" 16 + title + ", msgcontent=" + msgcontent + ", state=" + state 17 + ", sendto=" + sendto + ", msg_create_date=" + msg_create_date 18 + "]"; 19 } 20 21 public int getMsgid() { 22 return msgid; 23 } 24 25 public void setMsgid(int msgid) { 26 this.msgid = msgid; 27 } 28 29 public String getUsername() { 30 return username; 31 } 32 33 public void setUsername(String username) { 34 this.username = username; 35 } 36 37 public String getTitle() { 38 return title; 39 } 40 41 public void setTitle(String title) { 42 this.title = title; 43 } 44 45 public String getMsgcontent() { 46 return msgcontent; 47 } 48 49 public void setMsgcontent(String msgcontent) { 50 this.msgcontent = msgcontent; 51 } 52 53 public int getState() { 54 return state; 55 } 56 57 public void setState(int state) { 58 this.state = state; 59 } 60 61 public String getSendto() { 62 return sendto; 63 } 64 65 public void setSendto(String sendto) { 66 this.sendto = sendto; 67 } 68 69 public Date getMsg_create_date() { 70 return msg_create_date; 71 } 72 73 public void setMsg_create_date(Date msg_create_date) { 74 this.msg_create_date = msg_create_date; 75 } 76 77 }
1 package Dao; 2 3 public class User { 4 int id; 5 String uname; 6 String upwd; 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id = id; 12 } 13 public String getUname() { 14 return uname; 15 } 16 public void setUname(String uname) { 17 this.uname = uname; 18 } 19 public String getUpwd() { 20 return upwd; 21 } 22 public void setUpwd(String upwd) { 23 this.upwd = upwd; 24 } 25 26 }
3.登陆,注册
登录显示全部信息
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 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 25 <b>用户名:</b> <input type="text" name="uname"><br> <b>密码:</b> 26 <input type="password" name="upwd"><br> <input 27 type="button" value="登录" onClick="validate()"> <a 28 href="reg.jsp">注册</a> 29 </form> 30 </body> 31 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="Dao.dao"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 13 String uname = request.getParameter("uname"); 14 String upwd = request.getParameter("upwd"); 15 String name = new String(uname.getBytes("ISO-8859-1"), "utf-8"); 16 dao ud=new dao(); 17 if(ud.login(name, upwd)){ 18 session.setAttribute("uname", name); 19 request.getRequestDispatcher("main.jsp").forward(request, response); 20 }else{ 21 out.print("登陆失败,三面后跳回登陆页..."); 22 response.setHeader("refresh", "3;url=login.jsp"); 23 } 24 %> 25 </body> 26 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 3 <html> 4 <head> 5 6 <title></title> 7 <%@ page import="Dao.msgDao"%> 8 <%@ page import="Dao.Msg"%> 9 <style type="text/css"> 10 table { 11 border: 1px black solid; 12 width: 500px; 13 } 14 tr{ 15 border: 1px black solid; 16 } 17 </style> 18 </head> 19 20 <body> 21 22 <% 23 String name = (String) session.getAttribute("uname"); 24 %> 25 <% 26 out.println(name); 27 %>的邮箱: 28 <br> 29 <% 30 msgDao m = new msgDao(); 31 List<Msg> list = m.getMailByReceiver(name); 32 %> 33 <table> 34 <tr> 35 <td>发件人</td> 36 <td>主题</td> 37 <td>状态</td> 38 <td>时间</td> 39 </tr> 40 <% 41 for (int i = 0; i < list.size(); i++) { 42 %> 43 <tr> 44 <td><%=list.get(i).getUsername()%></td> 45 <td><%=list.get(i).getTitle()%></td> 46 <td> 47 <% 48 if (list.get(i).getState() == 0) { 49 %><img src="image/weidu.png" /> <% 50 } else { 51 %> <img src="image/yidu.png" /> <% 52 } 53 %> 54 </td> 55 <td><%=list.get(i).getMsg_create_date()%></td> 56 <% 57 } 58 %> 59 60 </table> 61 </body> 62 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="doreg.jsp" method="post"> 11 <b>用户名</b> 12 <input type="text" name="uname" /> 13 <br /> 14 <b>密码</b> 15 <input type="text" name="upwd" /> 16 <br /> 17 <input type="submit" value="注册" /> 18 <a href="login.jsp">登录</a> 19 </form> 20 </body> 21 </html>
4.截图演示
登陆前

登陆后


浙公网安备 33010602011771号