JSP第12次作业
package week12;
public class EmailEntity {
public String sender;
public String receiver;
public String title;
public String content;
public String status;
public EmailEntity() {
sender = new String("");
receiver = new String("");
title = new String("");
content = new String("");
status = new String("");
}
public String getHtml()
{
String _str;
_str = new String("<td>\n" + sender + "</td>\n" +
"<td>\n" + receiver + "</td>\n" +
"<td>\n" + title + "</td>\n" +
"<td>\n" + content + "</td>\n" +
"<td>\n" + status + "</td>\n");
return _str;
}
}
package week12;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class UserDao {
public static Connection CreateConnection()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/week12", "root", "1234567");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
}
return conn;
}
public static void closeConn(Connection conn)
{
try
{
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void register(String name, String password, int age)
{
Connection conn = null;
try
{
conn = CreateConnection();
java.sql.Statement sqlStatement = conn.createStatement();
String sqlQuery = "INSERT INTO `user_table` SET `name`='" + name + "',`password`='" + password + "',`age`=" + age + ";";
sqlStatement.executeQuery(sqlQuery);
}
catch (java.sql.SQLException throwables)
{
throwables.printStackTrace();
}
finally
{
if (conn != null)
{
closeConn(conn);
}
}
}
public static boolean login(String name, String password)
{
Connection conn = null;
int count = 0;
try
{
conn = CreateConnection();
java.sql.Statement sqlStatement = conn.createStatement();
String sqlQuery = "select count(*) from user_table where name = '"+ name +"', password = '" + password + "';";
ResultSet rs = sqlStatement.executeQuery(sqlQuery);
while (rs.next() != false)
{
count = rs.getInt(1);
}
}
catch (java.sql.SQLException throwables)
{
throwables.printStackTrace();
}
finally
{
if (conn != null)
{
closeConn(conn);
}
}
return (count > 0);
}
public static ArrayList<EmailEntity> getEmail() {
Connection conn = null;
ArrayList<EmailEntity> emailEntities = new ArrayList<EmailEntity>();
try
{
conn = CreateConnection();
java.sql.Statement sqlStatement = conn.createStatement();
String sqlQuery = "select sender, receiver, title, content, status from email;";
ResultSet rs = sqlStatement.executeQuery(sqlQuery);
while (rs.next() != false)
{
EmailEntity email = new EmailEntity();
email.sender = rs.getString(0);
email.receiver = rs.getString(1);
email.title = rs.getString(2);
email.content = rs.getString(3);
email.status = rs.getString(4);
emailEntities.add(email);
}
}
catch (java.sql.SQLException throwables)
{
throwables.printStackTrace();
}
finally
{
if (conn != null)
{
closeConn(conn);
}
}
// EmailEntity email = new EmailEntity();
// email.sender = "张三";
// email.receiver = "user";
// email.title = "测试";
// email.content = "测试内容啦啦啦";
// email.status = "未读";
// emailEntities.add(email);
//
// EmailEntity email2 = new EmailEntity();
// email2.sender = "aa";
// email2.receiver = "user";
// email2.title = "测试2";
// email2.content = "测试2";
// email2.status = "已读";
// emailEntities.add(email2);
return emailEntities;
}
void deleteEmail(int id)
{
Connection conn = null;
try
{
conn = CreateConnection();
java.sql.Statement sqlStatement = conn.createStatement();
String sqlQuery = "delete from email where id = '" + id + "'";
sqlStatement.executeQuery(sqlQuery);
}
catch (java.sql.SQLException throwables)
{
throwables.printStackTrace();
}
finally
{
if (conn != null)
{
closeConn(conn);
}
}
}
void replyEmail(String sender, String receiver, String title, String content)
{
Connection conn = null;
try
{
conn = CreateConnection();
java.sql.Statement sqlStatement = conn.createStatement();
String sqlQuery = "insert into email (sender, receiver, title, content, status) values(?, ?, ?, ?, ?);";
PreparedStatement ps = null;
try
{
ps = conn.prepareStatement(sqlQuery);
ps.setString(1, sender);
ps.setString(2, receiver);
ps.setString(3, title);
ps.setString(4, content);
ps.setString(5, "未读");
ps.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
}
}
catch (java.sql.SQLException throwables)
{
throwables.printStackTrace();
}
finally
{
if (conn != null)
{
closeConn(conn);
}
}
}
static public EmailEntity getReply(int id)
{
Connection conn = null;
EmailEntity email = null;
try
{
conn = CreateConnection();
java.sql.Statement sqlStatement = conn.createStatement();
String sqlQuery = "select sender, receiver, title, content, status from email;";
ResultSet rs = sqlStatement.executeQuery(sqlQuery);
while (rs.next() != false)
{
email.sender = rs.getString(0);
email.receiver = rs.getString(1);
email.title = rs.getString(2);
email.content = rs.getString(3);
email.status = rs.getString(4);
}
}
catch (java.sql.SQLException throwables)
{
throwables.printStackTrace();
}
finally
{
if (conn != null)
{
closeConn(conn);
}
}
return email;
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="week12.UserDao" %>
<%@ page import="week12.EmailEntity" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Detail</title>
</head>
<body>
<table border = "1">
<tr>
<th>发件人</th>
<th>主题</th>
<th>状态</th>
<th>时间</th>
</tr>
<tr>
<td>张三</td>
<td>测试</td>
<td>已读</td>
<td>2022-5-22</td>
</tr>
</table>
<p>
<%= UserDao.getReply(1); %>
</p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="week11.UserDao" %>
<%@ page import="week11.EmailEntity" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Email</title>
</head>
<body>
<p>
你好
<%
session.setAttribute("username", new String("user"));
out.println(session.getAttribute("username").toString());
%>
</p>
<table border = "1">
<tr>
<th>发件人</th>
<th>主题</th>
<th>内容</th>
<th>状态</th>
<th>时间</th>
<th></th>
</tr>
<%
request.setCharacterEncoding("utf-8");
ArrayList<EmailEntity> emailEntities = UserDao.getEmail();
for (int n = 0; n < emailEntities.size(); n++)
{
out.println("<tr>");
out.println(emailEntities.get(n).getHtml());
out.println("</tr>");
}
%>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="week11.UserDao" %>
<!DOCTYPE html>
<html>
<head>
<title>Login Process</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
if (UserDao.login(username, password))
{
out.println("<h1>登录成功</h1>");
session.setAttribute("username", username);
}
else
{
out.println("<h1>登录失败</h1>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<script>
function verify() {
let user = document.getElementById("username").value;
let password = document.getElementById("password").value;
if (user == '' || password == '') {
alert("用户名与密码不能为空.");
return;
}
loginForm.submit();
}
</script>
</head>
<body>
<form action="login_process.jsp" method="POST" name="loginForm">
用户名: <input type="text" id="username" name="username"><br/>
密码: <input type="password" id="password" name="password"><br/>
<input type="button" name="submitButton" value="提交" onclick="verify()">
<a href="register.jsp">register page</a>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="week11.UserDao" %>
<!DOCTYPE html>
<html>
<head>
<title>Register Process</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String age = request.getParameter("age");
UserDao.register(username, password, Integer.parseInt(age));
out.println("<h1>注册成功</h1>");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="week12.UserDao" %>
<%@ page import="week12.EmailEntity" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Detail</title>
</head>
<body>
<table border = "1">
<tr>
<th>发件人</th>
<th>主题</th>
<th>状态</th>
<th>时间</th>
</tr>
<tr>
<td>张三</td>
<td>测试</td>
<td>已读</td>
<td>2022-5-22</td>
</tr>
</table>
<p>
<%= UserDao.getReply(1); %>
</p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Register</title>
<script>
function verify() {
let user = document.getElementById("username").value;
let password = document.getElementById("password").value;
if (user == '' || password == '') {
alert("用户名与密码不能为空.");
return;
}
loginForm.submit();
}
</script>
</head>
<body>
<form action="register_process.jsp" method="POST" name="loginForm">
用户名: <input type="text" id="username" name="username"><br/>
密码: <input type="password" id="password" name="password"><br/>
<input type="button" name="submitButton" value="提交" onclick="verify()">
<a href="login.jsp">login page</a>
</form>
</body>
</html>