JavaWeb——新闻管理系统(Jsp+Servlet)之jsp新闻删除

发布时间:2024年01月07日

?

java-ee项目结构设计
1.dao:对数据库的访问,实现了增删改查
2.entity:定义了新闻、评论、用户三个实体,并设置对应实体的属性
3.filter:过滤器,设置字符编码都为utf8,防止乱码出现
4.service:业务逻辑处理
5.servlet:处理页面请求
6.utils:工具类
7.c3p0-config.xml:JDBC配置
JavaWeb新闻管理系统(基础版)-腾讯云开发者社区-腾讯云

https://www.cnblogs.com/luomei/p/13124130.htmlJSP显示新闻

Java Jsp+mysql实现新闻发布管理系统(新闻管理、栏目/评论管理、)_jsp项目案例:新闻发布系统—主题管理及首页新闻显示-CSDN博客

NewsDeleteServlet.java

package comm.ch11_pra.servlet.news;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;

@WebServlet(name = "newsDeleteServlet", value = "/newsDeleteServlet")
public class NewsDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("财院新闻");

        Connection connection = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ch11?characterEncoding=utf-8",
                    "root", "123456");

           String id=request.getParameter("id");
            PreparedStatement ps=connection.prepareStatement("delete  from  news where  id=?");
            ps.setString(1,id);
            ps.executeUpdate();
            response.sendRedirect("newsServlet");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
//            try {
//                connection.close();
//                st.close();
//                rs.close();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
        }
    }
}

news.jsp

<%@ page import="comm.ch11_pra.entity.News" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/12/23
  Time: 11:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<script>
    function addModelShow(){
        document.getElementsByName("add_title")[0].value="";
        document.getElementsByName("add_author")[0].value="";
        document.getElementsByName("add_content")[0].value="";
        var addModel=document.getElementById("addModel");
        addModel.style.display="block";
    }
    function addModelClose(){
        var addModel=document.getElementById("addModel");
        addModel.style.display="none";
    }
    function deleteNews(id){
        if(confirm("确认删除id为"+id+"该条数据吗?")){
    window.location.href="newsDeleteServlet?id="+id;
    }
      }
</script>

<body>
<%
    ArrayList<News> news_list = (ArrayList<News>) request.getAttribute("news_list");
%>
<form action="newsServlet">
    标题:<input type="text" name="search_title">
    作者:<input type="text" name="search_author">
    内容:<input type="text" name="search_content">
    <input type="submit" value="查询">
</form>
<input type="button" value="新增" onclick="addModelShow()">
<table border="1">
    <tr><th>id</th><th>title</th><th>author</th><th>content</th><th>date</th></tr>
    <%
        if(news_list!=null){
            for(News news : news_list){
                out.print("<tr>");
                out.print("<td>" + news.getId() + "</td>");
                out.print("<td>" + news.getTitle() + "</td>");
                out.print("<td>" + news.getAuthor() + "</td>");
                out.print("<td>" + news.getContent() + "</td>");
                out.print("<td>" + news.getDate() + "</td>");
                out.print("<td>" +
                        "<input type='button' value='删除' onclick='deleteNews("+news.getId()+")'>"+
                        "<input type='button' value='修改'>"+
                        "</td>");
                out.print("</tr>");
            }
        }
    %>
</table>
<div id="addModel" style="display: none;position: absolute;top: 40%;left: 45%;border: 2px dashed #f00;padding: 10px">
    <div><span style="margin-left: 70px">新增新闻</span><div style="float: right" onclick="addModelClose()">X</div></div>
    <form action="newsaddServlet">
        标题:<input type="text" name="add_title"><br>
        作者:<input type="text" name="add_author"><br>
        内容:<input type="text" name="add_content"><br>
        <input type="submit" value="确认">
    </form>

</div>
</body>
</html>

文章来源:https://blog.csdn.net/qq_73735007/article/details/135440064
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。