JavaWeb——新闻管理系统(Jsp+Servlet)之jsp新闻修改

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

NewsUpdateServlet.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( value = "/newsUpdateServlet")
public class NewsUpdateServlet 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 update_id= request.getParameter("update_id");
            String update_title = request.getParameter("update_title");
            String update_author=request.getParameter("update_author");
            String update_content=request.getParameter("update_content");
            PreparedStatement ps=connection.prepareStatement("update news set title=?,author=?,content=? ,date=? where id=?");
            ps.setString(1,update_title);
            ps.setString(2,update_author);
            ps.setString(3,update_content);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date(System.currentTimeMillis());
            String currentTime = dateFormat.format(date1);
            ps.setString(4,currentTime);
            ps.setString(5,update_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" %>
<%@ page import="com.fasterxml.jackson.databind.ObjectMapper" %><%--
  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;
    }
      }
     function updateModelShow(news_json){
         document.getElementsByName("update_id")[0].value=news_json.id;
         document.getElementsByName("update_title")[0].value=news_json.title;
         document.getElementsByName("update_author")[0].value=news_json.author;
         document.getElementsByName("update_content")[0].value=news_json.content;
         var updateModel=document.getElementById("updateModel");
         updateModel.style.display="block";
     }
    function updateModelClose(){
        var updateModel=document.getElementById("updateModel");
        updateModel.style.display="none";
    }
</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>");
                ObjectMapper mapper = new ObjectMapper();
                String news_json = mapper.writeValueAsString(news);

                out.print("<td>" +
                        "<input type='button' value='删除' onclick='deleteNews("+news.getId()+")'>"+
                        "<input type='button' value='修改' onclick='updateModelShow("+news_json+")'>"+
                        "</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>
<div id="updateModel" 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="updateModelClose()">X</div></div>
    <form action="newsUpdateServlet">
        <input type="text" name="update_id" style="display: none">
        标题:<input type="text" name="update_title"><br>
        作者:<input type="text" name="update_author"><br>
        内容:<input type="text" name="update_content"><br>
        <input type="submit" value="确认">
    </form>

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

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>comm</groupId>
  <artifactId>ch11_pra</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>ch11_pra</name>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
      <junit.version>5.9.1</junit.version>
      </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.1</version>
        </dependency>

    </dependencies>
</project>

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱吃java的羊儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值