简单新闻管理系统(jsp)

简单新闻管理系统(jsp)

一、构思

 主页中有添加,删除,修改新闻功能,跟数据库连接,分别跳转处理 

Navicat for mysql:
这里写图片描述

列表如图所示:
这里写图片描述

二、开始编写

新闻主页:

这里写图片描述

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <html> <head> <title>新闻主页</title> </head> <body> <h1 align="center">新闻主页</h1> <div> <table align="center" border="1"> <tr bgcolor="#7fffd4" align="center"> <th>新闻条目</th> <th>新闻标题</th> <th>发布日期</th> <th>修改</th> <th>删除</th> </tr> <% NewsDao newsDao = new NewsDao(); List<News> list = new ArrayList<News>(); list = newsDao.listAll(); int num = 0; %> <% for(int i=0;i<list.size()&&list!=null;i++){ %> <tr> <td align="center"><%=++num%></td> <td align="center"><a href="/select.jsp?id=<%=list.get(i).getId()%>"><%=list.get(i).getNewsTitle()%></a></td> <td align="center"><%=list.get(i).getDate()%></td> <td align="center"><a href="/update.jsp?id=<%=list.get(i).getId()%>">修改</a></td> <td align="center"><a href="/delete.jsp?id=<%=list.get(i).getId()%>">删除</a></td> </tr> <%}%> </table> <p align="center"><a href="/add.jsp">添加</a></p> </div> </body> </html>

浏览页面

这里写图片描述

select.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>浏览页面</title> </head> <body> <% News news = new News(); NewsDao newsDao = new NewsDao(); int id = Integer.parseInt(request.getParameter("id")); news = newsDao.selectNewsById(id); %> <h1 align="center"><%=news.getNewsTitle()%></h1> <form action="index.jsp" method="post"> <table align="center"> <tr><td>标题:<%=news.getAuthor()%></td></tr><br/> <tr><td>日期:<%=news.getDate()%></td></tr><br/> <tr><td>内容:<%=news.getContent()%></td></tr><br/> <tr><td><p align="center"> <input type="submit" value="返回"></p></td></tr> </table> </form> </body> </html> 

修改页面//添加页面

这里写图片描述

add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加页面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); %> <form action="adds.jsp?id=<%=request.getParameter("id")%>" method="post"> <p align="center">标题:<input type="text" name="newsTitle"></p> <p align="center">作者:<input type="text" name="author"></p> <p align="center">日期:<input type="text" name="date"></p> <p align="center">内容:<textarea style="width: 300px;length:500;" name="content"><%=request.getAttribute("content")%></textarea></p><br/> <p align="center"><input type="submit" value="添加"></p> </form> </body> </html>

adds.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>处理页面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); News news = new News(); NewsDao newsDao = new NewsDao(); news.setNewsTitle(request.getParameter("newsTitle")); news.setAuthor(request.getParameter("author")); news.setDate(request.getParameter("date")); news.setContent(request.getParameter("content")); boolean flag = newsDao.insertNews(news); if(flag){ request.getRequestDispatcher("/index.jsp").forward(request,response); } %> </body> </html>

edit.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改页面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); News news = new News(); NewsDao newsDao = new NewsDao(); int id = Integer.parseInt(request.getParameter("id")); news = newsDao.selectNewsById(id); %> <form action="updates.jsp?id=<%=request.getParameter("id")%>" method="post"> <table align="center"> <p align="center">标题:<input type="text" value="<%=news.getNewsTitle()%>" name="newsTitle"></p><br/> <p align="center">作者:<input type="text" value="<%=news.getAuthor()%>" name="author"></p><br/> <p align="center">日期:<input type="text" value="<%=news.getDate()%>" name="date"></p><br/> <p align="center">内容:<textarea style="width: 300px;length:500;" name="content"><%=news.getContent()%></textarea></p><br/> <p align="center"><input type="submit" value="修改"></p> </table> </form> </body> </html>

edits.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>处理页面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); NewsDao newsDao = new NewsDao(); News news = new News(); int id = Integer.parseInt(request.getParameter("id")); news.setId(id); news.setNewsTitle(request.getParameter("newsTitle")); news.setAuthor(request.getParameter("author")); news.setDate(request.getParameter("date")); news.setContent(request.getParameter("content")); boolean flag = newsDao.updateNews(news,id); if(flag){ request.getRequestDispatcher("/select.jsp").forward(request,response); } %> </body> </html>

delete.jsp(删除页面)

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>删除页面</title> </head> <body> <% NewsDao newsDao = new NewsDao(); int id =Integer.parseInt( request.getParameter("id")); boolean flag = newsDao.deleteNewsById(id); if(flag){ request.getRequestDispatcher("/index.jsp").forward(request,response); } %> </form> </body> </html>
这便是我所写的简单新闻管理系统,还有很多不足,欢迎广大同僚来踩踩 若是有什么问题,希望能给我提提意见^_^ 

原文链接:https://blog.csdn.net/houst388/article/details/70666563?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165277607816781818782534%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165277607816781818782534&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-27-70666563-null-null.nonecase&utm_term=%E6%96%B0%E9%97%BB

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
文明发言,共建和谐米科社区
提交
头像

昵称

取消
昵称表情图片