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

news/2025/2/12 0:55:20/

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 {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected 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: AdministratorDate: 2023/12/23Time: 11:43To 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>


http://www.ppmy.cn/news/1294984.html

相关文章

SSH端口转发

SSH&#xff1a;Secure Shell&#xff0c;是一种在不安全网络中提供安全连接的协议&#xff0c;通过加密隧道&#xff0c;提供了对远程计算机的访问。 可不仅是一个远程工具&#xff0c;还有多种功能&#xff0c;比如说端口转发&#xff0c;主要分为以下三种类型&#xff1a; 本…

【C++】带你学会使用C++线程库thread、原子库atomic、互斥量库mutex、条件变量库condition_variable

C线程相关知识讲解 前言正式开始C官方为啥要提供线程库thread构造函数代码演示this_threadget_id()yield()sleep_until和sleep_for mutex构造函数lock和unlock上锁全局锁局部锁lambda表达式 try_lock 其他锁时间锁递归版本专用锁recursive_mutex 锁的异常处理lock_guardunique_…

typescript递归处理

typescript是一种类型强约束的语言&#xff0c;一般来讲定义类型时都要明确指定类型的数据结构。而如果数据结构中涉及到不知道基层嵌套的递归时&#xff0c;就会有一些麻烦。 在 https://stackoverflow.com/questions/51657815/recursive-array-type-typescript 有一个回答…

Ubuntu上wps调用zotero的方法

本人电脑Ubuntu22.04 克隆这位大佬的项目&#xff0c;并转到合适的目录下https://github.com/tankwyn/WPS-Zotero 输入命令 unzip WPS-Zotero-main.zip # 解压文件 cd WPS-Zotero-main # 进入目录 ./install.py # 安装文件如果上面的流程全部正常&#xff0c;恭喜成功 如果出现…

11.20 校招 实习 内推 面经

绿*泡*泡&#xff1a; neituijunsir 交流裙 &#xff0c;内推/实习/校招汇总表格&#xff0c;简历修改咨询 1、校招&#xff5c;赛力斯集团2024届校园招聘热招职位盘点 校招&#xff5c;赛力斯集团2024届校园招聘热招职位盘点 2、校招&#xff5c;天津能源投资集团有限公司…

CHS_02.1.1.2+操作系统的特征

CHS_02.1.1.2操作系统的特征 操作系统的四个特征并发这个特征为什么并发性对于操作系统来说是一个很重要的基本特性资源共享虚拟异步性 各位同学 大家好 在这个小节当中 我们会学习 操作系统的四个特征 操作系统有并发 共享 虚拟和异部这四个基本的特征 其中 并发和共享是两个…

【Flink精讲】双流Join之Regular Join(即普通Join)

Regular Join 普通Join 通过条件关联两条实时数据流&#xff1a;动态表Join动态表支持Inner Join、Left Join、Right Join、Full Join。 1. Inner Join(Join)&#xff1a;只有两边数据流都关联上才输出[L,R] 2. Left Join(Left Outer Join)&#xff1a;只要左流有数据即输出[…

SpringBoot: 通过MyBatis访问ClickHouse

一、ClickHouse中建表&#xff0c;添加数据 二、SpringBoot项目添加mybatis、clickhouse、druid相关依赖 <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.6</version></dependency>…