基于javaweb+mysql的jsp+servlet简单留言板(java+jsp+servlet+javabean+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
发表留言,查看留言,回复留言,查看回复,删除留言(管理员)
技术框架
JSP Servlet MySQL JDBC Tomcat CSS JavaScript
基于javaweb+mysql的JSP+Servlet简单留言板(java+jsp+servlet+javabean+mysql+tomcat)
Reply reply=new Reply();
if(type.equals("reply")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","留言请登录");
response.sendRedirect("fail.jsp");
return;
}
String reusername=request.getParameter("reusername");
String recontent=request.getParameter("recontent");
reply.setReUsername(reusername);
reply.setReContent(recontent);
reply.setMid(Integer.parseInt(mid));
try {
reply.add(reply);
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("lookreply")){
try {
//留言的id
String getId=request.getParameter("getId");
ArrayList<Reply> replies=new ArrayList<Reply>();
replies=reply.queryAll(Integer.parseInt(getId));
request.getSession().setAttribute("replies", replies);
response.sendRedirect("lookreply.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
super();
// TODO Auto-generated constructor stub
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
//登录
String userName = null;
userName = request.getParameter("text_username_login");
String passWord = request.getParameter("text_password_login");
request.setCharacterEncoding("utf-8");
//如果 用户名 或者密码 或者 验证码为 空,就返回
if (userName == null || passWord == null)
return;
//如果 用户名 或者密码 或者 验证码为 空字符串,就返回
if (userName == "" || passWord == "")
return;
// 进行了编码转换,以便支持中文用户名
userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
try {
ArrayList<User> users = user.queryAll();
for (User user1 : users) {
if (user1.getUsername().equals(userName) && user1.getPassword().equals(passWord)) {
//传给前端得数据
request.getSession().setAttribute("info", user1);
resp.sendRedirect("MessageServlet?type=query");
return;
}
}
request.getSession().setAttribute("info", "账号或者密码错误");
resp.sendRedirect("fail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","查看留言请登录");
response.sendRedirect("fail.jsp");
return;
}
try {
Message one = message.queryOne(getId);
request.getSession().setAttribute("message",one);
response.sendRedirect("detail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
public class ReplyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String mid = request.getParameter("mid");
response.sendRedirect("fail.jsp");
return;
}
String title=request.getParameter("title");
String content=request.getParameter("content");
String theme=request.getParameter("theme");
message.setTitle(title);
message.setContent(content);
message.setTheme(theme);
message.setTime(String.valueOf(new Date()));
message.setUsername(((User)request.getSession().getAttribute("info")).getUsername());
try {
message.add(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("query")){
try {
ArrayList<Message> messages=new ArrayList<Message>();
messages=message.queryAll();
request.getSession().setAttribute("messages", messages);
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("delete")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","删除留言请登录");
response.sendRedirect("fail.jsp");
return;
}else{
if(!info.getGrade().equals("admin")){
request.getSession().setAttribute("info","非管理员不可删除");
response.sendRedirect("fail.jsp");
return;
}
}
String idString=request.getParameter("id");
int id=Integer.parseInt(idString);
message.setId(id);
try {
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("delete")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","删除留言请登录");
response.sendRedirect("fail.jsp");
return;
}else{
if(!info.getGrade().equals("admin")){
request.getSession().setAttribute("info","非管理员不可删除");
response.sendRedirect("fail.jsp");
return;
}
}
String idString=request.getParameter("id");
int id=Integer.parseInt(idString);
message.setId(id);
try {
message.delete(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("queryOne")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","查看留言请登录");
response.sendRedirect("fail.jsp");
return;
}
try {
Message one = message.queryOne(getId);
request.getSession().setAttribute("message",one);
response.sendRedirect("detail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/**
* Servlet implementation class Student
*/
public class MessageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MessageServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String getId = request.getParameter("getId");
String type=request.getParameter("type");
System.out.println("type:"+type);
Message message=new Message();
}
}else if(type.equals("delete")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","删除留言请登录");
response.sendRedirect("fail.jsp");
return;
}else{
if(!info.getGrade().equals("admin")){
request.getSession().setAttribute("info","非管理员不可删除");
response.sendRedirect("fail.jsp");
return;
}
}
String idString=request.getParameter("id");
int id=Integer.parseInt(idString);
message.setId(id);
try {
message.delete(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("queryOne")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","查看留言请登录");
response.sendRedirect("fail.jsp");
return;
}
try {
Message one = message.queryOne(getId);
request.getSession().setAttribute("message",one);
response.sendRedirect("detail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
//登录
String userName = null;
userName = request.getParameter("text_username_login");
String passWord = request.getParameter("text_password_login");
request.setCharacterEncoding("utf-8");
//如果 用户名 或者密码 或者 验证码为 空,就返回
if (userName == null || passWord == null)
return;
//如果 用户名 或者密码 或者 验证码为 空字符串,就返回
if (userName == "" || passWord == "")
return;
// 进行了编码转换,以便支持中文用户名
userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
try {
ArrayList<User> users = user.queryAll();
for (User user1 : users) {
if (user1.getUsername().equals(userName) && user1.getPassword().equals(passWord)) {
//传给前端得数据
request.getSession().setAttribute("info", user1);
resp.sendRedirect("MessageServlet?type=query");
return;
}
}
request.getSession().setAttribute("info", "账号或者密码错误");
resp.sendRedirect("fail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* Servlet implementation class Student
*/
public class MessageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MessageServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String getId = request.getParameter("getId");
String type=request.getParameter("type");
System.out.println("type:"+type);
Message message=new Message();
if(type.equals("add")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","留言请登录");
response.sendRedirect("fail.jsp");
return;
}
String title=request.getParameter("title");
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
//登录
String userName = null;
userName = request.getParameter("text_username_login");
String passWord = request.getParameter("text_password_login");
request.setCharacterEncoding("utf-8");
//如果 用户名 或者密码 或者 验证码为 空,就返回
if (userName == null || passWord == null)
return;
//如果 用户名 或者密码 或者 验证码为 空字符串,就返回
if (userName == "" || passWord == "")
return;
// 进行了编码转换,以便支持中文用户名
User user = new User();
//登录
String userName = null;
userName = request.getParameter("text_username_login");
String passWord = request.getParameter("text_password_login");
request.setCharacterEncoding("utf-8");
//如果 用户名 或者密码 或者 验证码为 空,就返回
if (userName == null || passWord == null)
return;
//如果 用户名 或者密码 或者 验证码为 空字符串,就返回
if (userName == "" || passWord == "")
return;
// 进行了编码转换,以便支持中文用户名
userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
try {
ArrayList<User> users = user.queryAll();
for (User user1 : users) {
if (user1.getUsername().equals(userName) && user1.getPassword().equals(passWord)) {
//传给前端得数据
request.getSession().setAttribute("info", user1);
resp.sendRedirect("MessageServlet?type=query");
return;
}
}
request.getSession().setAttribute("info", "账号或者密码错误");
resp.sendRedirect("fail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("query")){
try {
ArrayList<Message> messages=new ArrayList<Message>();
messages=message.queryAll();
request.getSession().setAttribute("messages", messages);
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("delete")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","删除留言请登录");
response.sendRedirect("fail.jsp");
return;
}else{
if(!info.getGrade().equals("admin")){
request.getSession().setAttribute("info","非管理员不可删除");
response.sendRedirect("fail.jsp");
return;
}
}
String idString=request.getParameter("id");
int id=Integer.parseInt(idString);
message.setId(id);
try {
message.delete(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("queryOne")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","查看留言请登录");
response.sendRedirect("fail.jsp");
return;
}
try {
Message one = message.queryOne(getId);
request.getSession().setAttribute("message",one);
response.sendRedirect("detail.jsp");
String theme=request.getParameter("theme");
message.setTitle(title);
message.setContent(content);
message.setTheme(theme);
message.setTime(String.valueOf(new Date()));
message.setUsername(((User)request.getSession().getAttribute("info")).getUsername());
try {
message.add(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("query")){
try {
ArrayList<Message> messages=new ArrayList<Message>();
messages=message.queryAll();
request.getSession().setAttribute("messages", messages);
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("delete")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","删除留言请登录");
response.sendRedirect("fail.jsp");
return;
}else{
if(!info.getGrade().equals("admin")){
request.getSession().setAttribute("info","非管理员不可删除");
response.sendRedirect("fail.jsp");
return;
}
}
String idString=request.getParameter("id");
int id=Integer.parseInt(idString);
message.setId(id);
try {
message.delete(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setCharacterEncoding("utf-8");
String mid = request.getParameter("mid");
String type=request.getParameter("type");
System.out.println("type:"+type);
Reply reply=new Reply();
if(type.equals("reply")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","留言请登录");
response.sendRedirect("fail.jsp");
return;
}
String reusername=request.getParameter("reusername");
String recontent=request.getParameter("recontent");
reply.setReUsername(reusername);
reply.setReContent(recontent);
reply.setMid(Integer.parseInt(mid));
try {
reply.add(reply);
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("lookreply")){
try {
//留言的id
String getId=request.getParameter("getId");
ArrayList<Reply> replies=new ArrayList<Reply>();
replies=reply.queryAll(Integer.parseInt(getId));
request.getSession().setAttribute("replies", replies);
response.sendRedirect("lookreply.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
request.getSession().setAttribute("info", "账号或者密码错误");
resp.sendRedirect("fail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
User user=new User();
//注册
String userName = null;
userName = request.getParameter("text_username");
String passWord = request.getParameter("text_password");
request.setCharacterEncoding("utf-8");
//如果 用户名 或者密码 或者 验证码为 空,就返回
if(userName == null || passWord == null)
return;
//如果 用户名 或者密码 或者 验证码为 空字符串,就返回
if(userName == "" || passWord == "" )
return;
// 进行了编码转换,以便支持中文用户名
userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
try {
ArrayList<User> users = user.queryAll();
for (User user1 : users) {
if(user1.getUsername().equals(userName))
{
resp.sendRedirect("fail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
User user=new User();
//注册
String userName = null;
userName = request.getParameter("text_username");
String passWord = request.getParameter("text_password");
request.setCharacterEncoding("utf-8");
//如果 用户名 或者密码 或者 验证码为 空,就返回
if(userName == null || passWord == null)
return;
//如果 用户名 或者密码 或者 验证码为 空字符串,就返回
if(userName == "" || passWord == "" )
return;
// 进行了编码转换,以便支持中文用户名
userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
try {
ArrayList<User> users = user.queryAll();
for (User user1 : users) {
if(user1.getUsername().equals(userName))
{
return;
}
}
if (userName == "" || passWord == "")
return;
// 进行了编码转换,以便支持中文用户名
userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8");
try {
ArrayList<User> users = user.queryAll();
for (User user1 : users) {
if (user1.getUsername().equals(userName) && user1.getPassword().equals(passWord)) {
//传给前端得数据
request.getSession().setAttribute("info", user1);
resp.sendRedirect("MessageServlet?type=query");
return;
}
}
request.getSession().setAttribute("info", "账号或者密码错误");
resp.sendRedirect("fail.jsp");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
request.getSession().setAttribute("info","留言请登录");
response.sendRedirect("fail.jsp");
return;
}
String title=request.getParameter("title");
String content=request.getParameter("content");
String theme=request.getParameter("theme");
message.setTitle(title);
message.setContent(content);
message.setTheme(theme);
message.setTime(String.valueOf(new Date()));
message.setUsername(((User)request.getSession().getAttribute("info")).getUsername());
try {
message.add(message);
response.sendRedirect("MessageServlet?type=query");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("query")){
try {
ArrayList<Message> messages=new ArrayList<Message>();
messages=message.queryAll();
request.getSession().setAttribute("messages", messages);
response.sendRedirect("show.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(type.equals("delete")){
User info = (User) request.getSession().getAttribute("info");
if(info==null){
request.getSession().setAttribute("info","删除留言请登录");
response.sendRedirect("fail.jsp");
return;
}else{
if(!info.getGrade().equals("admin")){
request.getSession().setAttribute("info","非管理员不可删除");
response.sendRedirect("fail.jsp");
return;
}
}