基于mysql数据库的JSP留言本代码
2018-07-20 来源:open-open
message.jsp
showMessage.jsp
---com.demo包---
Message.java
MessageServlet.java
MessageDao.java
DbConnection.java
数据库脚本
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>留言给我</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> <!-- textarea { FONT: 12px Tahoma; COLOR: #333333; margin-top: 3px; margin-bottom: 3px; } .input1 { width: 300px; height: 25px; margin-top: 3px; margin-bottom: 3px; } .input2 { width: 150px; height: 25px; margin-top: 3px; margin-bottom: 3px; } .submit1 { width: 50px; border: #CCCCCC solid 1px; height: 30px; background: #FFFFFF } #form1 tr th { text-align: right; } input { text-align: left; } body { font-family: "微软雅黑", Verdana, sans-serif, "宋体"; color: #333; } .mybutton { width: 70px; height: 25px; margin-top: 5px; margin-bottom: 5px; text-align: center; } h1 { margin-top: 100px; margin-bottom: 50px; color: #999; font-weight: bold; font-size: 36px; } --> </style> <script> function addCheck() { var name = document.getElementById("name").value; var title = document.getElementById("title").value; var content = document.getElementById("content").value; if (name == "") { alert("姓名容都不能为空!") document.getElementById("name").focus(); return false; } if (title == "") { alert("主题都不能为空!") document.getElementById("title").focus(); return false; } if (content == "") { alert("内容不能为空!") document.getElementById("content").focus(); return false; } } </script> </head> <body> <table width="400" align="center" cellpadding="2" cellspacing="0"> <tr> <td colspan="2"><h1> 留言本 </h1></td> </tr> <form name="form1" id="form1" action="<%=basePath%>servlet/MessageServlet" method="post" onSubmit="javascript: return addCheck()"> <tr> <th width="70" align="right"> 姓名: </th> <td width="280" align="left"><input name="name" type="text" class="input2" id="name"></td> </tr> <tr> <th > 主题: </th> <td><input name="title" type="text" class="input1"></td> </tr> <tr> <th ><span class="STYLE1">留言</span>: </th> <td ><textarea name="content" id="content" cols="60" rows="5"></textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="提交留言" class="mybutton"> <input type="button" name="submit" id="submit" value="查看留言" onclick="window.location.href('<%=basePath%>servlet/MessageServlet?action=list&&p=1')" class="mybutton"> <input type="reset" name="reset" id="reset" value="重新填写" class="mybutton"> <input name="action" type="hidden" id="check" value="add"></td> </tr> </form> </table> </body> </html>
showMessage.jsp
<%@ page language="java" import="java.util.*,com.demo.Message" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>查看留言</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> .line { border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #666; height: 10px; } body { margin-right: auto; margin-left: auto; text-align: center; max-width: 960px; } #main { border: 3px solid #999; background-color: #f5f5f5; padding: 1px; margin-right: auto; margin-left: auto; width: 960px; } #main table tr td { white-space: normal; } </style> </head> <body> <div id="main"> <div align="center"> <h1> 留言列表 </h1> </div> <% response.setCharacterEncoding("utf-8"); int p = 1; ArrayList messageList = (ArrayList) request.getAttribute("list"); int prep = p; // 上一页 int nextp = p; // 下一页 if (messageList.size() == 5) { nextp = p + 1; } if (p > 1) { prep = p - 1; } if (!messageList.isEmpty()) { for (int i = 0; i < messageList.size(); i++) { Message message = (Message) messageList.get(i); %> <table width="700" border="1" align="center" cellpadding="2" cellspacing="0"> <tr> <td width="130" align="right"> 姓名: </td> <td width="556" align="left"><%=message.getName()%></td> </tr> <tr> <td align="right"> 主题: </td> <td colspan="3" align="left"><%=message.getTitle()%></td> </tr> <tr> <td align="right"><span class="STYLE1">内容</span>: </td> <td colspan="3" align="left"><%=message.getContent()%></td> </tr> <div> <span style="float: right; clear: both"> <%=message.getTime()%> </span> <span><%=i + 1%>楼</span> </div> </table> <br> <div class="line"> </div> <br> <% } } else { out.print("<br><br>还没有留言!<br><br>"); } %> <div align="center"> <input type="button" value="上一页" onclick="window.location.href('MessageServlet?action=list&p=<%=prep%>')"> <input type="button" value="下一页" onclick="window.location.href('MessageServlet?action=list&p=<%=nextp%>')"> <input type="button" value="返回" onclick="window.location.href('../message.jsp')"> </div> </div> </body> </html>
---com.demo包---
Message.java
package com.demo; /** * 留言实体封装类 * */ public class Message { private int id; private String name; private String title; private String content; private String time; public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
MessageServlet.java
package com.demo; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.demo.Message; import com.demo.MessageDao; public class MessageServlet extends HttpServlet { public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); String action = request.getParameter("action"); if (action.equals("add")) { this.addMessage(request, response); // 发表留言 } if (action.equals("list")) { // 前台留言列表 this.listMessage(request, response); } } /** * 发表留言 */ public void addMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String name = request.getParameter("name"); String title = request.getParameter("title"); String content = request.getParameter("content"); Message message = new Message(); message.setName(name); message.setTitle(title); message.setContent(content); MessageDao messageDao = new MessageDao(); messageDao.addMessage(message); out.print(""); } /** * * @前台查看所有留言 */ public void listMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); int page = Integer.parseInt(request.getParameter("p")); MessageDao messageDao = new MessageDao(); List list = null; list = messageDao.listByPage(page); request.setAttribute("list", list); request.getRequestDispatcher("../showMessage.jsp").forward(request, response); } public void init() throws ServletException { } }
MessageDao.java
package com.demo; 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 com.demo.DbConnection; import com.demo.Message; /** * * 留言保存数据库访问类 * */ public class MessageDao { private Connection coon = null; private PreparedStatement pstmt = null; private ResultSet rs = null; private String sql = ""; public MessageDao() { coon = DbConnection.getConnection(); } /** * 保存留言 */ public void addMessage(Message message) { System.out.println(message.getName()); coon = DbConnection.getConnection(); sql = "INSERT INTO `message` ( name, title,content,time)VALUES (?,?,?,now())"; try { pstmt = coon.prepareStatement(sql); pstmt.setString(1, message.getName()); pstmt.setString(2, message.getTitle()); pstmt.setString(3, message.getContent()); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } /** * 查询所有留言 */ public List listByPage(int page) { int num = 5;// 每页显示的记录数 List list = new ArrayList(); int rowBegin = 0; int rowEnd = 0; if (page > 1) { rowBegin = num * (page - 1); rowEnd = rowBegin + num; sql = "select * from message where id limit " + rowBegin + "," + rowEnd; } else { sql = "select * from message order by id limit 5"; } try { pstmt = coon.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { Message message = new Message(); message.setId(rs.getInt(1)); message.setName(rs.getString(2)); message.setTitle(rs.getString(3)); message.setContent(rs.getString(4)); message.setTime(rs.getDate(5) + " " + rs.getTime(5)); list.add(message); } } catch (SQLException e) { e.printStackTrace(); } return list; } }
DbConnection.java
package com.demo; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接类 * */ public class DbConnection { public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager .getConnection( "jdbc:mysql://127.0.0.1:3306/message?useUnicode=true&characterEncoding=utf8", "**********", "*********"); // **********分别代表数据库用户名,密码 } catch (Exception e) { e.printStackTrace(); } if (conn == null) { System.out.println("no get connection!throws Exception"); } return conn; } }
数据库脚本
-- ---------------------------- -- Table structure for `message` -- ---------------------------- DROP TABLE IF EXISTS `message`; CREATE TABLE `message` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `title` varchar(200) NOT NULL, `content` varchar(500) NOT NULL, `time` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of message -- ----------------------------
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:JavaScript格式化日期
下一篇:倒计时的一段JS脚本
最新资讯
热门推荐