package com.gd.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 {
private static final String URL = "jdbc:mysql://localhost:3306/u";
private static final String USERNAME = "root";
private static final String PWD = "root";
//获取连接
public static Connection getCon() throws Exception {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
Connection con = DriverManager.getConnection(URL, USERNAME, PWD);
return con;
}
//关闭连接
public 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.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpSession;
public class UsersDao extends BaseDao {
// 登录功能
public boolean login(String uname, String upwd) throws Exception {
boolean flag = false;
// 获取连接
Connection conn = getCon();
// 编写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()) {
flag = true;
closeAll(conn, ps, rs);
return true;
} else {
closeAll(conn, ps, rs);
return false;
}
}
// public static void main(String[] args) {
// UsersDao ud=new UsersDao();
// try {
// System.out.println(ud.login("tom", "456"));
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}
package com.gd.entity;
public class Msg {
int msgid;
String usernname;
String title;
String msgcontent;
int state;
String sendto;
String msg_create_date;
public int getMsgid() {
return msgid;
}
public void setMsgid(int msgid) {
this.msgid = msgid;
}
public String getUsernname() {
return usernname;
}
public void setUsernname(String usernname) {
this.usernname = usernname;
}
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 String getMsg_create_date() {
return msg_create_date;
}
public void setMsg_create_date(String msg_create_date) {
this.msg_create_date = msg_create_date;
}
}
package com.gd.entity;
public class Users {
String username;
String password;
String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@page import="com.gd.entity.Msg"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String uname = (String) session.getAttribute("uname");
String newstate = null;
//获取邮件列表页面输入的标题
String contitle = request.getParameter("contitle");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加载驱动
String url = "jdbc:mysql://localhost:3306/u";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 连接对象
//conn = BaseDao.getConnection();
%>
<%
Msg msg = new Msg();
ps = conn.prepareStatement("select * from msg where username=?");
ps.setString(1, uname);
rs = ps.executeQuery();
while (rs.next()) {
%>
<form action="" name="huifu" method="post">
收件人: <%=rs.getString("sendto")%><br> <br>标题:<%=rs.getString("title")%><br>
<br> 正文:<%=rs.getString("msgcontent")%><br>
<%
//更新阅读状态
if (rs.getInt("state") == 1) {
ps = conn.prepareStatement("update msg set state=? where state=? and title=?");
// 执行sql语句
ps.setInt(1, 0);
ps.setInt(2, 1);
ps.setString(3, contitle);
ps.executeUpdate();
break;
}
%>
<%
}
%>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
//BaseDao.closeAll(conn, ps, rs);
try {
if (rs != null) {
rs.close();
}
} finally {
try {
if (ps != null) {
ps.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
}
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@page import="com.gd.entity.Msg"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String uname = (String) session.getAttribute("uname");
// 用户已登陆
%>
<%!
String DBDRIVER = "com.mysql.jdbc.Driver";
String DBURL = "jdbc:mysql://localhost:3306/u?characterEncoding=utf-8";
String DBUSER = "root";
String DBPASSWORD = "root";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
%>
<%
// 接收参数
int msgid = 0;
try {
msgid=Integer.parseInt(request.getParameter("id"));
} catch (Exception e) {
}
%>
<%
String sql = "delete from msg where msgid = ?";
boolean flag = false;
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
pstmt = conn.prepareStatement(sql);
// 设置删除条件
pstmt.setInt(1, msgid);
pstmt.executeUpdate();
pstmt.close();
conn.close();
flag = true;
} catch (Exception e) {
}
%>
<%
response.setHeader("refresh", "2;URL=index.jsp");
if (flag) {
%>
<form action="index.jsp" method="post">
<%
session.setAttribute("uname", uname);
%>
<%
out.print("删除成功");
%>
<input type="submit" value="退出登录" name="newyjlb">
</form>
<%
} else {
%>
邮件删除失败,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="index.jsp">这里</a>!!!
<%
}
%>
</body>
</html>
<%@page import="com.gd.dao.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
if ( session.getAttribute("uname") != null) {
// 用户已登陆
%>
<%
request.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname");
request.setAttribute(uname, "uname");
String upwd = request.getParameter("upwd");
UsersDao ud = new UsersDao();
if (ud.login(uname, upwd))
request.getRequestDispatcher("main.jsp").forward(request, response);
else
response.sendRedirect("index.jsp");
%>
<%
} else {
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh", "2;URL=index.jsp");
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="index.jsp">这里</a>!!!<br>
<%
}
%>
<%@ 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">
-->
<SCRIPT Language = "javascript">
//下面的副程序将执行资料检查
function isValid()
{
//下面的if判断语句将检查是否输入帐号资料
if (dologin.id.value == "")
{
window.alert("您必须完成帐号的输入!");
//显示错误信息
document.dologin.elements(0).focus();
//将光标移至帐号输入栏
return false;
}
//下面的if判断语句将检查是否输入帐号密码
if (dologin.password.value == "")
{
window.alert("您必须完成密码的输入!");
//显示错误信息
document.dologin.elements(1).focus();
//将光标移至密码输入栏
return false; //离开函数
}
dologin.submit(); //送出表单中的资料
}
</SCRIPT>
</head>
<body>
<form action="dologin.jsp" name="dologin.jsp" method="post" onSubmit="return isValid(this);">
用户名:<input type="text" name="uname" value="" /><Br>
密码 :<input type="password" name="upwd" value=""/><br>
<input type="submit" value="登录">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="com.gd.entity.Msg"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
//获取邮件列表页面输入的标题
String contitle = (String) session.getAttribute("newcontitle");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加载驱动
String url = "jdbc:mysql://localhost:3306/u";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 连接对象
//conn = BaseDao.getConnection();
%>
<%
Msg msg = new Msg();
String uuname = (String) session.getAttribute("uuname");
out.print("发件人:"+uuname);
ps = conn.prepareStatement("select * from msg where username= ? ");
ps.setString(1, uuname);
rs = ps.executeQuery();
while (rs.next()) {
%>
<form action="insert2.jsp" name="huifu" method="post">
收件人: <%=rs.getString("sendto")%><br> <br>
标题:<input type="text" name="ctitle"><br> <br>
正文:<input type="text" name="ccontent"><br>
<input type="submit" value="发送">
</form>
<%
}
%>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
} finally {
try {
if (ps != null) {
ps.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
}
}
%>
<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@page import="com.gd.entity.Msg"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String ctitle = request.getParameter("ctitle");
String ccontent = request.getParameter("ccontent");
//获取邮件列表页面输入的标题
String contitle = (String) session.getAttribute("newcontitle");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加载驱动
String url = "jdbc:mysql://localhost:3306/u";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 连接对象
//conn = BaseDao.getConnection();
%>
<%
Msg msg = new Msg();
String uuname = (String) session.getAttribute("uuname");
ps = conn.prepareStatement("select * from msg where username=?");
ps.setString(1, uuname);
rs = ps.executeQuery();
while (rs.next()) {
ps = conn.prepareStatement("insert into msg(username,title,msgcontent,sendto,state,msg_create_date)" + "values('"
+ rs.getString("sendto") + "','" + ctitle + "','" + ccontent + "','"
+ uuname + "','" + "1" + "','"+"2020-04-27"+"')");
ps.executeUpdate();
request.getRequestDispatcher("index.jsp").forward(request, response);
%>
<%
}
%>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
//BaseDao.closeAll(conn, ps, rs);
try {
if (rs != null) {
rs.close();
}
} finally {
try {
if (ps != null) {
ps.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
}
}
%>
<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@page import="com.gd.entity.Msg"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String ctitle = request.getParameter("ctitle");
String ccontent = request.getParameter("ccontent");
//获取邮件列表页面输入的标题
String contitle = (String) session.getAttribute("newcontitle");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加载驱动
String url = "jdbc:mysql://localhost:3306/u";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 连接对象
//conn = BaseDao.getConnection();
%>
<%
Msg msg = new Msg();
String uuname = (String) session.getAttribute("uuname");
ps = conn.prepareStatement("select * from msg where username=?");
ps.setString(1, uuname);
rs = ps.executeQuery();
while (rs.next()) {
ps = conn.prepareStatement("insert into msg(username,title,msgcontent,sendto,state,msg_create_date)" + "values('"
+ rs.getString("sendto") + "','" + ctitle + "','" + ccontent + "','"
+ uuname + "','" + "1" + "','"+"2020-04-27"+"')");
ps.executeUpdate();
request.getRequestDispatcher("index.jsp").forward(request, response);
%>
<%
}
%>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
//BaseDao.closeAll(conn, ps, rs);
try {
if (rs != null) {
rs.close();
}
} finally {
try {
if (ps != null) {
ps.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
}
}
%>
<br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@page import="com.gd.entity.Msg"%>
<%
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>
<center>
<h1>欢迎页面 </h1></br>
你好:<%= request.getParameter("uname") %>
</br>
</center>
<% if ( request.getParameter("uname") != null) {
// 用户已登陆
%>
<%
String uname = request.getParameter("uname");
session.setAttribute("uuname", uname);
request.setCharacterEncoding("utf-8");
String contitle = request.getParameter("contitle");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//加载驱动
String url = "jdbc:mysql://localhost:3306/u";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 连接对象
//conn = BaseDao.getConnection();
%>
<table align="center" width="92%" border="3">
<tr>
<td>ID</td>
<td>用户</td>
<td>标题</td>
<td>内容</td>
<td>状态</td>
<td>发件人</td>
<td>日期</td>
<td>删除</td>
<td>回复</td>
</tr>
<%
Msg msg = new Msg();
ps = conn.prepareStatement("select * from msg where username=?");
ps.setString(1, uname);
rs = ps.executeQuery();
while (rs.next()) {
%>
<tr>
<td><%=rs.getInt("msgid")%></td>
<td><%=rs.getString("username")%></td>
<td><a href="look.jsp?id=<%=rs.getInt("msgid")%>"><%=rs.getString("title")%></a></td>
<td><%=rs.getString("msgcontent")%></td>
<td>
<%
if (rs.getString("state").equals("1")) {
%> <%
out.print("未读");
%> <%
} else {
%> <%
out.print("已读");
%> <%
}
%>
</td>
<td><%=rs.getString("sendto")%></td>
<td><%=rs.getString("msg_create_date")%></td>
<td><a href="delete.jsp?id=<%=rs.getInt("msgid")%>">删除</a></td>
<td>
<form action="insert.jsp" method="post">
<input type="submit" name="a" value="回复">
</form>
</td>
</tr>
<%
}
%>
</table>
<br>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
//BaseDao.closeAll(conn, ps, rs);
try {
if (rs != null) {
rs.close();
}
} finally {
try {
if (ps != null) {
ps.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
}
}
%>
<%
} else {
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh", "2;URL=index.jsp");
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="index.jsp">这里</a>!!!<br>
<%} %>
</body>
</html>