一天的时间,终于搞定了这一实例,不用多说,以下为开发实例基本步骤:
1.建表guestbook(id,name,email,url,title,content,time)
2.新建web项目guestbook 3.为项目添加MySQL连接驱动 4.为项目添加fckeditor,放到WebRoot目录下 5.为项目添加js配置文件,也放置倒WebRoot目录下(一直都听说有必要学一学js,此处直接引用,深感其强大便捷) Code <script type="text/javascript"> var oFCKeditor = new FCKeditor("content"); oFCKeditor.BasePath = '/guestbook/fckeditor/' ; oFCKeditor.Height = 300 ; oFCKeditor.ToolbarSet = 'Basic'; oFCKeditor.Create() ; </script>
6.创建项目所需要的html页面addMessage.htm
7.创建一个工具类StringUtil(web开发中会经常用到)
Code package com.v512.guest;public class StringUtil { //验证输入字符串是否为空 public static boolean validateNull(String args){ if(args==null||args.length()==0){ return true; } else{ return false; } } String changeNull(String source,String target){ if(source==null||target==null){ return target; } else{ return source; } } //过滤Html代码 public static String filterHtml(String input) { if(input==null){ return null; } if(input.length()==0){ return input; } input=input.replaceAll("&", "&"); //实现转译 input=input.replaceAll("<", "<"); input=input.replaceAll(">", ">"); input=input.replaceAll(" ", " "); input=input.replaceAll("'", "'"); input=input.replaceAll("\"", """); input=input.replaceAll("\n", "<br>"); return input; } }
8.创建添加留言的servlet和读取留言的servlet AddMessageServlet.java(代码)
Code package com.v512.guest;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.text.SimpleDateFormat;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AddMessageServlet extends HttpServlet { private static final long serialVersionUID = -8349454122547148005L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/work"; String username = "root"; String password = ""; String sql = "insert into guestbook (name,email,phone,title,content,time) values(?,?,?,?,?,?)"; int result = 0; Connection conn = null; request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String title = request.getParameter("title"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>guestbook input page</title></head>"); out.println("<body>"); if (StringUtil.validateNull(name)) { out.println("对不起,姓名不能为空,请您重新输入!<br>"); out.println("<a href='/guestbook/addMessage.htm'>添加新的留言</a><br>"); } else if (StringUtil.validateNull(title)) { out.println("对不起,主题不能为空,请您重新输入!<br>"); out.println("<a href='/guestbook/addMessage.htm'>添加新的留言</a><br>"); } else { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, username, password); PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, StringUtil.filterHtml(name)); pstmt.setString(2, StringUtil.filterHtml(request.getParameter("email"))); pstmt.setString(3, StringUtil.filterHtml(request.getParameter("phone"))); pstmt.setString(4, StringUtil.filterHtml(title)); pstmt.setString(5, request.getParameter("content")); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); pstmt.setString(6, sdf.format(new java.util.Date())); result = pstmt.executeUpdate(); pstmt.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (result == 0) { out.println("对不起,添加留言不成功,请您重新输入!<br>"); out.println("<a href='/guestbook/addMessage.htm'>添加新的留言</a><br>"); } else { out.println("祝贺您,成功添加留言。<br>"); out.println("<a href='/guestbook/servlet/getMessage'>查看所有留言内容</a><br>"); } out.println("</body>"); out.println("</html>"); out.flush(); out.close(); } } }
GetMessageServlet.java(代码) Code package com.v512.guest;import java.io.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class GetMessageServlet extends HttpServlet { private static final long serialVersionUID = 8694828946703476237L; StringUtil c=new StringUtil(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/work"; String username = "root"; String password = ""; String sql = "select * from guestbook order by id desc"; Connection conn = null; response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>display messages</title></head>"); out.println("<body>"); out.println("<a href='/guestbook/addMessage.htm'>添加新的留言内容</a><br>"); out.println("留言内容<br><br>"); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, username, password); PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { this.printRow(out, rs); } rs.close(); pstmt.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } out.println("</body>"); out.println("</html>"); out.flush(); out.close(); } private void printRow(PrintWriter out, ResultSet rs) throws SQLException { out .println("<table width=\"600\" border=\"1\" style=\"table-layout:fixed;word-break:break-all\">"); out.println("<tr><td width=\"50\">编号</td>"); out.println("<td width=\"550\">" + rs.getInt("id") + "</td></tr>"); out .println("<tr><td>姓名</td><td>" + rs.getString("name") + "</td></tr>"); out.println("<tr><td>电话</td><td>" + c.changeNull((String)(rs.getString("phone")), "没填") + "</td></tr>"); out.println("<tr><td>email</td><td>" + c.changeNull(rs.getString("email"), "没填") + "</td></tr>"); out.println("<tr><td valign=\"top\">主题</td><td>" + rs.getString("title") + " </td></tr>"); out.println("<tr><td valign=\"top\">内容</td>"); out.println("<td>" + c.changeNull(rs.getString("content"), "没填") + "</td></tr>"); out.println("<tr><td>时间</td><td>" + rs.getString("time") + " </td></tr>"); out.println("</table><br>"); } }
9.调试运行程序