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博客
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 {
@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");
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);
// }
}
}
}
<%@ 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>
<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>