jsp第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)

邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层
entity,dao包
package com.gd.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/email?autoReconnect=true&useSSL=false", "root", "123456"); } 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(); } } }
package com.gd.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); ps.setString(2, pwd); ResultSet rs=ps.executeQuery(); if(rs.next()) f=true; closeAll(conn,ps,rs); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return f; } public void reg(String uname,String upwd){ Connection conn=getConnection(); PreparedStatement ps=null; try { String sql="insert into users(uname,upwd) values(?,?)"; ps=conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ps.executeUpdate(); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); }finally{ closeAll(conn,ps,null); } } }
package com.gd.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 com.gd.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 msg where sendto=?"; 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(); 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_creat_date(rs.getDate(7)); list.add(m); } } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); }finally{ closeAll(conn,ps,rs); } return list; } }
package com.gd.entity; import java.sql.Date; public class Msg { private int msgid; private String username; private String title; private String msgcontent; private int state; private String sendto; Date msg_creat_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_creat_date() { return msg_creat_date; } public void setMsg_creat_date(Date msg_creat_date) { this.msg_creat_date = msg_creat_date; } public Msg(int msgid, String username, String title, String msgcontent, int state, String sendto, Date msg_creat_date) { super(); this.msgid = msgid; this.username = username; this.title = title; this.msgcontent = msgcontent; this.state = state; this.sendto = sendto; this.msg_creat_date = msg_creat_date; } public Msg() { super(); } }
package com.gd.entity; public class Users { private int id; private String uname; private 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; } public Users(int id, String uname, String upwd) { super(); this.id = id; this.uname = uname; this.upwd = upwd; } public Users() { super(); } }
3.登陆,注册,登陆后显示全部邮件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>login</title> </head> <body> <p>欢迎来到登录页面!!</p><br> <form name="form" action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname"><br> 密码:<input type="password" name="upwd"><br> <input type="submit" value="登录" > <br> <a href="zhuce.jsp">注册新用户</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>register</title> </head> <body> <p>欢迎来到注册界面</p> <form name="form" action="dozhuce.jsp" method="post"> 姓名:<input type="text" name="uname"><br> 密码:<input type="password" name="upwd"><br> <input type="submit" value="注册完成" > <br> <a href="dozhuce.jsp">返回登录界面</a> </form> </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>do login</title> </head> <body> <% request.setCharacterEncoding("utf-8"); 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", "3;url=login.jsp"); } %> </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>do register</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 import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>main</title> </head> <body> <%String uname=(String)session.getAttribute("uname"); %> 欢迎<%=uname %> <table border="1"> <tr> <td>发件人</td> <td>主题</td> <td>状态</td> <td>时间</td> </tr> <% MsgDao md=new MsgDao(); List<Msg> list=md.getMailByReceiver(uname); for(int i=0;i<list.size();i++){ %> <tr> <td><%out.print(list.get(i).getUsername().toString());%></td> <td><%out.print(list.get(i).getTitle().toString()); %></td> <td><%if(list.get(i).getState()==1){ out.print("<img src='img/1.png'></img>"); }else{ out.print("<img src='img/0.png'></img>"); } %></td> <td><%out.print(list.get(i).getMsg_creat_date().toString()); %></td> </tr> <%} %> </table> </body> </html>




浙公网安备 33010602011771号