JSP第九次作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
dao
1 package com.gd.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 9 public class BaseDao { 10 // 获取连接 11 protected Connection getConnection() { 12 Connection conn = null; 13 try { 14 Class.forName("com.mysql.jdbc.Driver"); 15 // 2.建立连接 16 conn = DriverManager.getConnection( 17 "jdbc:mysql://localhost:3306/test", "root", "123456"); 18 } catch (Exception e) { 19 e.printStackTrace(); 20 } 21 return conn; 22 } 23 24 // 关闭连接 25 protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs) { 26 try { 27 if (rs != null) 28 rs.close(); 29 if (ps != null) 30 ps.close(); 31 if (con != null) 32 con.close(); 33 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 } 37 } 38 }
1 package com.gd.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 import com.gd.entity.Msg; 11 12 public class MsgDao extends BaseDao { 13 public List<Msg> listAll(String username) throws SQLException{ 14 List<Msg> list =new ArrayList<Msg>(); 15 Connection conn =getConnection(); 16 String sql="select * from msg where username=?"; 17 PreparedStatement pred =null; 18 19 ResultSet rest=null; 20 try { 21 pred=conn.prepareStatement(sql); 22 pred.setString(1, username); 23 rest=pred.executeQuery(); 24 while (rest.next()) { 25 Msg msg=new Msg(); 26 msg.setMsgid(rest.getInt("msgid")); 27 msg.setUsername(rest.getString("username")); 28 msg.setTitle(rest.getString("title")); 29 msg.setMsgcontent(rest.getString("msgcontent")); 30 msg.setState(rest.getInt("state")); 31 msg.setSendto(rest.getString("sendto")); 32 msg.setMsg_create_date(rest.getDate("msg_create_date")); 33 list.add(msg); 34 } 35 } catch (Exception e) { 36 // TODO: handle exception 37 } 38 finally{ 39 closeAll(conn, pred, rest); 40 } 41 return list; 42 } 43 44 }
1 package com.gd.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 UsersDao extends BaseDao { 9 public boolean login(String uname, String upwd) throws SQLException { 10 boolean f = false; 11 Connection conn = getConnection(); 12 PreparedStatement pred = null; 13 ResultSet rest = null; 14 String sql = "select * from users where uname=? and upwd=?"; 15 try { 16 pred = conn.prepareStatement(sql); 17 pred.setString(1, uname); 18 pred.setString(2, upwd); 19 rest = pred.executeQuery(); 20 while (rest.next()) { 21 f = true; 22 } 23 } catch (Exception e) { 24 // TODO: handle exception 25 } finally { 26 closeAll(conn, pred, rest); 27 } 28 return f; 29 } 30 31 public boolean id(Integer id) throws SQLException { 32 boolean f = false; 33 Connection conn = getConnection(); 34 PreparedStatement pred = null; 35 ResultSet rest = null; 36 String sql = "select * from users where id=?"; 37 try { 38 pred = conn.prepareStatement(sql); 39 pred.setInt(1, id); 40 rest = pred.executeQuery(); 41 while (rest.next()) { 42 f = true; 43 } 44 } catch (Exception e) { 45 // TODO: handle exception 46 } finally { 47 closeAll(conn, pred, rest); 48 } 49 return f; 50 } 51 52 public void zhuce(Integer id, String uname, String upwd) { 53 Connection conn = getConnection(); 54 PreparedStatement pred = null; 55 try { 56 String sql = "insert into users(id,uname,upwd) values(?,?,?)"; 57 pred = conn.prepareStatement(sql); 58 pred.setInt(1, id); 59 pred.setString(2, uname); 60 pred.setString(3, upwd); 61 pred.executeUpdate(); 62 63 } catch (Exception e) { 64 // TODO: handle exception 65 } finally { 66 closeAll(conn, pred, null); 67 } 68 } 69 70 }
entity
1 package com.gd.entity; 2 3 import java.util.Date; 4 5 public class Msg { 6 private Integer msgid; 7 private String username; 8 private String title; 9 private String msgcontent; 10 private Integer state; 11 private String sendto; 12 private Date msg_create_date; 13 14 public Integer getMsgid() { 15 return msgid; 16 } 17 public void setMsgid(Integer msgid) { 18 this.msgid = msgid; 19 } 20 public String getUsername() { 21 return username; 22 } 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 public String getTitle() { 27 return title; 28 } 29 public void setTitle(String title) { 30 this.title = title; 31 } 32 public String getMsgcontent() { 33 return msgcontent; 34 } 35 public void setMsgcontent(String msgcontent) { 36 this.msgcontent = msgcontent; 37 } 38 public Integer getState() { 39 return state; 40 } 41 public void setState(Integer state) { 42 this.state = state; 43 } 44 public String getSendto() { 45 return sendto; 46 } 47 public void setSendto(String sendto) { 48 this.sendto = sendto; 49 } 50 public Date getMsg_create_date() { 51 return msg_create_date; 52 } 53 public void setMsg_create_date(Date msg_create_date) { 54 this.msg_create_date = msg_create_date; 55 } 56 57 }
1 package com.gd.entity; 2 3 public class Users { 4 private Integer id; 5 private String uname; 6 private String upwd; 7 public Integer getId() { 8 return id; 9 } 10 public void setId(Integer 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 27 }
jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 request.setCharacterEncoding("utf-8"); 4 response.setCharacterEncoding("utf-8"); 5 %> 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 7 <html> 8 <head> 9 <title>My JSP 'zhuce.jsp' starting page</title> 10 </head> 11 <body> 12 <script type="text/javascript"> 13 function dozhuce() { 14 if (loginform2.id.value == "") { 15 alert("没有输入id"); 16 return; 17 } 18 if (loginform2.uname.value == "") { 19 alert("没有输入用户名"); 20 return; 21 } 22 if (loginform2.upwd.value == "") { 23 alert("没有输入密码"); 24 return; 25 } 26 if (loginform2.upwd2.value == "") { 27 alert("没有确认密码"); 28 return; 29 } 30 loginform2.submit(); 31 } 32 </script> 33 <form action="dozhuce.jsp" name="loginform2" method="post"> 34 id: <input type="number" name="id" /><br> 35 用户名:<input type="text" name="uname" /> <br> 36 密码:<input type="password" name="upwd" /> <br> 37 确认密码:<input type="password" name="upwd2" /><br> 38 <input type="button" value="注册" onclick="dozhuce()" /> 39 </form> 40 </body> 41 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="com.gd.dao.UsersDao"%> 3 <% 4 request.setCharacterEncoding("utf-8"); 5 response.setCharacterEncoding("utf-8"); 6 %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <title>My JSP 'dozhuce.jsp' starting page</title> 11 </head> 12 <body> 13 <% 14 String uid = request.getParameter("id"); 15 Integer id = Integer.parseInt(uid); 16 String uname = request.getParameter("uname"); 17 String upwd = request.getParameter("upwd"); 18 String upwd2 = request.getParameter("upwd2"); 19 UsersDao usersDao = new UsersDao(); 20 if (upwd.equals(upwd2)) { 21 if (usersDao.id(id)) { 22 out.print("已经有这个id了,即将跳回注册页....."); 23 response.setHeader("refresh", "5;url=zhuce.jsp"); 24 } else { 25 usersDao.zhuce(id, uname, upwd); 26 out.print("注册成功,即将跳回登录页....."); 27 response.setHeader("refresh", "5;url=login.jsp"); 28 } 29 } else { 30 out.print("两次密码不一致,即将跳回注册页....."); 31 response.setHeader("refresh", "5;url=zhuce.jsp"); 32 } 33 %> 34 </body> 35 </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 </head> 7 8 <body> 9 <script type="text/javascript"> 10 function dologin() { 11 if (loginform.uname.value == "") { 12 alert("没有输入用户名"); 13 return; 14 } 15 if (loginform.upwd.value == "") { 16 alert("没有输入密码"); 17 return; 18 } 19 loginform.submit(); 20 } 21 </script> 22 <form action="dologin.jsp" name="loginform" method="post"> 23 用户名:<input type="text" name="uname" /> <br> 24 密码:<input type="password" name="upwd" /> <br> 25 <input type="button" value="登录" onclick="dologin()" /> 26 <a href="zhuce.jsp">没有账号?立即注册</a> 27 </form> 28 </body> 29 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="sun.security.jgss.LoginConfigImpl"%> 3 <%@page import="com.gd.dao.UsersDao"%> 4 <% 5 request.setCharacterEncoding("utf-8"); 6 response.setCharacterEncoding("utf-8"); 7 %> 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <title>My JSP 'dologin.jsp' starting page</title> 12 </head> 13 14 <body> 15 <% 16 String uname = request.getParameter("uname"); 17 String upwd = request.getParameter("upwd"); 18 UsersDao ud = new UsersDao(); 19 if (ud.login(uname, upwd)) { 20 session.setAttribute("uname", uname); 21 request.getRequestDispatcher("index.jsp").forward(request, 22 response); 23 } else { 24 out.print("登录失败,5s后跳转登录页面"); 25 response.setHeader("refresh", "5;url=login.jsp"); 26 } 27 %> 28 </body> 29 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="java.util.List"%> 3 <%@page import="com.gd.dao.MsgDao"%> 4 <%@page import="com.gd.entity.Msg"%> 5 <%@page import="sun.security.jgss.LoginConfigImpl"%> 6 <%@page import="com.gd.dao.UsersDao"%> 7 <% 8 request.setCharacterEncoding("utf-8"); 9 response.setCharacterEncoding("utf-8"); 10 %> 11 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 12 <html> 13 <head> 14 <title>My JSP 'index.jsp' starting page</title> 15 </head> 16 <body> 17 <% 18 String uname = (String) session.getAttribute("uname"); 19 if (uname == null) { 20 out.print("您未登录,即将跳回登陆页....."); 21 response.setHeader("refresh", "5;url=login.jsp"); 22 } else { 23 %> 24 欢迎你<%=uname%> 25 26 27 28 <% 29 Msg msg = new Msg(); 30 MsgDao msgDao = new MsgDao(); 31 List<Msg> list = new ArrayList<Msg>(); 32 list = msgDao.listAll(uname); 33 %> 34 邮件列表: 35 <table width="600px" border="2px solid blue"> 36 <tr> 37 <th>id</th> 38 <th>发件人</th> 39 <th>标题</th> 40 <th>内容</th> 41 <th>已读未读状态</th> 42 <th>收件人</th> 43 <th>发送时间</th> 44 </tr> 45 <% 46 if (list.size() == 0) { 47 out.print("你没有邮件"); 48 } else { 49 for (Msg msg1 : list) { 50 %> 51 <tr> 52 <td> 53 <% 54 out.print(msg1.getMsgid()); 55 %> 56 </td> 57 <td> 58 <% 59 out.print(msg1.getUsername()); 60 %> 61 </td> 62 <td> 63 <% 64 out.print(msg1.getTitle()); 65 %> 66 </td> 67 <td> 68 <% 69 out.print(msg1.getMsgcontent()); 70 %> 71 </td> 72 <td> 73 <% 74 int state = msg1.getState(); 75 if (state == 1) { 76 %> <img src="image/yidu.png" /> <% 77 ; 78 } else { 79 %> <img src="image/weidu.png" /> <% 80 ; 81 } 82 %> 83 </td> 84 <td> 85 <% 86 out.print(msg1.getSendto()); 87 %> 88 </td> 89 <td> 90 <% 91 out.print(msg1.getMsg_create_date()); 92 %> 93 </td> 94 </tr> 95 <% 96 } 97 } 98 } 99 %> 100 </table> 101 </body> 102 </html>