學(xué)習(xí)Java Web有一段時(shí)間了夯秃,借此機(jī)會(huì)和大家分享一個(gè)小東西——在線留言系統(tǒng)介劫,雖然low徽惋,小噴即可,大噴傷心座韵,還是希望大家的指正和意見险绘,話不多說(shuō),直接上:
第一步:
①.寫一個(gè)注冊(cè)頁(yè)面誉碴,這一個(gè)頁(yè)面主要是用戶的輸入宦棺,比較簡(jiǎn)單,代碼如下:
regist.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>注冊(cè)頁(yè)面</title>
</head>
<body>
<form method = "POST" action = "registdo.jsp">
用戶名:<input type = "text" name= "username">
密碼:<input type = "password" name = "userpass">
<input type = "submit" value = "注冊(cè)">
<input type = "reset" value = "重置">
</form>
</body>
</html>
效果如圖:
這里寫圖片描述
②.下面這個(gè)頁(yè)面主要負(fù)責(zé)用來(lái)處理業(yè)務(wù)邏輯以及顯示注冊(cè)的結(jié)果黔帕,代碼如下:
registdo.jsp
<%@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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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("注冊(cè)成功");
out.println("<br><a href = 'login.jsp'>返回登陸</a></br>");
}else{
out.println("注冊(cè)失敗");
out.println("<br><a href = 'regist.jsp'>返回注冊(cè)</a></br>");
}
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
效果如圖:
這里寫圖片描述
①.這一步寫個(gè)登錄頁(yè)面代咸,主要用來(lái)接收用戶輸入的登錄信息:代碼如下:
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>
效果如圖
這里寫圖片描述
②.這一步是需要寫登錄的邏輯及驗(yàn)證,并顯示登錄的結(jié)果成黄,代碼如下
logindo.jsp
<%@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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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("<br><a href = 'login.jsp'>重新登錄</a></br>");
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.closeAll();
}
%>
</body>
</html>
效果如圖
這里寫圖片描述
前臺(tái)的注冊(cè)及登錄頁(yè)面到此已完成,下面我們接著做后臺(tái)的數(shù)據(jù)
第二步
①.我們需要User類封裝用戶的信息思瘟,代碼如下:
User.Java
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;
}
}
②.有了User類荸百,就需要進(jìn)行數(shù)據(jù)庫(kù)的訪問,這時(shí)候我們需要一個(gè)連接數(shù)據(jù)庫(kù)的基礎(chǔ)類滨攻,所有與連接數(shù)據(jù)庫(kù)的類都要 繼承于此够话,代碼如下:
DBOper.Java
public Connection conn = null;
public PreparedStatement ps = null;
public ResultSet rs = null;
/**
* 連接數(shù)據(jù)庫(kù)
* @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;
}
/**
* 關(guān)閉數(shù)據(jù)庫(kù)連接
*/
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();
}
}
}
}
/**
* 執(zhí)行SQL語(yǔ)句,只進(jìn)行查詢光绕,不進(jìn)行增刪改
* @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;
}
/**
* 執(zhí)行sql語(yǔ)句女嘲,進(jìn)行增刪改筛欢,不進(jìn)行查詢
* @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;
}
}
③.寫完了數(shù)據(jù)庫(kù)操作對(duì)象類蛾号,就要寫用戶 操作對(duì)象類代碼如下,
UserDao.Java
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<User> getAllUser(){
List<User> userList = new ArrayList<User>();
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;
}
/**
* 根據(jù)用戶名獲取用戶信息
* @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;
}
}
④.我們的留言Message類摸屠,代碼如下:
Message.Java
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;
}
}
⑤.留言數(shù)據(jù)操作對(duì)象MsgDao類景埃,代碼如下:
MsgDao.Java
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<Message> getAllMsg(){
List<Message> msglist = new ArrayList<Message>();
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;
}
/**
* 根據(jù)消息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;
}
}
⑥.回復(fù)Reply類媒至,代碼如下:
Reply.Java
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;
}
}
⑦.Reply的數(shù)據(jù)操作對(duì)象類,代碼如下:
ReplyDao.Java
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<Reply> getAllReply(){
Reply reply = null;
List<Reply>replylist = new ArrayList<Reply>();
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;
}
/**
* 添加回復(fù)
* @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;
}
}
第二步到此結(jié)束谷徙,這一步主要是對(duì)數(shù)據(jù)的封裝和連接數(shù)據(jù)庫(kù)拒啰。
第三步:
①.顯示用戶列表
userlist.jsp
<%@ 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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<User> userlist = new ArrayList<User>();
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">序號(hào)</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>
效果如圖
這里寫圖片描述
②.在用戶列表里我們點(diǎn)擊“留言”就可以對(duì)用戶留言,頁(yè)面將跳轉(zhuǎn)到留言頁(yè)面完慧,如下:
addmsg.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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"/>
留言內(nèi)容:<input type = "text" name = "context"/>
<input type = "submit" value = "留言"/>
<input type = "reset" value = "重置"/>
</form>
</body>
</html>
效果如圖:
這里寫圖片描述
③.留言頁(yè)面的數(shù)據(jù)處理谋旦,代碼如下:
addOK.jsp
<%@ 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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("<br><a href = 'showmsg.jsp'>查看留言</a></br>");
}else{
out.println("留言失斍帷册着!");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
%>
</body>
</html>
效果如圖:
這里寫圖片描述
④.點(diǎn)擊“查看留言”,將跳轉(zhuǎn)到showmsg.jsp頁(yè)面脾歧,代碼如下:
showmsg.jsp
<%@ 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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<Message> msglist = new ArrayList<Message>();
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">留言序號(hào)</span></th>
<th width="10%"height="30"><span style="font-weight: 10">留言ID</span></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">留言內(nèi)容</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}">回復(fù)</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>
效果如圖:
這里寫圖片描述
⑤.點(diǎn)擊頁(yè)面中的“回復(fù)”將跳轉(zhuǎn)到reply.jsp甲捏,代碼如下:
reply.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="com.szx.jnmc.Reply" %>
<%@page import="com.szx.jnmc.ReplyDao" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>回復(fù)頁(yè)面</title>
</head>
<body>
<%
String messageid = request.getParameter("messageid");
if(messageid != null || messageid.equals("")){
pageContext.setAttribute("messageid", messageid);
}
%>
<form method = "POST" action = "replydo.jsp">
內(nèi)容:<input type = "text" name = "context">
<input type = "hidden" name = "id" value="${messageid }">
<input type = "submit" value = "提交">
<input type = "reset" value ="重置">
</form>
</body>
</html>
效果如圖:
這里寫圖片描述
⑥.輸入回復(fù)數(shù)據(jù),點(diǎn)擊提交轉(zhuǎn)到回復(fù)結(jié)果replydo.jsp頁(yè)面鞭执,代碼如下:
replydo.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="com.szx.jnmc.Reply" %>
<%@page import="com.szx.jnmc.ReplyDao" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>回復(fù)留言</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("回復(fù)成功司顿!");
out.println("<br><a href = 'showmsg.jsp'>返回留言列表</a></br>");
}else{
out.println("回復(fù)失敗兄纺!");
out.println("<br><a href = 'replydo.jsp'>重新回復(fù)</a></br>");
}
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
效果如圖:
這里寫圖片描述
⑦.點(diǎn)擊返回留言列表大溜,轉(zhuǎn)到留言列表showmsg.jsp頁(yè)面,進(jìn)行編輯估脆,代碼如下:
editmsg.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import = "com.szx.jnmc.MsgDao" %>
<%@page import = "com.szx.jnmc.Message" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>內(nèi)容: <input type = "text" name = "context" value ="${msg.context}"></td>
</tr>
<tr>
<%-- <input type = "text" name = "lefttime" value ="${msg.lefttime}"> --%>
<td>日期: <span>${msg.lefttime }</span></td>
</tr>
<%-- <input type = "text" name = "wholeft" value ="${msg.wholeft}"readonly="readonly"> --%>
<tr>
<td>留言人:<span>${msg.wholeft}</span></td>
</tr>
<%-- <input type ="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>
效果如圖:
這里寫圖片描述
⑧.點(diǎn)擊提交钦奋,跳轉(zhuǎn)到 編輯結(jié)果頁(yè)面editmsgdo.jsp,代碼如下:
editmsgdo.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import = "com.szx.jnmc.MsgDao" %>
<%@page import = "com.szx.jnmc.Message" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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("<br><a href = 'showmsg.jsp'>返回留言列表</a></br>");
}else{
out.println("修改失敗");
out.println("<br><a href = 'editmsgdo.jsp'>重新修改</a></br>");
}
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
效果如圖:
這里寫圖片描述
⑨.點(diǎn)擊“返回留言列表 ”回到留言列表頁(yè)面,點(diǎn)擊刪除則直接刪除留言锨苏,代碼如下:
delmsg.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import = "com.szx.jnmc.MsgDao" %>
<%@page import = "com.szx.jnmc.Message" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>
數(shù)據(jù)表的創(chuàng)建SQL語(yǔ)句:
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;
附上工程的結(jié)構(gòu)圖:
這里寫圖片描述