作者主页:源码空间站2022
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
本项目分为前后台;
前台主要功能为:
首页、娱乐新闻、经济新闻、文化新闻、小道新闻、用户评价等;
后台主要功能为:
可以对新闻类别,新闻,新闻评论,友情链接等进行数据管理
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.数据库:MySql 5.7版本;
6.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
技术栈
1. 后端:servlet
2. 前端:JSP+css+javascript
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中news.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,在浏览器中输入http://localhost:8080/News
后台地址:http://localhost:8080/News/user?action=login
用户名:admin,密码:123456
运行截图
前台界面
后台界面
相关代码
CommentServlet
package com.java1234.web;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import com.java1234.dao.CommentDao;
import com.java1234.model.Comment;
import com.java1234.model.PageBean;
import com.java1234.util.DbUtil;
import com.java1234.util.NavUtil;
import com.java1234.util.PageUtil;
import com.java1234.util.PropertiesUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
public class CommentServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
CommentDao commentDao=new CommentDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String action=request.getParameter("action");
if("save".equals(action)){
commentSave(request,response);
}else if("backList".equals(action)){
commentBackList(request,response);
}else if("delete".equals(action)){
commentDelete(request,response);
}
}
private void commentSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsId=request.getParameter("newsId");
String content=request.getParameter("content");
String userIP=request.getRemoteAddr();
Comment comment=new Comment(Integer.parseInt(newsId), content, userIP);
Connection con=null;
try{
con=dbUtil.getCon();
commentDao.commentAdd(con, comment);
request.getRequestDispatcher("news?action=show&newsId="+newsId).forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void commentBackList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s_bCommentDate=request.getParameter("s_bCommentDate");
String s_aCommentDate=request.getParameter("s_aCommentDate");
String page=request.getParameter("page");
HttpSession session=request.getSession();
if(StringUtil.isEmpty(page)){
page="1";
session.setAttribute("s_bCommentDate", s_bCommentDate);
session.setAttribute("s_aCommentDate", s_aCommentDate);
}else{
s_bCommentDate=(String) session.getAttribute("s_bCommentDate");
s_aCommentDate=(String) session.getAttribute("s_aCommentDate");
}
Connection con=null;
try{
con=dbUtil.getCon();
int total=commentDao.commentCount(con, new Comment(), s_bCommentDate, s_aCommentDate);
String pageCode=PageUtil.getPagation(request.getContextPath()+"/comment?action=backList", total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("backPageSize")));
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("backPageSize")));
List<Comment> commentBackList=commentDao.commentList(con, new Comment(),pageBean,s_bCommentDate,s_aCommentDate);
request.setAttribute("pageCode", pageCode);
request.setAttribute("commentBackList", commentBackList);
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("新闻评论管理", "新闻评论维护"));
request.setAttribute("mainPage", "/background/comment/commentList.jsp");
request.getRequestDispatcher("/background/mainTemp.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void commentDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String commentIds=request.getParameter("commentIds");
Connection con=null;
try{
con=dbUtil.getCon();
JSONObject result=new JSONObject();
int delNums=commentDao.commentDelete(con, commentIds);
if(delNums>0){
result.put("success", true);
result.put("delNums", delNums);
}else{
result.put("errorMsg", "删除失败");
}
ResponseUtil.write(result, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
IndexServlet
package com.java1234.web;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.java1234.dao.LinkDao;
import com.java1234.dao.NewsDao;
import com.java1234.dao.NewsTypeDao;
import com.java1234.model.Link;
import com.java1234.model.News;
import com.java1234.model.NewsType;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;
public class IndexServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
NewsDao newsDao=new NewsDao();
NewsTypeDao newsTypeDao=new NewsTypeDao();
LinkDao linkDao=new LinkDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Connection con=null;
try{
con=dbUtil.getCon();
List<NewsType> newsTypeList=newsTypeDao.newsTypeList(con);
String sql="select * from t_news where isImage=1 order by publishDate desc limit 0,5";
List<News> imageNewsList=newsDao.newsList(con, sql);
request.setAttribute("imageNewsList", imageNewsList);
sql="select * from t_news where isHead=1 order by publishDate desc limit 0,1 ";
List<News> headNewsList=newsDao.newsList(con, sql);
News headNews=headNewsList.get(0);
headNews.setContent(StringUtil.Html2Text(headNews.getContent()));
request.setAttribute("headNews", headNews);
sql="select * from t_news where isHot=1 order by publishDate desc limit 0,8 ";
List<News> hotSpotNewsList=newsDao.newsList(con, sql);
request.setAttribute("hotSpotNewsList", hotSpotNewsList);
List allIndexNewsList=new ArrayList();
if(newsTypeList!=null && newsTypeList.size()!=0){
for(int i=0;i<newsTypeList.size();i++){
NewsType newsType=newsTypeList.get(i);
sql="select * from t_news,t_newsType where typeId=newsTypeId and typeId="+newsType.getNewsTypeId()+" order by publishDate desc limit 0,8";
List<News> oneSubList=newsDao.newsList(con, sql);
allIndexNewsList.add(oneSubList);
}
}
request.setAttribute("allIndexNewsList", allIndexNewsList);
sql="select * from t_link order by orderNum ";
List<Link> linkList=linkDao.linkList(con, sql);
request.setAttribute("linkList", linkList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
NewsServlet
package com.java1234.web;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.java1234.dao.CommentDao;
import com.java1234.dao.NewsDao;
import com.java1234.dao.NewsTypeDao;
import com.java1234.model.Comment;
import com.java1234.model.News;
import com.java1234.model.NewsType;
import com.java1234.model.PageBean;
import com.java1234.util.DateUtil;
import com.java1234.util.DbUtil;
import com.java1234.util.NavUtil;
import com.java1234.util.PageUtil;
import com.java1234.util.PropertiesUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
public class NewsServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
NewsDao newsDao=new NewsDao();
NewsTypeDao newsTypeDao=new NewsTypeDao();
CommentDao commentDao=new CommentDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String action=request.getParameter("action");
if("list".equals(action)){
this.newsList(request, response);
}else if("show".equals(action)){
this.newsShow(request, response);
}else if("preSave".equals(action)){
this.newsPreSave(request, response);
}else if("save".equals(action)){
this.newsSave(request, response);
}else if("backList".equals(action)){
this.newsBackList(request,response);
}else if("delete".equals(action)){
this.newsDelete(request,response);
}
}
private void newsList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String typeId=request.getParameter("typeId");
String page=request.getParameter("page");
if(StringUtil.isEmpty(page)){
page="1";
}
Connection con=null;
News s_news=new News();
if(StringUtil.isNotEmpty(typeId)){
s_news.setTypeId(Integer.parseInt(typeId));
}
try{
con=dbUtil.getCon();
int total=newsDao.newsCount(con, s_news,null,null);
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));
List<News> newestNewsListWithType=newsDao.newsList(con, s_news, pageBean,null,null);
request.setAttribute("newestNewsListWithType", newestNewsListWithType);
request.setAttribute("navCode", NavUtil.genNewsListNavigation(newsTypeDao.getNewsTypeById(con, typeId).getTypeName(), typeId));
request.setAttribute("pageCode", PageUtil.getUpAndDownPagation(total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")), typeId));
request.setAttribute("mainPage", "news/newsList.jsp");
request.getRequestDispatcher("foreground/newsTemp.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void newsShow(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsId=request.getParameter("newsId");
Connection con=null;
try{
con=dbUtil.getCon();
newsDao.newsClick(con, newsId);
News news=newsDao.getNewsById(con, newsId);
Comment s_comment=new Comment();
s_comment.setNewsId(Integer.parseInt(newsId));
List<Comment> commentList=commentDao.commentList(con, s_comment,null,null,null);
request.setAttribute("commentList", commentList);
request.setAttribute("news", news);
request.setAttribute("pageCode", this.genUpAndDownPageCode(newsDao.getUpAndDownPageId(con, newsId)));
request.setAttribute("navCode", NavUtil.genNewsNavigation(news.getTypeName(), news.getTypeId()+"",news.getTitle()));
request.setAttribute("mainPage", "news/newsShow.jsp");
request.getRequestDispatcher("foreground/newsTemp.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String genUpAndDownPageCode(List<News> upAndDownPage){
News upNews=upAndDownPage.get(0);
News downNews=upAndDownPage.get(1);
StringBuffer pageCode=new StringBuffer();
if(upNews.getNewsId()==-1){
pageCode.append("<p>Previous: none</p>");
}else{
pageCode.append("<p>Previous: <a href='news?action=show&newsId="+upNews.getNewsId()+"'>"+upNews.getTitle()+"</a></p>");
}
if(downNews.getNewsId()==-1){
pageCode.append("<p>Next: none</p>");
}else{
pageCode.append("<p>Next: <a href='news?action=show&newsId="+downNews.getNewsId()+"'>"+downNews.getTitle()+"</a></p>");
}
return pageCode.toString();
}
private void newsPreSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsId=request.getParameter("newsId");
Connection con=null;
try{
con=dbUtil.getCon();
if(StringUtil.isNotEmpty(newsId)){
News news=newsDao.getNewsById(con, newsId);
request.setAttribute("news", news);
}
List<NewsType> newsTypeList=newsTypeDao.newsTypeList(con);
request.setAttribute("newsTypeList", newsTypeList);
if(StringUtil.isNotEmpty(newsId)){
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("ÐÂÎŹÜÀí", "ÐÂÎÅÐÞ¸Ä"));
}else{
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("ÐÂÎŹÜÀí", "ÐÂÎÅÌí¼Ó"));
}
request.setAttribute("mainPage", "/background/news/newsSave.jsp");
request.getRequestDispatcher("/background/mainTemp.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void newsSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
List<FileItem> items=null;
try {
items=upload.parseRequest(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator itr=items.iterator();
News news=new News();
while(itr.hasNext()){
FileItem item=(FileItem) itr.next();
if(item.isFormField()){
String fieldName=item.getFieldName();
if("newsId".equals(fieldName)){
if(StringUtil.isNotEmpty(item.getString("utf-8"))){
news.setNewsId(Integer.parseInt(item.getString("utf-8")));
}
}
if("title".equals(fieldName)){
news.setTitle(item.getString("utf-8"));
}
if("content".equals(fieldName)){
news.setContent(item.getString("utf-8"));
}
if("author".equals(fieldName)){
news.setAuthor(item.getString("utf-8"));
}
if("typeId".equals(fieldName)){
news.setTypeId(Integer.parseInt(item.getString("utf-8")));
}
if("isHead".equals(fieldName)){
news.setIsHead(Integer.parseInt(item.getString("utf-8")));
}
if("isImage".equals(fieldName)){
news.setIsImage(Integer.parseInt(item.getString("utf-8")));
}
if("isHot".equals(fieldName)){
news.setIsHot(Integer.parseInt(item.getString("utf-8")));
}
if("imageName".equals(fieldName)&&news.getImageName()==null){
if(StringUtil.isNotEmpty(item.getString("utf-8"))){
news.setImageName(item.getString("utf-8").split("/")[1]);
}
}
}else if(!"".equals(item.getName())){
try {
String imageName=DateUtil.getCurrentDateStr();
news.setImageName(imageName+"."+item.getName().split("\\.")[1]);
String filePath=PropertiesUtil.getValue("imagePath")+imageName+"."+item.getName().split("\\.")[1];
item.write(new File(filePath));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Connection con=null;
try{
con=dbUtil.getCon();
if(news.getNewsId()!=0){
newsDao.newsUpdate(con, news);
}else{
newsDao.newsAdd(con, news);
}
request.getRequestDispatcher("/news?action=backList").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void newsBackList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s_bPublishDate=request.getParameter("s_bPublishDate");
String s_aPublishDate=request.getParameter("s_aPublishDate");
String s_title=request.getParameter("s_title");
String page=request.getParameter("page");
HttpSession session=request.getSession();
if(StringUtil.isEmpty(page)){
page="1";
session.setAttribute("s_bPublishDate", s_bPublishDate);
session.setAttribute("s_aPublishDate", s_aPublishDate);
session.setAttribute("s_title", s_title);
}else{
s_bPublishDate=(String) session.getAttribute("s_bPublishDate");
s_aPublishDate=(String) session.getAttribute("s_aPublishDate");
s_title=(String) session.getAttribute("s_title");
}
Connection con=null;
News s_news=new News();
if(StringUtil.isNotEmpty(s_title)){
s_news.setTitle(s_title);
}
try{
con=dbUtil.getCon();
int total=newsDao.newsCount(con, s_news, s_bPublishDate, s_aPublishDate);
String pageCode=PageUtil.getPagation(request.getContextPath()+"/news?action=backList", total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("backPageSize")));
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("backPageSize")));
List<News> newsBackList=newsDao.newsList(con, s_news,pageBean,s_bPublishDate,s_aPublishDate);
request.setAttribute("pageCode", pageCode);
request.setAttribute("newsBackList", newsBackList);
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("ÐÂÎŹÜÀí", "ÐÂÎÅά»¤"));
request.setAttribute("mainPage", "/background/news/newsList.jsp");
request.getRequestDispatcher("/background/mainTemp.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void newsDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
String newsId=request.getParameter("newsId");
Connection con=null;
boolean delFlag;
try{
con=dbUtil.getCon();
int delNums=newsDao.newsDelete(con, newsId);
if(delNums==1){
delFlag=true;
}else{
delFlag=false;
}
ResponseUtil.write(delFlag, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
如果也想学习本系统,下面领取。关注并回复:128jsp