JSP第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
BaseDao.java
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/test", "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(); } } }
MsgDao.java
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; import entity.Msg; public class MsgDao extends BaseDao{ //关于邮件的增删改查 //添加邮件(写邮件,回复邮件都调用,邮件状态为0,时间为系统当前时间) //根据id删除邮件 //根据id修改邮件状态(统一改成1) //根据收件人查看全部邮件 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对象,对象放到集合中 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; } }
UserDao.java
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 users where uname=? and upwd=?"; PreparedStatement ps; try { ps = conn.prepareStatement(sql); ps.setString(1, name);//第一个?赋值为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 int logon(String uname, int upwd) { Connection conn = getConnection(); PreparedStatement ps = null; int r = -1; try { String sql = "insert into users(uname,upwd) values(?,?)"; // 2个占位符 // 4.执行SQL语句 ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setInt(2, upwd); r = ps.executeUpdate();// 增删改都用这个 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } return r; } }
Msg.java
package entity; 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; } }
Users.java
package entity; public class Users { 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; } }
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'student.jsp' starting page</title> </head> <% %> <body> <form action="login_link.jsp"> 姓名:<input type="text" name="name" /> <br>密码:<input type="password" name="pwd" /> <br><input type="submit" value="登录" /> </form> <form action="logon.jsp"> <input type="submit" value="注册" /> </form> </body> </html>
login_link.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <%@ page import="dao.UsersDao"%> <% String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); String uname = new String(name.getBytes("ISO-8859-1"), "utf-8"); try { UsersDao usersdao = new UsersDao(); if (usersdao.login(uname, pwd)) { String n = (String) session.getAttribute("uname"); if (n == null) { session.setAttribute("uname", name); } request.getRequestDispatcher("mail.jsp").forward(request, response); } else { out.println("登陆失败!"); out.println("三秒后跳转到登录页面..."); response.setHeader("refresh", "3;url=login.jsp"); } } catch (Exception e) { out.println("异常!!"); out.println("三秒后跳转到登录页面..."); response.setHeader("refresh", "3;url=login.jsp"); } %> <body> <a href="login.jsp">返回</a> </body> </html>
logon.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <% %> <body> <h3>请输入信息来完成注册...</h3> <form action="logon_link.jsp"> 用户名:<input type="text" name="name" /> 密码:<input type="password" name="pwd" /> <input type="submit" value="注册" /> </form> <a href="login.jsp">返回</a> </body> </html>
logon_link.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'slogon_link.jsp' starting page</title> </head> <%@ page import="dao.UsersDao"%> <% String name = request.getParameter("name"); String upwd = request.getParameter("pwd"); String uname=new String(name.getBytes("ISO-8859-1"),"utf-8"); int pwd = Integer.parseInt(upwd); UsersDao usersdao=new UsersDao(); if (usersdao.logon(uname, pwd) != -1) { out.println("注册成功!"); out.println("三秒后跳转到登录页..."); response.setHeader("refresh", "3;url=login.jsp"); } else { out.println("注册失败!"); out.println("三秒后跳转到登录页..."); response.setHeader("refresh", "3;url=login.jsp"); } %> <body> </body> </html>
mail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> <%@ page import="dao.MsgDao"%> <%@ page import="entity.Msg"%> <style type="text/css"> table { border: 1px black solid; width: 500px; } </style> </head> <body> <% String name = (String) session.getAttribute("uname"); String uname = new String(name.getBytes("ISO-8859-1"), "utf-8"); %> <% out.println(uname); %>的邮箱: <br> <% MsgDao m = new MsgDao(); List<Msg> list = m.getMailByReceiver(uname); %> <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>