基于javaweb+JSP+Servlet简单留言板

本文介绍了如何使用Java、JSP和Servlet开发一个简易的留言板系统,包括留言发表、查看、回复功能,适合课程设计或项目实践。通过Eclipse/IDEA等工具配置,配合MySQL数据库,实现用户交互与数据持久化。
摘要由CSDN通过智能技术生成

基于javaweb+JSP+Servlet简单留言板

开发工具:eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等


<!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=UTF-8">
    <title>查看留言</title>
</head>
<body>
<table border="1" width="750px" align="center">
    <tr>
        <td colspan="2" align="center" bgcolor="yellow"><h2>留言板</h2></td>
<%--        <td>
            <form action="MessageServlet">
                <input type="hidden" name="type" value="query">
                <input type="submit" value="查看">
            </form>
        </td>--%>

                response.sendRedirect("lookreply.jsp");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
package com.ser;

import com.bean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserServlet extends HttpServlet {
    <tr>
        <td><%=message.getId()%></td>
        <td><%=message.getTitle()%></td>
        <td><%=message.getUsername()%></td>
        <td><%=message.getContent()%></td>
        <td><%=message.getTheme()%></td>
        <td><%=message.getTime()%></td>
    </tr>
    <tr>
        <td><a href="show.jsp" >点击返回</a></td>
    </tr>
</table>

<br><br>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" import="java.util.*,com.bean.Message"%>
<%@ page import="com.bean.User" %>
<%@ page import="com.bean.Reply" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

public class RegServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        User user=new User();
    <script type="text/javascript">

        // 检查注册时用户输入的注册信息是否正确
        function checkLogin() {
            var username = document.getElementById("text_username_login");
            if (username.value == "") {
                alert(new String("必须输入用户名!"));
                text_username_login.focus();
                return;
            }
            var password = document.getElementById("text_password_login");
            if (password.value == "") {
                alert("密码必须输入!");
                text_password_login.focus();
                return;
            }


            login_form.submit();
        }
    </script>
        <td align="center"><a href="MessageServlet?type=delete&id=<%=messages.get(i).getId() %>">删除</a></td>
        <% }%>
    </tr>
    <% }
    }
    %>

    <tr>
        <td colspan="3" align="center"><a href="message.jsp"><h3>添加新留言</h3></a></td>
    </tr>
</table>
<br><br>

</body>
        <td><%=message.getContent()%></td>
        <td><%=message.getTheme()%></td>
        <td><%=message.getTime()%></td>
    </tr>
    <tr>
        <td><a href="show.jsp" >点击返回</a></td>
    </tr>
</table>

<br><br>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
        return reContent;
    }
    public void setReContent(String reContent) {
        this.reContent = reContent;
    }
    //添加一条
    public void add(Reply reply) throws SQLException{
        //获取数据库连接
        Connection conn= Util.getConnection();
        PreparedStatement pre= conn.prepareStatement("INSERT INTO reply(mid,reusername,recontent) VALUES (?,?,?)");
        pre.setInt(1, reply.getMid());
        pre.setString(2,reply.getReUsername());
        pre.setString(3,reply.getReContent());
        pre.execute();
        System.out.println("插入一条记录");
        conn.close();
    }
    //查询所有
    public ArrayList<Reply> queryAll(int mid) throws SQLException {
        ArrayList<Reply> replies=new ArrayList<Reply>();
        ResultSet rs=null;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public String getTime() {
        return time;
    }
				
				messages=message.queryAll();
				
				request.getSession().setAttribute("messages", messages);
				
				response.sendRedirect("show.jsp");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(type.equals("delete")){
			User info = (User) request.getSession().getAttribute("info");
			if(info==null){
				request.getSession().setAttribute("info","删除留言请登录");
				response.sendRedirect("fail.jsp");
				return;
			}else{
				if(!info.getGrade().equals("admin")){
					request.getSession().setAttribute("info","非管理员不可删除");
					response.sendRedirect("fail.jsp");
					return;
				}
			}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>My JSP 'userlogin.jsp' starting page</title>

运行环境

Java≥8、Tomcat≥8.0、MySQL≥5.7

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

技术框架

JSP Servlet MySQL JDBC Tomcat CSS JavaScript

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

发表留言,查看留言,回复留言,查看回复,删除留言(管理员)

20220314115024

20220314115038

20220314115049

20220314115126

20220314115147

20220314115228

20220314124832


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值