JSP第九次作业
BaseDao
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/users", "root", "admin");
} 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
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.Date; import java.util.List; import com.gd.entity.Msg; public class MsgDao extends BaseDao { // 发送,回复---insert操作 public void addMsg(Msg msg) { try { Connection con = getConnection(); String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?);"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, msg.getUsername());// 给sql语句的问号赋值 ps.setString(2, msg.getTitle()); ps.setString(3, msg.getMsgcontent()); ps.setInt(4, 0);// 刚插入的邮件为未读邮件,直接赋值0 ps.setString(5, msg.getSendto()); ps.setDate(6, new java.sql.Date(new Date().getTime())); ps.executeUpdate(); closeAll(con, ps, null); } catch (SQLException ex) { ex.printStackTrace(); } } // 邮件列表 --select * from msg where username=.... public List<Msg> getMailByReceiver(String receiverName) { List<Msg> list = new ArrayList<Msg>(); try { Connection con = getConnection();// 获取连接 String sql = "select * from msg where sendto=?";// 编写sql语句 PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, receiverName);// //给sql问号赋值 ResultSet rs = ps.executeQuery();// 执行查询 // 处理查询结果 while (rs.next()) { // 循环读取rs结果集,每一行作为一个msg对象,放入list集合中 Msg msg = new Msg(); msg.setMsgid(rs.getInt("msgid")); msg.setMsgcontent(rs.getString("msgcontent")); msg.setMsg_create_date(rs.getDate("msg_create_date")); msg.setSendto(rs.getString("sendto")); msg.setState(rs.getInt("state")); msg.setTitle(rs.getString("title")); msg.setUsernname(rs.getString("username")); list.add(msg); } closeAll(con, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } // 根据id查内容 select public Msg getMailById(int id) { Msg msg = new Msg(); try { Connection con = getConnection();// 获取连接 String sql = "select * from msg where msgid=?";// 编写sql语句 PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id);// //给sql问号赋值 ResultSet rs = ps.executeQuery();// 执行查询 // 处理查询结果 while (rs.next()) { // 循环读取rs结果集,每一行作为一个msg对象,放入list集合中 msg.setMsgid(rs.getInt("msgid")); msg.setMsgcontent(rs.getString("msgcontent")); msg.setMsg_create_date(rs.getDate("msg_create_date")); msg.setSendto(rs.getString("sendto")); msg.setState(rs.getInt("state")); msg.setTitle(rs.getString("title")); msg.setUsernname(rs.getString("username")); } closeAll(con, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return msg; } public static void main(String[] args) { MsgDao md = new MsgDao(); md.delMsg(14); } // 阅读状态改变,,,未读 已读 update public void alterMsg(int id){ try { Connection con=getConnection(); String sql="update msg set state=1 where msgid=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); closeAll(con, ps, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 删除邮件 delete public void delMsg(int id){ try { Connection con=getConnection(); String sql="delete from msg where msgid=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); closeAll(con, ps, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
UsersDao
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.gd.entity.Users; public class UsersDao extends BaseDao { // 登录功能 public boolean login(String uname, String upwd) throws SQLException { // 获取连接 Connection conn = getConnection(); // 编写sql语句 String sql = "select * from users where username=? and password=?"; // 执行sql语句 PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ResultSet rs = ps.executeQuery(); if (rs.next()) { closeAll(conn, ps, rs); return true; } else { closeAll(conn, ps, rs); return false; } } public void addUsers(Users users) { try { Connection con = getConnection(); String sql = "insert into users(username,password,email) values(?,?,?);"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, users.getUsername()); ps.setString(2, users.getPassword()); ps.setString(3, users.getEmail()); ps.executeUpdate(); closeAll(con, ps, null); } catch (SQLException ex) { ex.printStackTrace(); } } }
delete
<%@page import="com.gd.entity.Users"%> <%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% int id = Integer.parseInt(request.getParameter("delid")); MsgDao md = new MsgDao(); md.delMsg(id); request.getRequestDispatcher("main.jsp").forward(request, response); %>
detail
<%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% int id = Integer.parseInt(request.getParameter("id")); MsgDao md = new MsgDao(); Msg m = md.getMailById(id); md.alterMsg(m.getMsgid()); %> <p> 题目:<%=m.getTitle()%></p> <p> 来自:<%=m.getUsername()%></p> <p> 时间:<%=m.getMsg_create_date()%></p> <p> 内容:<%=m.getMsgcontent()%></p> 回复 <a href="main.jsp">返回</a> </body> </html>
do
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.gd.entity.Users"%> <%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@page import="com.gd.dao.UsersDao"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'do.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setCharacterEncoding("utf-8"); Users a=new Users(); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String email = request.getParameter("uemail"); a.setUsername(uname); a.setPassword(upwd); a.setEmail(email); UsersDao as=new UsersDao(); as.addUsers(a); request.getRequestDispatcher("index.jsp").forward(request, response); %> </body> </html>
dologin
<%@page import="com.gd.entity.Users"%> <%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); UsersDao ud = new UsersDao(); if(request.getParameter("validationCode1").equals(request.getParameter("validationCode"))) { if (ud.login(uname, upwd)){ //登录成功,创建User对象,并放入session Users u=new Users(); u.setUsername(uname); u.setPassword(upwd); session.setAttribute("user", u); request.getRequestDispatcher("main.jsp").forward(request, response); } else{ response.sendRedirect("index.jsp"); } } else{ response.sendRedirect("index.jsp"); } %>
dowrite
<%@page import="com.gd.entity.Users"%> <%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% Users u = (Users) session.getAttribute("user"); String uname = request.getParameter("sjr"); String title = request.getParameter("title"); String content = request.getParameter("content"); Msg m = new Msg(); m.setUsernname(u.getUsername());//发件人 登陆者 m.setSendto(uname);//收件人: 上一页面填写的 m.setTitle(title); m.setMsgcontent(content); MsgDao md = new MsgDao(); md.addMsg(m); request.getRequestDispatcher("main.jsp").forward(request, response); %>
index
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script type="text/javascript"> function mycheck() { //判断验证码是否为空 if (form1.validationCode.value==""){ alert("验证码不能为空,请输入验证码!"); form1.validationCode.focus(); return; } //判断验证码是否正确 if (form1.validationCode.value != form1.validationCode1.value) { alert("请输入正确的验证码!!"); form1.validationCode.focus(); return; } form1.submit1(); } </script> <body> <form action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname" value="kitty" /><Br> 密码 :<input type="password" name="upwd" value="777"/><br> <br> 验证码:<input type="text" name="validationCode" onKeyDown="if(event.keyCode==13){form1.submit.focus();}" size="6"> <% int intmethod1 = (int) ((((Math.random()) * 11)) - 1); int intmethod2 = (int) ((((Math.random()) * 11)) - 1); int intmethod3 = (int) ((((Math.random()) * 11)) - 1); int intmethod4 = (int) ((((Math.random()) * 11)) - 1); //将得到的随机数进行连接 String intsum = intmethod1 +""+ intmethod2+intmethod3+intmethod4; %> <!-- 设置隐藏域,验证比较时使用--> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <!-- 将图片名称与得到的随机数相同的图片显示在页面上 --> <img src="images/<%=intmethod1%>.png"> <img src="images/<%=intmethod2%>.png"> <img src="images/<%=intmethod3%>.png"> <img src="images/<%=intmethod4%>.png"> <br> <input type="submit" value="登录"> <a href="register.jsp">注册</a> </form> </body> </html>
logout
<%@page import="com.gd.entity.Users"%> <%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% session.removeAttribute("user"); request.getRequestDispatcher("index.jsp").forward(request, response); %>
main
<%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@page import="com.gd.entity.Users"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 欢迎页面!!!欢迎你!!!<% Users u = (Users) session.getAttribute("user"); out.print(u.getUsername()); MsgDao md = new MsgDao(); List<Msg> list = md.getMailByReceiver(u.getUsername()); out.print(list.size()); %> <a href="write.jsp">写邮件</a> <a href="logout.jsp">退出</a> <table border="1" width="1000"> <tr> <td>邮件id</td> <td>发件人</td> <td>标题</td> <td>收件人</td> <td>状态</td> <td>时间</td> <td> </td> <td> </td> </tr> <% for (int i = 0; i < list.size(); i++) { %> <tr> <td><%=list.get(i).getMsgid()%></td> <td><%=list.get(i).getUsername()%></td> <td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle()%></a> </td> <td><%=list.get(i).getSendto()%></td> <td> <% if (list.get(i).getState() == 0) { %> <img src="images/sms_unReaded.png"></img> <% } else { %> <img src="images/sms_readed.png"></img> <% } %> </td> <!-- 0已读,1未读 --> <td><%=list.get(i).getMsg_create_date()%></td> <td><a href="write.jsp?mailto=<%=list.get(i).getUsername() %>">回复</a> </td> <td> <a href="delete.jsp?delid=<%=list.get(i).getMsgid()%>"> 删除</a> </td> </tr> <% } %> </table> </body> </html>
register
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="do.jsp" method="post"> 用户名:<input type="text" name="uname" /><Br> 密码 :<input type="password" name="upwd" /><br> email:<input type="text" name="uemail" /><br> <input type="submit" value="确认"> </form> </body> </html>
write
<%@page import="com.gd.entity.Msg"%> <%@page import="com.gd.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="dowrite.jsp" method="post"> <p> 收件人:<input type="text" name="sjr" value=<%=request.getParameter("mailto") %> /> </p> <p> 标题:<input type="text" name="title" /> </p> <p> 内容:<input type="text" name="content" /> </p> <input type="submit" value="发送"> </form> <a href="main.jsp">返回</a> </body> </html>
注册