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

JavaWeb——新闻管理系统(Jsp+Servlet)之jsp新闻修改_第1张图片

JavaWeb——新闻管理系统(Jsp+Servlet)之jsp新闻修改_第2张图片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" %>


    Title




<%
    ArrayList news_list = (ArrayList) request.getAttribute("news_list");
%>
标题: 作者: 内容:
<% if(news_list!=null){ for(News news : news_list){ out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); ObjectMapper mapper = new ObjectMapper(); String news_json = mapper.writeValueAsString(news); out.print(""); out.print(""); } } %>
idtitleauthorcontentdate
" + news.getId() + "" + news.getTitle() + "" + news.getAuthor() + "" + news.getContent() + "" + news.getDate() + "" + ""+ ""+ "

 

pom.xml



  4.0.0

  comm
  ch11_pra
  1.0-SNAPSHOT
  ch11_pra
  war

  
    UTF-8
    1.8
    1.8
      5.9.1
      

    
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
            javax.servlet.jsp
            jsp-api
            2.2
        
        
            mysql
            mysql-connector-java
            8.0.33
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.15.1
        

    

你可能感兴趣的:(课堂,java,servlet,开发语言)