简单的在线留言系统

article/2025/8/29 9:04:45

学习Java Web有一段时间了,借此机会和大家分享一个小东西——在线留言系统,虽然low,小喷即可,大喷伤心,还是希望大家的指正和意见,话不多说,直接上:

第一步:

①.写一个注册页面,这一个页面主要是用户的输入,比较简单,代码如下:

regist.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>注册页面</title>
</head>
<body>
<form method = "POST" action = "registdo.jsp">
用户名:<input type = "text" name= "username">
密码:<input type = "password" name = "userpass">
<input type = "submit" value = "注册"> 
<input type = "reset" value = "重置"> 
</form>
</body>
</html>

效果如图:
这里写图片描述

②.下面这个页面主要负责用来处理业务逻辑以及显示注册的结果,代码如下:

registdo.jsp

<%@page import="com.szx.jnmc.UserDao"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.szx.jnmc.DBOper"%>
<%@page import="java.io.PrintWriter"%>
<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import="java.util.List"%><%@page import="com.szx.jnmc.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=gbk");/* PrintWriter out = response.getWriter();  */String username = request.getParameter("username");String userpass = request.getParameter("userpass");ServletContext ctx = this.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");UserDao dao = new UserDao();User user = new User();user.setUsername(username);user.setUserpass(userpass);try{dao.getConn(server, dbname, dbuser, dbpwd);if(dao.addUser(user)){out.println("注册成功");out.println("<br><a href = 'login.jsp'>返回登陆</a></br>");}else{out.println("注册失败");out.println("<br><a href = 'regist.jsp'>返回注册</a></br>");}}catch(Exception e){e.printStackTrace();}
%>
</body>
</html>

效果如图:
这里写图片描述

第二步:

①.这一步写个登录页面,主要用来接收用户输入的登录信息:代码如下:

login.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<form method = "POST" action = "logindo.jsp">
用户名:<input type = "text" name = "username">
密码:<input type = "password" name = "userpass">
<input type = "submit" value= "登录">
<input type = "reset"value = "重置">
</form>
</body>
</html>

效果如图
这里写图片描述

②.这一步是需要写登录的逻辑及验证,并显示登录的结果,代码如下

logindo.jsp

<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=gbk"pageEncoding="gbk"%><%@page import = "com.szx.jnmc.DBOper" %><%@page import = "javax.servlet.http.Cookie" %><%@page import = "javax.servlet.RequestDispatcher" %><%@page import = "javax.servlet.http.HttpSession" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>留言板</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");String username = request.getParameter("username");String userpass = request.getParameter("userpass");ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");DBOper db = new DBOper();String sql = "SELECT * FROM user Where username = ? AND userpass = ?";try{db.getConn(server, dbname, dbuser, dbpwd);ResultSet rs = db.executeQuery(sql, new String[]{username,userpass});if(rs!= null && rs.next()){session.setAttribute("username", username);Cookie cookie  = new Cookie("username",username);cookie.setMaxAge(60*60*24*30);response.addCookie(cookie);RequestDispatcher dispatcher = request.getRequestDispatcher("userlist.jsp");dispatcher.forward(request, response);}else{out.println("登录失败!");out.println("<br><a href = 'login.jsp'>重新登录</a></br>");}}catch(Exception e){e.printStackTrace();}finally{db.closeAll();}
%>
</body>
</html>

效果如图
这里写图片描述

前台的注册及登录页面到此已完成,下面我们接着做后台的数据

第二步

①.我们需要User类封装用户的信息,代码如下:

User.Java

package com.szx.jnmc;public class User {private int id;private String username;private String userpass;private String phone;   private String email;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUserpass() {return userpass;}public void setUserpass(String userpass) {this.userpass = userpass;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}}

②.有了User类,就需要进行数据库的访问,这时候我们需要一个连接数据库的基础类,所有与连接数据库的类都要 继承于此,代码如下:

DBOper.Java

    public Connection conn = null;public PreparedStatement ps = null;public ResultSet rs = null;/*** 连接数据库* @param server* @param dbname* @param dbuser* @param dbpwd* @return* @throws SQLException* @throws ClassNotFoundException*/public Connection getConn(String server,String dbname,String dbuser,String dbpwd) throws SQLException, ClassNotFoundException{      String DRIVER = "com.mysql.jdbc.Driver";String URL = "jdbc:mysql://" + server + ":3306/" + dbname + "?user="+ dbuser + "&password=" +dbpwd+"&useUnicode=true&characterEncoding=utf8";Class.forName(DRIVER);conn = DriverManager.getConnection(URL);        return conn;}/*** 关闭数据库连接*/public void closeAll(){try{if(rs != null){rs.close();}}catch(SQLException e){e.printStackTrace();}finally{try{if(ps != null){ps.close();}}catch(SQLException e){e.printStackTrace();                }finally{try{if(conn != null){conn.close();}}catch(SQLException e){e.printStackTrace();                    }}}}/*** 执行SQL语句,只进行查询,不进行增删改* @param preparedsql* @param param* @return*/public ResultSet executeQuery(String preparedsql,String[]param){try{ps = conn.prepareStatement(preparedsql);if(param != null){for (int i = 0; i < param.length; i++) {ps.setString(i + 1, param[i]);}}rs = ps.executeQuery();}catch(SQLException e){e.printStackTrace();}return rs;}/*** 执行sql语句,进行增删改,不进行查询* @param preparedsql* @param param* @return*/public int executeUpdate(String preparedsql,String[]param){int num = 0;try{ps = conn.prepareStatement(preparedsql);if(param != null){for (int i = 0; i < param.length; i++) {ps.setString(i + 1, param[i]);}}num = ps.executeUpdate();}catch(SQLException e){e.printStackTrace();}return num;}
}

③.写完了数据库操作对象类,就要写用户 操作对象类代码如下,

UserDao.Java

package com.szx.jnmc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class UserDao extends DBOper{/*** 获取所有用户列表* @return*/public List<User> getAllUser(){List<User> userList = new ArrayList<User>();String sql = "Select *from user";try{ResultSet rs = this.executeQuery(sql,null);while(rs.next()){User user = new User();             user.setUsername(rs.getString("username"));user.setUserpass(rs.getString("userpass")); userList.add(user);}}catch(SQLException e){e.printStackTrace();}return userList;        }/*** 根据用户名获取用户信息* @param name* @return*/public User getUserByName(String name){User user = new User();String sql = "SELECT * FROM user Where username = ?";try{ResultSet rs = this.executeQuery(sql, new String []{name});if(rs.next()){user.setEmail(rs.getString("email"));user.setId(rs.getInt("id"));user.setPhone(rs.getString("phone"));user.setUsername(rs.getString("username"));user.setUserpass(rs.getString("userpass"));}}catch(SQLException e){e.printStackTrace();}return user;} /*** 添加用户* @param user* @return*/public boolean addUser(User user){boolean r = false;String sql  = "INSERT INTO user(username,userpass)VALUES(?,?)";try{int num = this.executeUpdate(sql,new String []{user.getUsername(),user.getUserpass()});if(num > 0){r = true;}}catch(Exception e){e.printStackTrace();}       return r;       }  
}

④.我们的留言Message类,代码如下:

Message.Java

package com.szx.jnmc;public class Message {private int messageid;private String title;private String context;private String lefttime;private String wholeft;public String getWholeft() {return wholeft;}public void setWholeft(String wholeft) {this.wholeft = wholeft;}public int getMessageid() {return messageid;}public void setMessageid(int messageid) {this.messageid = messageid;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContext() {return context;}public void setContext(String context) {this.context = context;}public String getLefttime() {return lefttime;}public void setLefttime(String lefttime) {this.lefttime = lefttime;}
}

⑤.留言数据操作对象MsgDao类,代码如下:

MsgDao.Java

package com.szx.jnmc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class MsgDao extends DBOper {/*** 获取所有留言* @return*/public List<Message> getAllMsg(){       List<Message> msglist = new ArrayList<Message>();String sql = "SELECT * FROM message";try{            ResultSet rs = this.executeQuery(sql, null);while(rs.next()){           Message msg = new Message();msg.setMessageid(rs.getInt("messageid"));msg.setContext(rs.getString("context"));                msg.setLefttime(rs.getString("lefttime"));msg.setTitle(rs.getString("title"));msg.setWholeft(rs.getString("wholeft"));msglist.add(msg);}}catch(SQLException e){e.printStackTrace();}finally{this.closeAll();}return msglist;}public Message getMsgById(String id){Message msg = null;String sql = "SELECT * FROM message WHERE messageid = ?";try{ResultSet rs = this.executeQuery(sql, new String[]{id});if(rs.next()){msg = new Message();msg.setContext(rs.getString("context"));            msg.setLefttime(rs.getString("lefttime"));msg.setMessageid(rs.getInt("messageid"));msg.setTitle(rs.getString("title"));msg.setWholeft(rs.getString("wholeft"));}}catch(SQLException e){e.printStackTrace();}return msg;} /*** 添加留言* @param msg* @return*/public boolean addMsg(Message msg){boolean r = false;String sql = "INSERT INTO message(title,context,wholeft,lefttime)VALUES(?,?,?,?) ";try{int num = this.executeUpdate(sql, new String[]{msg.getTitle(),msg.getContext(),msg.getWholeft(),msg.getLefttime()});if(num > 0){r = true;}}catch(Exception e){e.printStackTrace();}finally{this.closeAll();}       return r;}/*** 修改留言信息* @param msg* @return*/public boolean editMsg(Message msg){boolean r = false;String sql = "UPDATE message SET context = ?,title =?,lefttime = ?,wholeft=? WHERE messageid ="+msg.getMessageid();try{int num = this.executeUpdate(sql,new String[]{msg.getContext(), msg.getTitle(), msg.getLefttime(), msg.getWholeft()});System.out.println("num:"+num);if(num > 0){r = true;}}catch(Exception e){            e.printStackTrace();}return r;               }/*** 根据消息Id删除留言* @param id* @return*/public boolean delMsg(int id){boolean r = false;String sql = "DELETE FROM message WHERE messageid = ?";try{int num = this.executeUpdate(sql, new String[]{""+id});if(num > 0){r = true;}}catch(Exception e){e.printStackTrace();}return r;       }
}

⑥.回复Reply类,代码如下:

Reply.Java

package com.szx.jnmc;public class Reply {private String context;private int messageid;public int getMessageid() {return messageid;}public void setMessageid(int messageid) {this.messageid = messageid;}public String getContext() {return context;}public void setContext(String context) {this.context = context;}   
}

⑦.Reply的数据操作对象类,代码如下:

ReplyDao.Java


package com.szx.jnmc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class ReplyDao extends DBOper{/*** 获取所有留言* @return*/public List<Reply> getAllReply(){Reply reply = null;List<Reply>replylist = new ArrayList<Reply>();String sql = "SELECT * FROM reply";try{ResultSet rs = this.executeQuery(sql, null);while(rs.next()){reply = new Reply();reply.setContext(rs.getString("context"));              replylist.add(reply);}}catch(SQLException e){e.printStackTrace();}finally{this.closeAll();}return replylist;       }/*** 添加回复* @param reply* @return*/public boolean addReply(Reply reply){boolean r = false;String sql = "INSERT INTO reply(context,messageid)VALUES(?,?)";try{int num = this.executeUpdate(sql, new String[]{reply.getContext(),""+reply.getMessageid()});if(num > 0){r = true;}}catch(Exception e){e.printStackTrace();}finally{this.closeAll();}return r;}
}

第二步到此结束,这一步主要是对数据的封装和连接数据库。

第三步:

①.显示用户列表

userlist.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import = "java.util.List" %><%@page import = "java.util.ArrayList" %><%@page import = "com.szx.jnmc.User" %><%@page import = "com.szx.jnmc.UserDao" %><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;char=gbk");ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");List<User> userlist = new ArrayList<User>();UserDao dao = new UserDao();try{dao.getConn(server,dbname,dbuser,dbpwd);        }catch(Exception e){e.printStackTrace();}    userlist = dao.getAllUser(); pageContext.setAttribute("userlist", userlist); 
%>      <div class="list_div" style="height: 87%"><table border="1" align="center" cellspacing="0" class="list_table"id="senfe" style='width: 50%'><thead><tr><th width="5%"><span style="font-weight: 10">序号</span></th><th width="5%"><span style="font-weight: 10">用户名</span></th><th width="5%"><span style="font-weight: 10">操作</span></th>                 </tr></thead><tbody>                     <c:forEach var="user" items="${userlist}" varStatus="status"><tr>                        <td align="center">${status.count }</td><td align="center">${user.username}</td>                       <td align="center"><a
                            href="addmsg.jsp?username=${user.username}">留言</a></td>                        </tr>                   </c:forEach></tbody></table></div>      
</body>
</html>

效果如图
这里写图片描述

②.在用户列表里我们点击“留言”就可以对用户留言,页面将跳转到留言页面,如下:

addmsg.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<form method = "POST" action = "addOK.jsp">
留言人:<input type = "text" name = "wholeft"/>
留言主题:<input type = "text" name = "title"/>
留言内容:<input type = "text" name = "context"/>
<input type = "submit" value = "留言"/>
<input type = "reset" value = "重置"/>
</form>
</body>
</html>

效果如图:
这里写图片描述

③.留言页面的数据处理,代码如下:

addOK.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import = "com.szx.jnmc.Message" %><%@page import = "com.szx.jnmc.MsgDao" %><%@page import = "java.text.SimpleDateFormat" %><%@page import = "java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");String wholeft = request.getParameter("wholeft");String title = request.getParameter("title");String context = request.getParameter("context");ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");Date curtime = new Date();String lefttime = sdf.format(curtime);Message msg = new Message();msg.setContext(context);msg.setLefttime(lefttime);msg.setTitle(title);msg.setWholeft(wholeft);MsgDao dao = new MsgDao();try{dao.getConn(server, dbname, dbuser, dbpwd);if(dao.addMsg(msg)){out.println("留言成功!");out.println("<br><a href = 'showmsg.jsp'>查看留言</a></br>");}else{out.println("留言失败!");}}catch(ClassNotFoundException e){e.printStackTrace();}
%>  
</body>
</html>

效果如图:
这里写图片描述

④.点击“查看留言”,将跳转到showmsg.jsp页面,代码如下:

showmsg.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import = "java.util.List" %><%@page import = "java.util.ArrayList" %><%@page import = "com.szx.jnmc.MsgDao" %><%@page import = "com.szx.jnmc.Message" %><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");MsgDao dao = new MsgDao();  List<Message> msglist = new ArrayList<Message>();try{dao.getConn(server,dbname,dbuser,dbpwd);                }catch(Exception e){e.printStackTrace();}msglist = dao.getAllMsg();if(msglist!=null){application.setAttribute("msglist",msglist);}
%>
<div class="list_div" style="height: 87%"><table border="1" align="center" cellspacing="0" class="list_table"id="senfe" style='width: 80%'><thead><tr><th width="10%"height="30"><span style="font-weight: 10">留言序号</span></th><th width="10%"height="30"><span style="font-weight: 10">留言ID</span></th><th width="5%"height="30"><span style="font-weight: 10">留言人</span></th><th width="10%"height="30"><span style="font-weight: 10">主题</span></th> <th width="25%"height="30"><span style="font-weight: 10">留言内容</span></th>   <th width="20%"height="30"><span style="font-weight: 10">操作</span></th>                         </tr></thead><tbody>                     <c:forEach var="msg" items="${msglist}" varStatus="status"><tr>                        <td align="center">${status.count }</td><td align="center">${msg.messageid}</td>   <td align="center">${msg.wholeft}</td> <td align="center">${msg.title}</td>               <td align="center">${msg.context}</td>                                 <td align="center"><a href="reply.jsp?messageid=${msg.messageid}">回复</a><a href="editmsg.jsp?messageid=${msg.messageid}">编辑</a><a href="delmsg.jsp?messageid=${msg.messageid}">删除</a>                                             </td>                       </tr>                   </c:forEach>                </tbody></table></div>      
</body>
</html>

效果如图:
这里写图片描述

⑤.点击页面中的“回复”将跳转到reply.jsp,代码如下:

reply.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import="com.szx.jnmc.Reply" %><%@page import="com.szx.jnmc.ReplyDao" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>回复页面</title>
</head>
<body>
<%String messageid = request.getParameter("messageid");   if(messageid != null || messageid.equals("")){pageContext.setAttribute("messageid", messageid);}
%> 
<form method = "POST" action = "replydo.jsp">
内容:<input type = "text" name = "context">
<input type = "hidden" name = "id" value="${messageid }">
<input type = "submit" value = "提交">
<input type = "reset" value ="重置">
</form>
</body>
</html>

效果如图:
这里写图片描述

⑥.输入回复数据,点击提交转到回复结果replydo.jsp页面,代码如下:

replydo.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import="com.szx.jnmc.Reply" %><%@page import="com.szx.jnmc.ReplyDao" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>回复留言</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");String messageid = request.getParameter("id");String context = request.getParameter("context");   ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");Reply reply = new Reply();ReplyDao dao = new ReplyDao();reply.setContext(context);try{dao.getConn(server, dbname, dbuser, dbpwd);if(dao.addReply(reply)){out.println("回复成功!");out.println("<br><a href = 'showmsg.jsp'>返回留言列表</a></br>");}else{out.println("回复失败!");out.println("<br><a href = 'replydo.jsp'>重新回复</a></br>");}}catch(Exception e){e.printStackTrace();}
%>
</body>
</html>

效果如图:
这里写图片描述

⑦.点击返回留言列表,转到留言列表showmsg.jsp页面,进行编辑,代码如下:

editmsg.jsp


<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import = "com.szx.jnmc.MsgDao" %><%@page import = "com.szx.jnmc.Message" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>修改留言</title>
</head>
<body>
<%String messageid = request.getParameter("messageid");request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");MsgDao dao = new MsgDao();try{dao.getConn(server, dbname, dbuser, dbpwd);Message msg = dao.getMsgById(messageid);if(msg != null){pageContext.setAttribute("msg", msg);}}catch(Exception e){e.printStackTrace();}   
%>
<form method = "POST" action = "editmsgdo.jsp"> 
<table><tr><td>主题:&nbsp;&nbsp;<input type = "text" name = "title" value ="${msg.title}"></td></tr><tr><td>内容:&nbsp;&nbsp;<input type = "text" name = "context" value ="${msg.context}"></td></tr><tr><%-- <input type = "text" name = "lefttime" value ="${msg.lefttime}"> --%><td>日期:&nbsp;&nbsp;<span>${msg.lefttime }</span></td>  </tr><%-- <input type = "text" name = "wholeft" value ="${msg.wholeft}"readonly="readonly"> --%><tr><td>留言人:<span>${msg.wholeft}</span></td></tr><%-- <input type ="text" name = "messageid" value = "${msg.messageid }" readonly="readonly"> --%><tr><td>留言id:<input type ="text" name = "messageid" value = "${msg.messageid }" readonly="readonly"></td>      </tr><tr><td><td><input type = "submit" value = "提交"></td>   <td><input type = "reset" value = "重置"></td></tr></table>
</form>
</body>
</html>

效果如图:
这里写图片描述

⑧.点击提交,跳转到 编辑结果页面editmsgdo.jsp,代码如下:

editmsgdo.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import = "com.szx.jnmc.MsgDao" %><%@page import = "com.szx.jnmc.Message" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>修改留言</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");String title = request.getParameter("title");String context = request.getParameter("context");String messageid = request.getParameter("messageid");String lefttime = request.getParameter("lefttime");String wholeft = request.getParameter("wholeft");   ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");   MsgDao dao = new MsgDao();  Message msg = new Message();msg.setContext(context);msg.setTitle(title);msg.setLefttime(lefttime);msg.setWholeft(wholeft);try{dao.getConn(server,dbname,dbuser,dbpwd);if(dao.editMsg(msg)){out.println("修改成功");out.println("<br><a href = 'showmsg.jsp'>返回留言列表</a></br>");}else{out.println("修改失败");out.println("<br><a href = 'editmsgdo.jsp'>重新修改</a></br>");}}catch(Exception e){e.printStackTrace();}
%>
</body>
</html>

效果如图:
这里写图片描述

⑨.点击“返回留言列表 ”回到留言列表页面,点击删除则直接删除留言,代码如下:

delmsg.jsp

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="GBK"%><%@page import = "com.szx.jnmc.MsgDao" %><%@page import = "com.szx.jnmc.Message" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>留言板</title>
</head>
<body>
<%request.setCharacterEncoding("GBK");response.setContentType("text/html;charset=GBK");ServletContext ctx = request.getServletContext();String server = ctx.getInitParameter("server");String dbname = ctx.getInitParameter("dbname");String dbuser = ctx.getInitParameter("dbuser");String dbpwd = ctx.getInitParameter("dbpwd");MsgDao dao = new MsgDao();try{String messageid = request.getParameter("messageid");dao.getConn(server,dbname,dbuser,dbpwd);if(dao.delMsg(Integer.parseInt(messageid))){out.println("删除成功");response.sendRedirect("showmsg.jsp");}else{out.println("删除失败");}}catch(Exception e){e.printStackTrace();}   
%>
</body>
</html>

数据表的创建SQL语句:
user表:

CREATE TABLE `user` (`username` varchar(255) NOT NULL,`userpass` varchar(255) NOT NULL,`phone` varchar(255) DEFAULT NULL,`email` varchar(255) DEFAULT NULL,`userid` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8;

message表:

CREATE TABLE `message` (`messageid` int(11) NOT NULL AUTO_INCREMENT,`title` varchar(255) DEFAULT NULL,`context` varchar(255) DEFAULT NULL,`wholeft` varchar(255) DEFAULT NULL,`lefttime` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`messageid`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

reply表:

CREATE TABLE `reply` (`context` varchar(255) DEFAULT NULL,`messageid` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`messageid`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

附上工程的结构图:

这里写图片描述

至此我们的主要功能已经完成,页面非常low。不多说,也不早了,困死,该休息一会儿了。

在此附上工程压缩包下载地址:http://download.csdn.net/download/itcreater000/9860689


http://chatgpt.dhexx.cn/article/AiO7ENmB.shtml

相关文章

网络留言系统(Javaweb简易开发)

目录 第一步&#xff1a;建立数据库test 第二步&#xff1a;创建javaee项目&#xff0c;onlineMessage 第三步&#xff1a;创建JDBC 先导入JDBC驱动包 创建SqlSrvDBConn类 第四步&#xff1a;构件JavaBean 第五步&#xff1a;编写Servlet MainServlet AddServlet Regi…

留言管理系统的设计与实现

一、设计目的 留言系统目前在各类网站中被广泛使用。通过综合运用JSP、Servlet、JavaBean、JDBC、MySQL和MVC DAO等技术与方法&#xff0c;设计与实现一个留言管理系统&#xff0c;进一步理解和掌握Java Web编程开发的基本技术和方法&#xff0c;熟练使用开发工具。 二、课程…

留言板系统的设计与实现_kaic

目 录 内容摘要 Abstract Key Words 1.课程题目介绍 1.1选题背景及意义 1.2需求分析 1.3达到的目标 2&#xff0e;系统总体模块 2.1 系统总体模块流程图 2.2 配置服务器 3.各功能模块界面设计与功能实现 3.1首页界面 3.2 文章日记管理…

位操作(左移和右移)

位操作提高程序运行效率 减少除法和取模的运算。在计算机程序中数据的位是可以操作的最小数据单位&#xff0c;理论上可以用”位运算”来完成所有的运算和操作。 左移&#xff0c;后空缺自动补0&#xff1b; 右移&#xff0c;分为逻辑右移和算数右移 1&#xff09;逻辑右移 …

`>>`(有符号右移) 和 `>>>`(无符号右移)的区别

JavaScript >>(有符号右移) 和 >>>(无符号右移)的区别 之所以以 JS 为例&#xff0c;是因为Java中不支持无符号操作 直接看MDN英文吧&#xff1a; >> (有符号右移) sign-propagating&#xff08;符号位-传播&#xff09;右移 我们知道&#xff0c;计算…

0x,左移,右移

0x代表的是十六进制&#xff0c;表示的是补码&#xff0c;默认为int类型&#xff0c;即32位&#xff0c;不足高位补0。如&#xff1a;0xff0x000000ff&#xff0c;每一位代表一个十六进制&#xff0c; -2表示的补码为1111 1111 1111 1111 1111 1111 1111 1110。十六进制为 0xfff…

右移

今天看到一道题&#xff0c;题为&#xff1a;用最有效率的方法算出2乘以8等於几?答案是&#xff1a;2 << 3&#xff0c;因为将一个数左移n位&#xff0c;就相当于乘以了2的n次方&#xff0c;那么&#xff0c;一个数乘以8只要将其左移3位即可&#xff0c;而位运算cpu直接…

企业信息安全————2、如何描述风险价值

什么是信息安全风险 安全的定义&#xff1a; 例如&#xff1a; 漏洞未修复————脆弱性 发生概率————威胁 带来影响————风险 威胁和脆弱性的关系&#xff1a; 功能越强大、提供的服务越多可能存在漏洞的概率也就越高。 总结&#xff1a;可以利用脆弱性&#xff0…

R语言用GARCH模型波动率建模和预测、回测风险价值 (VaR)分析股市收益率时间序列...

原文链接&#xff1a;http://tecdat.cn/?p26897 风险价值 (VaR) 是金融风险管理中使用最广泛的市场风险度量&#xff0c;也被投资组合经理等从业者用来解释未来市场风险&#xff08;点击文末“阅读原文”获取完整代码数据&#xff09;。 风险价值 (VaR) VaR 可以定义为资产在给…

【敏捷7.1】风险管理

风险管理 在 PMP 中&#xff0c;风险是一个重要的章节&#xff0c;并且有许多的过程&#xff0c;比如说我们要识别风险、进行定性定量分析、应对风险等&#xff0c;工具方面也有决策树、敏捷性分析等&#xff0c;最后还有一个风险应对和机会应对&#xff08;PMP认为风险和机会是…

MATLAB数学建模(二):评估股票的价值和风险

以下内容为学习笔记&#xff0c;更多的内容请访问原链接&#xff1a;https://mp.weixin.qq.com/s/fcCxFpiLMT1uFsyTFux_ig? 以下学习笔记与原链接的不同之处在于&#xff1a;对原链接内容进行了增删&#xff0c;删去了一些题外话&#xff0c;增加了对一些概念的解释&#xff0…

风险控制之VaR

什么是VaR VaR是value of risk的缩写称为风险价值&#xff0c;或者受险价值&#xff0c;指的是在一定的概率下&#xff0c;一个金融资产在未来一段时间内的最大可能损失。常用于金融机构的风险管理。它的数学定义为&#xff1a; 其中&#xff0c;的含义是金融资产在持有期的时…

拓端tecdat|R语言风险价值VaR(Value at Risk)和损失期望值ES(Expected shortfall)的估计

最近我们被要求撰写关于风险价值VaR的研究报告&#xff0c;包括一些图形和统计输出。 视频&#xff1a;风险价值VaR原理与Python蒙特卡罗Monte Carlo模拟计算投资组合实例 风险价值VaR原理与Python蒙特卡罗Monte Carlo模拟计算投资组合实例 &#xff0c;时长10:03 风险价值VaR和…

用Flink SQL流化市场数据2:盘中风险价值

本文是一个由多部分组成的系列文章的第二篇&#xff0c;该系列文章展示了FlinkSQL应用于市场数据的功能和可表达性。万一您错过了它&#xff0c;第一部分从计算流VWAP的简单情况开始。该系列的代码和数据可在github上获得。 速度在金融市场上至关重要。无论目标是最大化alpha还…

拓端tecdat|Python计算股票投资组合的风险价值(VaR)

最近我们被要求撰写关于风险价值&#xff08;VaR&#xff09;的研究报告&#xff0c;包括一些图形和统计输出。 什么是风险价值&#xff08;VaR&#xff09;&#xff1f; 风险价值&#xff08;VaR&#xff09;用于尝试量化指定时间范围内公司或投资组合中的财务风险水平。VaR提…

R语言风险价值:ARIMA,GARCH模型,Delta-normal法滚动估计,预测VaR(Value at Risk)和回测分析花旗公司股票时间序列数据

最近我们被客户要求撰写关于风险价值可视化的研究报告&#xff0c;包括一些图形和统计输出。 相关视频&#xff1a;时间序列分析&#xff1a;ARIMA GARCH模型分析股票价格数据 时间序列分析模型 ARIMA-ARCH GARCH模型分析股票价格数据 相关视频&#xff1a;在Python和R语言中建…

蒙特卡洛模拟计算风险价值VAR之R语言实现

一、解析VAR 当在分析方法中计算风险价值&#xff08;VAR&#xff09;时&#xff0c;我们需要假设金融工具的返回遵循一定的概率分布。最常用的是正态分布&#xff0c;这也是为什么我们通常称它为delta normal方法。要计算VAR&#xff0c;我们需要找到一个阈值&#xff08;T&am…

VaR - 风险价值 - 蒙特卡罗法 - Python

风险价值&#xff08;VaR&#xff09;&#xff1a;即在市场正常波动的条件下&#xff0c;在一定概率水平P%下&#xff0c;某一金融资产或金融资产组合的VaR是在未来特定一段时间Δt内最大可能损失。 现在我们使用蒙特卡罗模拟法进行风险价值的估算。简单来说&#xff0c;蒙特卡…

R语言用GARCH模型波动率建模和预测、回测风险价值 (VaR)分析股市收益率时间序列

最近我们被客户要求撰写关于GARCH的研究报告&#xff0c;包括一些图形和统计输出。 风险价值 (VaR) 风险价值 (VaR) 是金融风险管理中使用最广泛的市场风险度量&#xff0c;也被投资组合经理等从业者用来解释未来市场风险。VaR 可以定义为资产在给定时间段内以概率 θ 超过的市…

VaR风险价值-Python版本

1、VaR简介 2、VaR原理 3、不同VaR实现方法及适用场景 3.1 历史模拟法 3.1.1 使用TUSHARE读入美的复权后估计数据 隆重介绍一下TUSHARE&#xff0c; 非常好的财经数据库&#xff0c; 能获取到国内股价信息 #环境&数据准备 import sys as sy import numpy as np import…