博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
servlet+jdbc+fckeditor网络留言板
阅读量:5159 次
发布时间:2019-06-13

本文共 6522 字,大约阅读时间需要 21 分钟。

一天的时间,终于搞定了这一实例,不用多说,以下为开发实例基本步骤:

1.建表guestbook(id,name,email,url,title,content,time)

2.新建web项目guestbook
3.为项目添加MySQL连接驱动
4.为项目添加fckeditor,放到WebRoot目录下
5.为项目添加js配置文件,也放置倒WebRoot目录下(一直都听说有必要学一学js,此处直接引用,深感其强大便捷)

ContractedBlock.gif
ExpandedBlockStart.gif
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开发中会经常用到)

 

ContractedBlock.gif
ExpandedBlockStart.gif
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("&""&amp");  //实现转译
            input=input.replaceAll("<""&lt");
            input
=input.replaceAll(">""&gt");
            input
=input.replaceAll(" ""&nbsp");
            input
=input.replaceAll("'""'");
            input
=input.replaceAll("\"""&quot;");
            input=input.replaceAll("\n""<br>");
            
return input;
        }
        }

8.创建添加留言的servlet和读取留言的servlet
    AddMessageServlet.java(代码)

 

ContractedBlock.gif
ExpandedBlockStart.gif
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(代码)

 

ContractedBlock.gif
ExpandedBlockStart.gif
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.调试运行程序

转载于:https://www.cnblogs.com/java-zhu/archive/2008/08/08/1263571.html

你可能感兴趣的文章
Objective C 总结(十一):KVC
查看>>
BZOJ 3747 洛谷 3582 [POI2015]Kinoman
查看>>
vue实战(7):完整开发登录页面(一)
查看>>
[转载]mysql的left,right,substr,instr截取字符串,截取
查看>>
Visual Studio自定义模板(二)
查看>>
【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
查看>>
摘抄详细的VUE生命周期
查看>>
javascript高级程序设计---js事件思维导图
查看>>
sprint计划会议
查看>>
读《构建之法-软件工程》第四章有感
查看>>
使用 Printf via SWO/SWV 输出调试信息
查看>>
.net 分布式架构之分布式锁实现(转)
查看>>
吴恩达机器学习笔记 —— 3 线性回归回顾
查看>>
Bouncy Castle内存溢出
查看>>
多线程_java多线程环境下栈信息分析思路
查看>>
机器学习数学【1】
查看>>
Problem E: Automatic Editing
查看>>
Java数组排序
查看>>
SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
查看>>
《DSP using MATLAB》Problem 6.17
查看>>