当前位置: 首页 > news >正文

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

NewsServlet.java

package comm.ch11_pra.servlet.news;import comm.ch11_pra.entity.News;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;@WebServlet("/newsServlet")
public class NewsServlet 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");st = connection.createStatement();String search_title = request.getParameter("search_title");String search_author = request.getParameter("search_author");String search_content = request.getParameter("search_content");String sql ="select*from news where 1=1";
if(search_title!=null && !("".equals(search_title))){sql=sql+" and title like '%"+search_title+"%'";}
if(search_author!=null && !("".equals(search_author))){sql=sql+" and author like '%"+search_author+"%'";}
if(search_content!=null && !("".equals(search_content))){sql=sql+" and content like '%"+search_content+"%'";}System.out.println(sql);/*  String sql = "select * from news";*/rs = st.executeQuery(sql);ArrayList<News> news_list = new ArrayList<News>();while(rs.next()){News news = new News();news.setId(rs.getInt("id"));news.setTitle(rs.getString("title"));news.setAuthor(rs.getString("author"));news.setContent(rs.getString("content"));news.setDate(rs.getString("date"));news_list.add(news);}request.setAttribute("news_list", news_list);request.getRequestDispatcher("news.jsp").forward(request, response);} 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: 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>
<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><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("</tr>");}}%>
</table>
</body>
</html>

相关文章:

  • Linux离线安装MySQL(rpm)
  • java基于SSM的游戏商城的设计与实现论文
  • 总结ECMAScript和JavaScript的区别
  • sublim安装Autoprefixer插件
  • 滑动窗口协议仿真(2024)
  • GoldenGate工作原理及应用场景
  • 面试算法91:粉刷房子
  • CentOS使用docker安装mysql并使用navicat 远程链接
  • scroll、offset、client —— JS三大家族
  • 生成式AI:革新软件开发流程与工具的未来趋势
  • MySQL中的开发基于Python的SQL工具类操作数据库简单示例
  • 【鸿蒙4.0】安装DevEcoStudio
  • vue-springboot基于Java的人力资源管理系统 企业员工考勤打卡请假系统的设计与实现
  • 向爬虫而生---Redis 基石篇2 <拓展Hash>
  • 基于springboot的靓车汽车销售网站
  • 30天自制操作系统-2
  • 77. Combinations
  • centos安装java运行环境jdk+tomcat
  •  D - 粉碎叛乱F - 其他起义
  • gulp 教程
  • HTTP--网络协议分层,http历史(二)
  • Java Agent 学习笔记
  • Java 内存分配及垃圾回收机制初探
  • JavaScript 基本功--面试宝典
  • js面向对象
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Mac转Windows的拯救指南
  • MySQL的数据类型
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • Python socket服务器端、客户端传送信息
  • python 学习笔记 - Queue Pipes,进程间通讯
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 学习笔记TF060:图像语音结合,看图说话
  • 怎么将电脑中的声音录制成WAV格式
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • #pragma once与条件编译
  • (007)XHTML文档之标题——h1~h6
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (C++17) optional的使用
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)Linux下编译安装log4cxx
  • (转)程序员疫苗:代码注入
  • .net core 6 redis操作类
  • .net MySql
  • .net 简单实现MD5
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .net专家(张羿专栏)