简单的在线留言系统

学习Java Web有一段时间了,借此机会和大家分享一个小东西——在线留言系统,虽然low,小喷即可,大喷伤心,还是希望大家的指正和意见,话不多说,直接上:

第一步:

①.写一个注册页面,这一个页面主要是用户的输入,比较简单,代码如下:

regist.jsp

<%@ 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>

效果如图:
简单的在线留言系统_第1张图片

②.下面这个页面主要负责用来处理业务逻辑以及显示注册的结果,代码如下:

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"%>

<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>

效果如图:
简单的在线留言系统_第2张图片

第二步:

①.这一步写个登录页面,主要用来接收用户输入的登录信息:代码如下:

login.jsp

<%@ 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>

效果如图
简单的在线留言系统_第3张图片

②.这一步是需要写登录的逻辑及验证,并显示登录的结果,代码如下

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" %>

<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>

效果如图
简单的在线留言系统_第4张图片

前台的注册及登录页面到此已完成,下面我们接着做后台的数据

第二步

①.我们需要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类,就需要进行数据库的访问,这时候我们需要一个连接数据库的基础类,所有与连接数据库的类都要 继承于此,代码如下:

DBOper.Java

    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;

    }
}

③.写完了数据库操作对象类,就要写用户 操作对象类代码如下,

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 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;       
    }  
}

④.我们的留言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;
    }
}

⑤.留言数据操作对象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 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;       
    }
}

⑥.回复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的数据操作对象类,代码如下:

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 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;
    }
}

第二步到此结束,这一步主要是对数据的封装和连接数据库。

第三步:

①.显示用户列表

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"%>

<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>

效果如图
简单的在线留言系统_第5张图片

②.在用户列表里我们点击“留言”就可以对用户留言,页面将跳转到留言页面,如下:

addmsg.jsp

<%@ 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>

效果如图:
简单的在线留言系统_第6张图片

③.留言页面的数据处理,代码如下:

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" %>

<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>

效果如图:
简单的在线留言系统_第7张图片

④.点击“查看留言”,将跳转到showmsg.jsp页面,代码如下:

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"%>

<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>

效果如图:
简单的在线留言系统_第8张图片

⑤.点击页面中的“回复”将跳转到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" %>

<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>

效果如图:
简单的在线留言系统_第9张图片

⑥.输入回复数据,点击提交转到回复结果replydo.jsp页面,代码如下:

replydo.jsp

<%@ 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>

效果如图:
简单的在线留言系统_第10张图片

⑦.点击返回留言列表,转到留言列表showmsg.jsp页面,进行编辑,代码如下:

editmsg.jsp


<%@ 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>


效果如图:
简单的在线留言系统_第11张图片

⑧.点击提交,跳转到 编辑结果页面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" %>

<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>

效果如图:
简单的在线留言系统_第12张图片

⑨.点击“返回留言列表 ”回到留言列表页面,点击删除则直接删除留言,代码如下:

delmsg.jsp

<%@ 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;

附上工程的结构图:

简单的在线留言系统_第13张图片

至此我们的主要功能已经完成,页面非常low。不多说,也不早了,困死,该休息一会儿了。

在此附上工程压缩包下载地址:http://download.csdn.net/download/itcreater000/9860689

你可能感兴趣的:(javaweb学习总结)