JSP第11周作业

题目:

1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层
entity,dao包

3.登陆,注册,登陆后显示全部邮件

CREATE TABLE `week11`.`user_table` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `password` varchar(255) NOT NULL DEFAULT ''
  PRIMARY KEY (`Id`)
);

CREATE TABLE `week11`.`email` (
  `Id` int(11) NOT NULL AUTO_INCREMENT, 
  `render` varchar(255) NOT NULL DEFAULT '',
  `receiver` varchar(255) NOT NULL DEFAULT '',
  `title` varchar(255) NOT NULL, 
  `content` varchar(255) NOT NULL, 
  `status` varchar(255) NOT NULL
);

package week11;

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 week11;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;

public class UserDao {
	public static Connection CreateConnection()
	{
		try
		{
			String driver = "com.mysql.jdbc.Driver";
			Class.forName(driver).newInstance();
			System.out.println("test");
			Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/week10", "root", "1234567");
			return conn;
		}
		catch (Exception e)
		{
			System.out.println("CreateConnection");
			e.printStackTrace();
			return null;
		}
	}
	
	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);	
			}
		}
		return emailEntities;
		
	}
}

<%@ 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>
<%@ 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"%>
<!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>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"%>
<%@ 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>
	你好
	<%
		out.println(session.getAttribute("username").toString());
	%>
	</p>
	<table border = "1">
		<tr>
			<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(emailEntities.get(n).getHtml());
			}
		%>
	</table>
</body>
</html>

posted @ 2022-05-15 16:50  SuoJing  阅读(20)  评论(0编辑  收藏  举报