jsp+servlet+jdbc旅游网站实现添加评论功能

留言功能界面

1、页面-添加留言

<form id="msg-form" method="post" action="${pageContext.request.contextPath}/msgAdd.action">
                        <div class="col-md-12">
                        <div class="form-group col-md-9 col-md-offset-1">
                            <label for="msg">Your message here:</label>
                            <textarea id="msg_text" class="form-control" rows="3" name="msg_text"  placeholder="Enter Your message:"  required></textarea>
                        </div>
                        <div class="form-group col-md-9 col-md-offset-1 text-center">
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </div>
                </form>

2、点击提交按钮,添加留言,保存留言信息到数据库


    a、下面是servlet的代码,处理添加留言的请求

@WebServlet(name = "MsgAddServlet",value = "/msgAdd.action")
public class MsgAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String msg_text = request.getParameter("msg_text");

        MsgService msgService = new MsgServiceImpl();
        UserService userService = new UserServiceImpl();

        //获取当前登录的用户信息
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");

        Cookie cookie = CookieUtil.getCookieByName(request, "front_name_pass");
        System.out.println("cookie:"+cookie);

        if(user == null  ){
            Boolean flg = false;
            if(cookie != null) {
                //有保存当前项目学号和密码的cookie
                String namePass = URLDecoder.decode(cookie.getValue(),"UTF-8");

                String[]  namePass2 =namePass.split("#");
                //获取保存在cookie中的用户名
                User user2 = userService.findByName(namePass2[0]);

                if (user2 != null && user2.getPassword().equals(namePass2[1]) && user2.getGroup_id().equals(1) && user2.getDisabled().equals(0)){
                    //用户正常使用,cookie中用户信息有效,登录成功
                    request.getSession().setAttribute("user",user2);
                    //保存留言到数据库
                    Msg msg = new Msg(msg_text,user2.getUser_id());
                    msgService.save(msg);

                    response.sendRedirect(request.getServletContext().getContextPath()+"/msg.action");
                }else{
                    //未登录,要求先登录才能留言,跳转到登录界面
                    request.setAttribute("msg","请先登录,在留言!");
                    request.getRequestDispatcher("/login.jsp").forward(request,response);
                }
            }else{
                //未登录,要求先登录才能留言,跳转到登录界面
                request.setAttribute("msg","请先登录,在留言!");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }

        }else {
            //保存留言到数据库
            Msg msg = new Msg(msg_text,user.getUser_id());
            msgService.save(msg);

            response.sendRedirect(request.getServletContext().getContextPath()+"/msg.action");
        }
    }

}

        b、servlet调用service中的方法

public class MsgServiceImpl implements MsgService {
    MsgDao msgDao = new MsgDaoImpl();
    @Override
    public void save(Msg msg) {
        msgDao.save(msg);
    }

    @Override
    public List<Msg> findAll() {
        return msgDao.findAll();
    }
}

        c、service调用dao层代码

 @Override
    public void save(Msg msg) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //①获取连接
            connection = ConnectionFactory.getConnection();

            //②准备sql语句
            String  sql ="INSERT INTO t_msg(msg_text,msg_time,user_id)   VALUE(?,NOW(),?)";
            //③ 获取集装箱
            preparedStatement =connection.prepareStatement(sql);
            preparedStatement.setString(1,msg.getMsg_text());
            preparedStatement.setInt(2,msg.getUser_id());

            //④执行查询,将查询出来的结果封装在resultSet,注意他这个地方会将表头信息查询出来
            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //一般用于善后清理工作,比如关闭连接   集装箱  结果集
            ConnectionFactory.close(connection,preparedStatement,resultSet);
        }
    }

2、页面-查询留言

<c:forEach items="${msgs}" var="msg">
                <p class="story_t"> <font color="337ab7"> ${msg.user.username}</font>:${msg.msg_text}</p>
                <p class="story_time">
                        ${msg.msg_time}
                    <button class="btn btn-default" onclick="onValue( ${msg.msg_id})" data-target="#myModal" data-toggle="modal"
                            value="回复" style="background: url(${pageContext.request.contextPath}/image/reply.png) no-repeat;width: 30px;height: 15px;border: none;">
                    </button>
                </p>

                <c:forEach items="${msg.replies}" var="replie">
                 <p class="story_hf">@${replie.user.username}:${replie.reply_context}</p>
                </c:forEach>
            </c:forEach>

3、页面-点击回复出现回复的模态框

<!--fade 淡入淡出  回复留言的模态框-->
        <div class="modal fade"  id="myModal" aria-labelledby="myModallabel" aria-hidden="true" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" type="button" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h5 class="modal-title" id="myModallabel">回复信息</h5>
                    </div>
                    <form id="formData" class="form-horizontal" action="${pageContext.request.contextPath}/msgReply.action" method="get">
                        <div class="modal-body">
                            <div class="form-group">
                                <label class="control-label col-lg-2">留言:</label>
                                <div class="col-lg-9">
                                    <input type="hidden"  id="msg_id"  name="msg_id"  >
                                    <textarea  name="reply_context"  id="reply_context" placeholder="请输入回复信息" class="form-control" rows="5" required></textarea> </br>
                                </div>
                            </div>
                        </div>

                        <div class="modal-footer">
                            <button class="btn btn-default" type="button" data-dismiss="modal">close</button>
                            <button class="btn btn-success" type="submit" >提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT实战营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值