学习Java Web有一段时间了,借此机会和大家分享一个小东西——在线留言系统,虽然low,小喷即可,大喷伤心,还是希望大家的指正和意见,话不多说,直接上:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<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>
<%@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"%>
<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("
返回登陆");
}else{
out.println("注册失败");
out.println("
返回注册");
}
}catch(Exception e){
e.printStackTrace();
}
%>
body>
html>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<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>
<%@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" %>
<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("
重新登录");
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.closeAll();
}
%>
body>
html>
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;
}
}
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;
}
}
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 getAllUser(){
List userList = new ArrayList();
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;
}
}
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;
}
}
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 getAllMsg(){
List msglist = new ArrayList();
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;
}
}
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;
}
}
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 getAllReply(){
Reply reply = null;
Listreplylist = new ArrayList();
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;
}
}
<%@ 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"%>
<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 userlist = new ArrayList();
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>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<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>
<%@ 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" %>
<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("
查看留言");
}else{
out.println("留言失败!");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
%>
body>
html>
<%@ 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"%>
<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 msglist = new ArrayList();
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">留言IDspan>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>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="com.szx.jnmc.Reply" %>
<%@page import="com.szx.jnmc.ReplyDao" %>
<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>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="com.szx.jnmc.Reply" %>
<%@page import="com.szx.jnmc.ReplyDao" %>
<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("
返回留言列表");
}else{
out.println("回复失败!");
out.println("
重新回复");
}
}catch(Exception e){
e.printStackTrace();
}
%>
body>
html>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import = "com.szx.jnmc.MsgDao" %>
<%@page import = "com.szx.jnmc.Message" %>
<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>主题: <input type = "text" name = "title" value ="${msg.title}">td>
tr>
<tr>
<td>内容: <input type = "text" name = "context" value ="${msg.context}">td>
tr>
<tr>
<%-- "text" name = "lefttime" value ="${msg.lefttime}"> --%>
<td>日期: <span>${msg.lefttime }span>td>
tr>
<%-- "text" name = "wholeft" value ="${msg.wholeft}"readonly="readonly"> --%>
<tr>
<td>留言人:<span>${msg.wholeft}span>td>
tr>
<%-- "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>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import = "com.szx.jnmc.MsgDao" %>
<%@page import = "com.szx.jnmc.Message" %>
<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("
返回留言列表");
}else{
out.println("修改失败");
out.println("
重新修改");
}
}catch(Exception e){
e.printStackTrace();
}
%>
body>
html>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import = "com.szx.jnmc.MsgDao" %>
<%@page import = "com.szx.jnmc.Message" %>
<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;