大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
java实现简单留言板功能的代码实例
数据库对应的表:
guestbook:id ,name,phone,email,title,content,time; id
新建web project:guestbook
build Path:引入连接数据库的驱动的类库
在webRoot目录中加入:
+faceditor
+js
web.xml
代码:
xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
AddMessageServlet
webbook.guestbook.AddMessageServlet
GetMessagesServlet
webbook.guestbook.GetMessagesServlet
AddMessageServlet
/servlet/addMessage
GetMessagesServlet
/servlet/getMessages
addMessage.htm
代码:
add message
请您输入留言
姓名: | |
E-Mail: | |
电话: | |
主题: | |
内容: |
var oFCKeditor = new FCKeditor(“content”); oFCKeditor.BasePath = ‘/guestbook/fckeditor/’ ; oFCKeditor.Height = 300 ; oFCKeditor.ToolbarSet = ‘Basic’; oFCKeditor.Create() ; |
content.html
display content
留言内容
编号 | 12 |
姓名 | liuwei |
电话 | 2922266 |
phpa@www.3ppt.com | |
主题 | you can test |
内容 | test content! |
编号 | |
姓名 | |
电话 | |
主题 | |
内容 |
编号 | |
姓名 | |
电话 | |
主题 | |
内容 |
validation-config.xml
代码;
———————————————————————
******************************************************
这是一个很有用的工具类,包含了:
判读字符串参数是否为空:validateNull(String args)
判断输入的字符串参数是否为空或者是”null”字符:chanageNull(String source, String target)
过滤,n 字符的方法:filterHtml(String input)
*****************************************************
———————————————————————
StringUtil.java
代码:
package webbook.util;
public class StringUtil {
/**
* 判断输入的字符串参数是否为空。
* @param args 输入的字串
* @return true/false
*/
public static boolean validateNull(String args) {
if (args == null || args.length() == 0) {
return true;
} else {
return false;
}
}
/**
* 判断输入的字符串参数是否为空或者是”null”字符,如果是,就返回target参数,如果不是,就返回source参数。
*/
public static String chanageNull(String source, String target) {
if (source == null || source.length() == 0 || source.equalsIgnoreCase(“null”)) {
return target;
} else {
return source;
}
}
/**
* 过滤,n 字符的方法。
* @param input 需要过滤的字符
* @return 完成过滤以后的字符串
*/
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”, “
“);
return input;
}
}
AddmessageServlet.java
代码;
package webbook.guestbook;
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;
import webbook.util.StringUtil;
public class AddMessageServlet extends HttpServlet {
private static final long serialVersionUID = -8349454122547148005L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = “jdbc:oracle:thin:@192.168.1.20:1521:ora9”;
String username = “scott”;
String password = “tiger”;
String sql = “insert into guestbook (id,name,email,phone,title,content,time) values(gb_seq.nextval,?,?,?,?,?,?)”;
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(“”);
out.println(“
guestbook input page”);
out.println(“
“);
if (StringUtil.validateNull(name)) {
out.println(“对不起,姓名不能为空,请您重新输入!
“);
out.println(“添加新的留言
“);
} else if (StringUtil.validateNull(title)) {
out.println(“对不起,主题不能为空,请您重新输入!
“);
out.println(“添加新的留言
“);
} else {
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
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(“对不起,添加留言不成功,请您重新输入!
“);
out.println(“添加新的留言
“);
} else {
out.println(“祝贺您,成功添加留言。
“);
out.println(“查看所有留言内容
“);
}
out.println(“”);
out.println(“”);
out.flush();
out.close();
}
}
}
GetMessageServlet.java
代码:
package webbook.guestbook;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import webbook.util.StringUtil;
public class GetMessagesServlet extends HttpServlet {
private static final long serialVersionUID = 5964428201228635704L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = “jdbc:oracle:thin:@192.168.1.20:1521:ora9”;
String username = “scott”;
String password = “tiger”;
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(“”);
out.println(“
display messages”);
out.println(“
“);
out.println(“添加新的留言内容
“);
out.println(“留言内容
“);
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
//查询结果是一个ResultSet对象。
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(” “);
out.println(“”);
out.flush();
out.close();
}
private void printRow(PrintWriter out, ResultSet rs) throws SQLException {
out.println(“
out.println(“
编号”);
out.println(“
” + rs.getInt(“id”) + “”);
out.println(“
姓名” + rs.getString(“name”) + “”);
out.println(“
电话” +
StringUtil.chanageNull(rs.getString(“phone”), “没填”) +
“
“);
out.println(“
email” +
StringUtil.chanageNull(rs.getString(“email”), “没填”) +
“
“);
out.println(“
主题” + rs.getString(“title”) + ” “);
out.println(“
内容”);
out.println(“
” + StringUtil.chanageNull(rs.getString(“content”), “没填”) + “”);
out.println(“
时间” + rs.getString(“time”) + ” “);
out.println(“
“);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/189579.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...