21xrx.com
2024-11-05 16:22:57 Tuesday
登录
文章检索 我的文章 写文章
实现一个简单的留言板功能 包含留言的发布、查看、删除和修改功能
2023-06-15 06:46:29 深夜i     --     --
JavaWeb编程 留言板 发布 查看 删除 修改

简单的JavaWeb课程设计题目及代码

在学习JavaWeb编程的过程中,设计一个简单的课程设计题目不仅能够帮助学习者巩固所学知识,还能够锻炼编程能力。下面就是一个简单的JavaWeb课程设计题目及对应的代码。

代码实现:

1.前端页面(略)

2.后端实现

(1)实现留言的发布功能

  public class MessageServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      request.setCharacterEncoding("UTF-8");

      String content = request.getParameter("content");

      Date date = new Date();

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      String time = sdf.format(date);

      MessageDao messageDao = new MessageDao();

      messageDao.saveMessage(content, time);

      response.sendRedirect(request.getContextPath() + "/message");

    }

  }

(2)实现留言的查看功能

  public class MessageServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      MessageDao messageDao = new MessageDao();

      List messageList = messageDao.getAllMessage();

      request.setAttribute("messageList",messageList);

      request.getRequestDispatcher("message.jsp").forward(request,response);

    }

  }

(3)实现留言的删除功能

  public class MessageServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      int id = Integer.parseInt(request.getParameter("id"));

      MessageDao messageDao = new MessageDao();

      messageDao.deleteMessage(id);

      response.sendRedirect(request.getContextPath() + "/message");

    }

  }

(4)实现留言的修改功能

  public class MessageServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      int id = Integer.parseInt(request.getParameter("id"));

      MessageDao messageDao = new MessageDao();

      Message message = messageDao.getMessageById(id);

      request.setAttribute("message",message);

      request.getRequestDispatcher("edit.jsp").forward(request,response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      request.setCharacterEncoding("UTF-8");

      String content = request.getParameter("content");

      int id = Integer.parseInt(request.getParameter("id"));

      MessageDao messageDao = new MessageDao();

      messageDao.updateMessage(content,id);

      response.sendRedirect(request.getContextPath() + "/message");

    }

  }

文章 .

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复