Java+Servlet+JSP

采用MVC模式(Model+View+Controller)

功能:

项目的主要模块:注册模块,登陆模块,留言列表模块,留言模块,留言详情模块。

注册模块:实现用户注册,将新注册用户数据(用户名和密码)写入数据库,并能识别该用户是否已注册,以及判断注册时密码和确认密码是否一致。

登陆模块:实现用户登陆,能够判断该用户是否已注册,用户名密码是否为空以及密码填写是否正确。

留言列表模块:展示是数据库中留言信息(包括留言数,留言标题,留言内容,留言时间以及用户ID),点击留言标题可以跳转到详细的留言页面。

留言模块:留言模块包含留言标题和留言内容,点击提交之后会将数据存在数据库中,并展示于留言列表页面。

留言详情模块:展示某项留言的详细信息,以及展示该留言下的评论。以及评论该留言。

整体框架

java留言板的实现 简单的javaweb留言板设计_java留言板的实现

java留言板的实现 简单的javaweb留言板设计_html_02

View:\登陆界面\注册界面\留言列表界面\留言细节界面(评论)\留言界面

1.login.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新浪微博</title>

<style type="text/css">
	body {
		background-color: white;
		background-size: 100% 100%;
		background-repeat:no-repeat; 
		background-attachment:fixed
		
		}
	html {
		height:100%;
		weight:100%;
	}
	label{
	color:white;
	font-weight:bolder;
	}
	.center-in-center{   
		position: absolute;   
		top: 50%;   
		left: 50%;   
		-webkit-transform: translate(-50%, -50%);   
		-moz-transform: translate(-50%, -50%);   
		-ms-transform: translate(-50%, -50%);   
		-o-transform: translate(-50%, -50%);   
		transform: translate(-50%, -50%);   
	}
	.col{
		color:white;
		font-weight:bolder;
	}
</style>
</head>

<body background="pictures/言叶之庭8.png">

<div align="center" class="center-in-center">
		<h1 class ="col">   登陆入口</h1>
	<form action="LoginServlet" method = "post">
		<input type="hidden" name="action" value="login">
		
		<label>用户名:</label>
		<input type="text" name="username" style=width:150px;height:20px; ><br>
		
		<label>密码:</label>    
		<input type="password" name="password" style=width:150px;height:20px; ><br>
		
		            
		<input type="submit" name="login" value="登陆" style=width:150px;height:20px;><br>
		            
		<input type="reset" name="reset" value="重置" style=width:150px;height:20px;><br>
		            
		<button type="submit" formaction="register.jsp" style=width:150px;height:20px;>注册</button>
	</form>
	<p class="col">${message}</p>

</div>
</body>
</html>

java留言板的实现 简单的javaweb留言板设计_java_03

2.register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新浪微博</title>
<style type="text/css">
	body {
		background-color: white;
		background-size: 100% 100%;
		background-repeat:no-repeat; 
		background-attachment:fixed
		}
	html {
		height:100%;
		weight:100%;
	}
	label{
	color:white;
	font-weight:bolder;
	}
	h1{
	color:white;
	}
	.center-in-center{   
		position: absolute;   
		top: 50%;   
		left: 50%;   
		-webkit-transform: translate(-50%, -50%);   
		-moz-transform: translate(-50%, -50%);   
		-ms-transform: translate(-50%, -50%);   
		-o-transform: translate(-50%, -50%);   
		transform: translate(-50%, -50%);   
	}   
	.col{
		color:white;
		font-weight:bolder;
	}
</style>
</head>

<body background="pictures/言叶之庭8.png">

<div align="center" class="center-in-center">
	
	<h1 align="center">     请完善您的相关信息:</h1>
	<form action="RegistServlet" method="post">
	
		<input type="hidden" name="action" value="regist"><br>
		
		<label>用户名:</label>
		   
		<input type="text" name="username" required><br>
		
		<label>密码:</label>
		       
		<input type="text" name="password" required><br>
		
		<label>确认密码:</label>
		<input type="password" name="password2" required>
		<p class="col">${message}</p>
		                 
		<input type="submit" name="regist" value="注册" style=width:75px;>
		<input type="reset" value="重置" style=width:75px;> 
	</form>
	
</div>
</body>

</html>

java留言板的实现 简单的javaweb留言板设计_html_04

3.MessageList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*,javaBean.Message"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>留言板系统</title>
	
	<style type="text/css">
		body {
			background-color: white;
			background-size: 100% 100%;	
			background-repeat:no-repeat; 
			background-attachment:fixed
		}
		html {
			height:100%;
			weight:100%;
		}
	</style>
</head>

<body background="pictures/言叶之庭4.jpeg">
<div align="center">
	<br>
	<h1>欢迎来到留言系统</h1>
	
	
	<table width="800" border="1" cellpadding="5">
		<%
   			 ArrayList mL=(ArrayList)request.getAttribute("MessageList");
		%>
		<tr>
			<th>留言数</th>
			<th>标题</th>
			<th>留言内容</th>
			<th>用户ID</th>
		</tr>
		<% if (mL != null){ 
		for (int i = 0; i < mL.size(); ++i ){
				Message message = (Message)mL.get(i);%>
				<tr>
					<td><%= message.getmid() %></td>
					<td><a href="MessageDetail.jsp?mid=<%= message.getmid() %>"><%= message.gettitle() %></td>	
					<td><%= message.getcontext() %></td>
					<td><%= message.getuserid() %></td>
				</tr>
		<%	} } %>
		
	</table>
	<h2><a href="LeaveMessage.jsp">我要留言</a> </h2>
</div>	
</body>
</html>

java留言板的实现 简单的javaweb留言板设计_html_05

4.LeaveMessage.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>新浪微博</title>
	<style type="text/css">
		body {
			background-color: white;
			background-size: 100% 100%;	
			background-repeat:no-repeat; 
			background-attachment:fixed
		}
		html {
			height:100%;
			weight:100%;
		}
		.center-in-center{   
			position: absolute;   
			top: 50%;   
			left: 50%;   
			-webkit-transform: translate(-50%, -50%);   
			-moz-transform: translate(-50%, -50%);   
			-ms-transform: translate(-50%, -50%);   
			-o-transform: translate(-50%, -50%);   
			transform: translate(-50%, -50%);   
		}
	</style>
</head>

<body background="pictures/言叶之庭4.jpeg">
<div align="center" class="center-in-center">
	<form action="LeaveMessageServlet" method="post">
	
		<input type="hidden" name="action" value="leave">
		
		<p>标题</p>
		<textarea name="title"  rows="1" cols="60"  required></textarea>
		<p>留言</p>
		<textarea name="context" cols="60" rows="8" required></textarea><br>
		<input type="submit" value="提交">
		
	</form>
	
</div>
</body>
</html>

java留言板的实现 简单的javaweb留言板设计_java_06

5.MessageDetail.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*,javaBean.*,useBean.*"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>新浪微博</title>
<style type="text/css">
		body {
			background-color: white;
			background-size: 100% 100%;	
			background-repeat:no-repeat; 
			background-attachment:fixed
		}
		html {
			height:100%;
			weight:100%;
		}
		
	</style>
</head>

<body background="pictures/言叶之庭4.jpeg">
	<%
		String mid ="";
		mid= request.getParameter("mid");
		Message message = new Message();
		message = MessageDB.selectMessagem(mid);	
	%>
	
	<h1>主题:<%= message.gettitle() %></h1>
	<h2>内容:</h2>
	<p><%= message.getcontext() %></p>
	
	<h3>评论:</h3>
	<p>=================================================================================</p>
   <%
   			 ArrayList mL=(ArrayList)request.getAttribute("DetailList");
	%>
	
	<% if (mL != null){ 
		for (int i = 0; i < mL.size(); ++i ){
				Detail detail = (Detail)mL.get(i);
				
				if(detail.getmid().equals(mid)){
			%>
				
				
				<p>用户:
				<%= detail.getcuser() %></p>
				<p>评论:	
				<%= detail.getcomment() %></p>
				<p>时间:
				<%= detail.getctime() %></p>
				<p>=================================================================================</p>
			
				
		<%}	}} %>
		
	<form action="LeaveDetailServlet" method="post">
		<input type="hidden" name="action" value="leave">	
		<p>提交留言,查看评论:</p>
		<textarea name="comment" cols="60" rows="4" required></textarea><br>
		<input type="hidden" name="mid" value="<%= mid %>">
		<input type="submit" value="卑微评论.">	
	</form>
	
</body>
</html>

java留言板的实现 简单的javaweb留言板设计_mvc_07

Model:

UserBean:\User.java\Message.java\Detail.java

UseBean:\UserDB.java\MessageDB.java\DetailDB.java

ConnectionPool:\ConnectionPool.java\DBUtil.java

JDBC:\JDBC.java

1.User.java

package javaBean;

import java.io.*;

public class User implements Serializable{
	private String id;
	private String username;
	private String password;
	
	
	public User() {
		id ="";
		username = "";
		password = "";
	}
	
	public User(String username,String password) {
		this.username =username;
		this.password = password;		
	}
	
	public String getid() {
		return id;
	}
	public void setid(String id) {
		this.id = id;
	}
	
	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;
	}

}

2.Message.java

package javaBean;

import java.io.Serializable;

public class Message implements Serializable{
	private String mid;
	private String userid;
	private String title;
	private String context;
	
	
	public Message() {
		mid ="";
		userid="";
		title = "";
		context = "";
	}
	
	public Message(String userid,String title,String context) {
		this.userid=userid;
		this.title =title;
		this.context = context;		
	}
	
	
	public String getmid() {
		return mid;
	}
	
	public String gettitle() {
		return title;
	}
	
	public String getuserid() {
		return userid;
	}
	
	public String getcontext() {
		return context;
	}
	
	
	
	public void setuserid(String userid) {
		this.userid = userid;
	}
	
	public void settitle(String title) {
		this.title = title;
	}

	public void setmid(String mid) {
		this.mid = mid;
	}

	public void setcontext(String context) {
		this.context = context;
	}
}

3.Detail.java

package javaBean;

import java.io.Serializable;
import java.sql.Date;

public class Detail implements Serializable{
	private String cid;
	private String mid;
	private String cuser;
	private String comment;
	private Date ctime;
	
	
	public Detail() {
		cid ="";
		cuser="";
		comment = "";
	}
	
	public Detail(String cuser, String mid, String comment,Date ctime) {
		this.cuser=cuser;
		this.mid= mid;
		this.comment =comment;
		this.ctime = ctime;		
	}
	
	
	public String getcid() {
		return cid;
	}
	
	public String getmid() {
		return mid;
	}
	public String getcomment() {
		return comment;
	}
	
	public String getcuser() {
		return cuser;
	}
	
	public Date getctime() {
		return ctime;
	}
	
	
	
	public void setcid(String cid) {
		this.cid = cid;
	}
	
	public void setmid(String mid) {
		this.mid = mid;
	}
	
	public void setcomment(String comment) {
		this.comment = comment;
	}

	public void setcuser(String cuser) {
		this.cuser = cuser;
	}

	public void setctime(Date ctime) {
		this.ctime = ctime;
	}
}

4.UserDB.java

package useBean;

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 connectionPool.ConnectionPool;
import connectionPool.DBUtil;
import javaBean.Message;
import javaBean.User;

public class UserDB {
	
	
	//插入用户
	public static int insert(User user) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		
		String query = "INSERT INTO User (username, password)"
						+"VALUES (?, ?)";
		try { 
			
			ps = connection.prepareStatement(query);
			ps.setString(1, user.getusername());
			ps.setString(2, user.getpassword());
			
		
			return ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println(e);
			return 0;
		} finally {
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		} 
		
	}


	//判断用户名是否存在
	public static boolean UserExists(String username) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String query = "SELECT username FROM User "
		+ "WHERE username = ?";
		try {
			ps = connection.prepareStatement(query);
			ps.setString(1, username);
			rs = ps.executeQuery();
			return rs.next();
		} catch (SQLException e) {
			System.out.println(e);
			return false;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
		
	}
	
	//根据username读出user
	public static User selectUser(String username) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		
		String query = "SELECT * FROM User "
		+ "WHERE username = ?";
		try {
			ps = connection.prepareStatement(query);
			ps.setString(1, username);
			rs = ps.executeQuery();
			User user = null;
			if (rs.next()) {
				user = new User ();
				user.setid(rs.getString("id"));
				user.setusername(rs.getString("username"));
				user.setpassword(rs.getString("password"));
			}
			return user;
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
	}
	
	
	
	//根据userid读出user
		public static User selectUser1(String id) {
			ConnectionPool pool = ConnectionPool.getInstance();
			Connection connection = pool.getConnection();
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			
			String query = "SELECT * FROM User "
			+ "WHERE id = ?";
			try {
				ps = connection.prepareStatement(query);
				ps.setString(1, id);
				rs = ps.executeQuery();
				User user = null;
				if (rs.next()) {
					user = new User ();
					user.setid(rs.getString("id"));
					user.setusername(rs.getString("username"));
					user.setpassword(rs.getString("password"));
				}
				return user;
			} catch (SQLException e) {
				System.out.println(e);
				return null;
			} finally {
				DBUtil.closeResultSet(rs);
				DBUtil.closePreparedStatement(ps);
				pool.freeConnection(connection);
			}
		}
	
		
	//从message表中读出所有数据
	public static List<Message> selectU() {
		List<Message> mL= new ArrayList<Message>();
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		
		String query = "SELECT * FROM Message ";
		try {
			ps = connection.prepareStatement(query);
			rs = ps.executeQuery();
			Message m = null;
			while (rs.next()) {
				m = new Message ();
				m.setcontext(rs.getString("context"));
				m.setmid(rs.getString("mid"));
				m.settitle(rs.getString("title"));
				m.setuserid(rs.getString("userid"));
				mL.add(m);
			}
			
			return mL;
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
	}
	
}

5.MessageDB.java

package useBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import connectionPool.ConnectionPool;
import connectionPool.DBUtil;
import javaBean.Message;


public class MessageDB {
	
	
	
	public static int insert(Message message) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		
		String query = "INSERT INTO Message (userid, title, context)"
						+"VALUES (?, ?, ?)";
		try { 
			
			ps = connection.prepareStatement(query);
			ps.setString(1, message.getuserid());
			ps.setString(2, message.gettitle());
			ps.setString(3, message.getcontext());
			
			return ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println(e);
			return 0;
		} finally {
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		} 
		
	}

	
	public static Message selectMessage(String userid) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		
		String query = "SELECT * FROM Message "
		+ "WHERE userid = ?";
		try {
			ps = connection.prepareStatement(query);
			ps.setString(1, userid);
			rs = ps.executeQuery();
			Message message = null;
			if (rs.next()) {
				message = new Message ();
				message.setmid(rs.getString("mid"));
				message.setuserid(rs.getString("userid"));
				message.settitle(rs.getString("title"));
				message.setcontext(rs.getString("context"));
			}
			
			return message;
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
	}
	
	public static Message selectMessagem(String mid) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String query = "SELECT * FROM Message "
		+ "WHERE mid = ?";
		try {
			ps = connection.prepareStatement(query);
			ps.setString(1, mid);
			rs = ps.executeQuery();
			Message message = null;
			if (rs.next()) {
				message = new Message ();
				message.setmid(rs.getString("mid"));
				message.setuserid(rs.getString("userid"));
				message.settitle(rs.getString("title"));
				message.setcontext(rs.getString("context"));
			}
			
			return message;
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
	}
	
}

6.DetailDB.java

package useBean;

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 connectionPool.ConnectionPool;
import connectionPool.DBUtil;
import javaBean.Detail;


public class DetailDB {
	
	public static int insert(Detail detail) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		
		String query = "INSERT INTO Detail (mid, cuser, comment, ctime)"
						+"VALUES (?, ?, ?, ?)";
		try { 
			
			ps = connection.prepareStatement(query);
			ps.setString(1, detail.getmid());
			ps.setString(2, detail.getcuser());
			ps.setString(3, detail.getcomment());
			ps.setDate(4, detail.getctime());
			
			return ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println(e);
			return 0;
		} finally {
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		} 
		
	}

	
	public static Detail selectDetail(String userid) {
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String query = "SELECT * FROM Detail "
		+ "WHERE userid = ?";
		try {
			ps = connection.prepareStatement(query);
			ps.setString(1, userid);
			rs = ps.executeQuery();
			Detail detail = null;
			if (rs.next()) {
				detail = new Detail();
				detail.setmid(rs.getString("mid"));
				detail.setcuser(rs.getString("cuser"));
				detail.setcomment(rs.getString("comment"));
				detail.setctime(rs.getDate("ctime"));
			}
			
			return detail;
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
	}
	

	
	public static List<Detail> selectU() {
		List<Detail> mL= new ArrayList<Detail>();
		ConnectionPool pool = ConnectionPool.getInstance();
		Connection connection = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		
		String query = "SELECT * FROM detail";
		try {
			ps = connection.prepareStatement(query);
			rs = ps.executeQuery();
			Detail m = null;
			while (rs.next()) {
				m = new Detail ();
				m.setcid(rs.getString("cid"));
				m.setmid(rs.getString("mid"));
				m.setcuser(rs.getString("cuser"));
				m.setcomment(rs.getString("comment"));
				m.setctime(rs.getDate("ctime"));
				mL.add(m);
			}
			
			return mL;
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} finally {
			DBUtil.closeResultSet(rs);
			DBUtil.closePreparedStatement(ps);
			pool.freeConnection(connection);
		}
	}
	
}

7.ConnectionPool.java

package connectionPool;

import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ConnectionPool {
	private static ConnectionPool pool = null;
	private static DataSource dataSource = null;
	
	private ConnectionPool() {
		try {
			InitialContext ic = new InitialContext();
			dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/123");
		} catch (NamingException e) {
			System.out.println(e);
		}
	}
	
	public static synchronized ConnectionPool getInstance() {
		if (pool == null) {
			pool = new ConnectionPool();
		}
		return pool;
	}
	
	public Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		} 
	}
	
	public void freeConnection(Connection c) {
		try {
			c.close();
		} catch (SQLException e) {
			System.out.println(e);
		}
	}
	
}

8.DBUtil.java

package connectionPool;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
	
	public static void closeStatement(Statement s) {
		try {
			if (s != null) {
				s.close();
			}
		} catch (SQLException e) {
			System.out.println(e);
		}
	}
	public static void closePreparedStatement(Statement ps) {
		try {
			if (ps != null) {
				ps. close();
			}
		} catch (SQLException e) {
			System.out.println(e);
		}
	}
	
	public static void closeResultSet(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			System.out.println(e);
			}
	}
}

9.JDBC.java

package JDBC;

import java.sql.*;
public class JDBC {
  public static void main(String args[]) {
    try {
      Class.forName("com.mysql.cj.jdbc.Driver");     //加载MYSQL JDBC驱动程序   
      //Class.forName("org.gjt.mm.mysql.Driver");
     System.out.println("Success loading Mysql Driver!");
    }
    catch (Exception e) {
      System.out.print("Error loading Mysql Driver!");
      e.printStackTrace();
    }
    
    
    try {
      Connection connect = DriverManager.getConnection(
    		  "jdbc:mysql://localhost:3306/messageboard?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&useSSL=false","用户名","密码");
           //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码
      
      System.out.println("Success connect Mysql server!");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from 表名");        //!!!表名
                                                             
      while (rs.next()) {
    	  System.out.print(rs.getString(1));
    	  System.out.print(rs.getString(2));
    	  System.out.println(rs.getString(3));
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
  }
}

10.context.xml(与JDBC对应)

java留言板的实现 简单的javaweb留言板设计_开发语言_08

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/MessageBoard">
<!-- context里的path要填映像值 resource里的name要填connection的lookup的值
		url后面要指定时区 -->
	<Resource name="jdbc/123" auth="Container"
		driverClassName="com.mysql.jdbc.Driver"
		url = "jdbc:mysql://localhost:3306/messageboard?serverTimezone=UTC"
		username="root" password="123"
		maxActive="100" maxidle="30" maxWait="10000"
		logAbandoned="true" removeAbandoned="true"
		removeAbandonedTimeout="60" type="javax.sql.DataSource" />
</Context>

Controller:

\LoginServlet.java\RegistServlet.java\\LeaveMessageServlet.java

\LeaveDetailServlet.java

1.LoginServlet.java

package Sevlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javaBean.Message;
import javaBean.User;
import useBean.MessageDB;
import useBean.UserDB;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	String message="";
       
    public LoginServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url ="/login.jsp";
		String action = request.getParameter("action");
		request.setCharacterEncoding("utf-8");
		
		//检测页面是否提交
		if(action == null)
			url="/login.jsp";
		else if(action.equals("login"))
			url = login(request,response);
		
		request.setAttribute("message", message);
		getServletContext().getRequestDispatcher(url).forward(request, response);
	}
	
	@SuppressWarnings("unused")
	private String login(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		HttpSession session = request.getSession();
		String userid="";
		
		User user = UserDB.selectUser(username);

		String url = "/register.jsp";

		if(user != null) {
			
			if(user.getpassword().equals(password)) {
				Cookie u = new Cookie("userid",username);
				u.setMaxAge(60*60*24*365*2);
				u.setPath("/");
				response.addCookie(u);
				
				userid = user.getid();
				session.setAttribute("userid", userid);
				
				List<Message> mL = UserDB.selectU(); 
				request.setAttribute("MessageList", mL);
				url="/MessageList.jsp";
				
			}
			else {
				message = "密码错误";
				url="/login.jsp";
			}
		}
		else {
			message = "该用户不存在";
			url = "/login.jsp";
		}
		return url;		
	}	
}

2.RegistServlet.java

package Sevlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javaBean.User;
import useBean.UserDB;

@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	String message ="";
  
  public RegistServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		String url ="register.html";
		
		if(action.equals("regist"))
			url = registerUser(request,response);
		
		getServletContext().getRequestDispatcher(url).forward(request, response);
	}
	
	
	private String registerUser(HttpServletRequest request,HttpServletResponse response) {
		String username = request.getParameter("username"); 
		String password = request.getParameter("password");
		String password2 = request.getParameter("password2");
		String message = "";
		HttpSession session = request.getSession();
		
		//将数据存储于User对象
		User user = new User();
		user.setusername (username); 
		user.setpassword(password);
		
		String url = "/login.jsp";
		
		if(password.equals(password2))
		{
			if( !UserDB.UserExists(username) ) {
				message = "注册成功!";
				session.setAttribute("message", message);
				
				UserDB.insert(user);
				return url;
			}
			else
			{
				message = "用户名已存在";
				url = "/register.jsp";
				session.setAttribute("message", message);
				return url;
			}
		}
		else 
		{
			message = "密码不一致";
			session.setAttribute("message", message);
			url = "/register.jsp";
			return url;
		}
	}
}

3.LeaveMessageServlet.java

package Sevlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javaBean.Message;
import useBean.MessageDB;
import useBean.UserDB;

@WebServlet("/LeaveMessageServlet")
public class LeaveMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LeaveMessageServlet() {
        super();
    }

    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		String url ="/LeaveMessage.jsp";
		
		if(action.equals("leave"))
			url = leaveMessage(request,response);
		
		getServletContext().getRequestDispatcher(url).forward(request, response);
	}

	private String leaveMessage(HttpServletRequest request,HttpServletResponse response) {
		String title = request.getParameter("title"); 
		String context = request.getParameter("context");
		String userid="";
		String url="";
		HttpSession session = request.getSession();
	
		//将数据存储于Message对象
		Message message = new Message();
		
		message.setuserid((String)session.getAttribute("userid"));
		message.settitle (title); 
		message.setcontext(context);
		
		MessageDB.insert(message);
		
		Message messagess = MessageDB.selectMessage(userid);
		
		List<Message> mL = UserDB.selectU(); 
		request.setAttribute("MessageList", mL);
		url="/MessageList.jsp";
		
		return url;
	}
}

4.LeaveDetailServlet.java

package Sevlet;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javaBean.Detail;
import javaBean.Message;
import javaBean.User;
import useBean.DetailDB;
import useBean.MessageDB;
import useBean.UserDB;

@WebServlet("/LeaveDetailServlet")
public class LeaveDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public LeaveDetailServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		String url ="/MessageDetail.jsp";
		
		if(action.equals("leave"))
			url = leaveDetail(request,response);
		
		getServletContext().getRequestDispatcher(url).forward(request, response);
	}
	
	
	
	private String leaveDetail(HttpServletRequest request,HttpServletResponse response) {
		String comment = request.getParameter("comment");
		String mid = request.getParameter("mid");
		HttpSession session = request.getSession();
		String userid="";
		String url="";
		String cuser="";
		
		//将数据存储于Detail对象
		Detail detail = new Detail();
		userid = (String)session.getAttribute("userid");
		User user = UserDB.selectUser1(userid);
		cuser = user.getusername();
		
		detail.setmid(mid);
		detail.setcuser(cuser);
		detail.setcomment(comment);
		detail.setctime(new Date(System.currentTimeMillis()));
		
		DetailDB.insert(detail);
		
		List<Detail> mL = DetailDB.selectU(); 
		request.setAttribute("DetailList", mL);
		
		url="/MessageDetail.jsp?mid="+mid;
		return url;
	}

}

数据库结构

java留言板的实现 简单的javaweb留言板设计_html_09

java留言板的实现 简单的javaweb留言板设计_开发语言_10

java留言板的实现 简单的javaweb留言板设计_mvc_11

java留言板的实现 简单的javaweb留言板设计_开发语言_12

部分功能展示

java留言板的实现 简单的javaweb留言板设计_java_13

java留言板的实现 简单的javaweb留言板设计_mvc_14

java留言板的实现 简单的javaweb留言板设计_java留言板的实现_15

java留言板的实现 简单的javaweb留言板设计_开发语言_16

java留言板的实现 简单的javaweb留言板设计_mvc_17

java留言板的实现 简单的javaweb留言板设计_mvc_18

===========================================分割线===========================================

2020-12-19

留言板源码

链接:https://pan.baidu.com/s/1-ukIjRPInwRRFdDVRss7Sw 
提取码:e2ih 
 

eclipse-ee(这个是for web开发者的)

链接:https://pan.baidu.com/s/1o9GkyYzMMuU6koWw5PzZ2w 
提取码:s6g4 

tomcat

链接:https://pan.baidu.com/s/1jfMHL351VXOF0LxD9ClrKg 
提取码:wqv2 
 

===========================================分割线===========================================

源码要修改自己的数据库

设计数据库的时候id要递增

如何配置请百度

===========================================分割线===========================================

先自己配置一下,不会的百度!百度!

java留言板的实现 简单的javaweb留言板设计_java_19

勾选自动递增!