作者主页:源码空间站2022
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
本项目主要功能有:
写日记 查看日记 日记删除 日记修改 日记类别添加 日记类别修改 日记类别删除 个人信息查看 个人信息修改 分页功能
用户名:xyj 密码:123456
环境需要
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+bootstrap+jQuery
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中db.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,在浏览器中输入http://localhost:8080/ 登录
运行截图
相关代码
NewsTypeServlet
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 net.sf.json.JSONObject;
import com.java1234.dao.NewsDao;
import com.java1234.dao.NewsTypeDao;
import com.java1234.model.Link;
import com.java1234.model.NewsType;
import com.java1234.util.DbUtil;
import com.java1234.util.NavUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
public class NewsTypeServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
NewsDao newsDao=new NewsDao();
NewsTypeDao newsTypeDao=new NewsTypeDao();
@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("preSave".equals(action)){
this.newsTypePreSave(request, response);
}else if("save".equals(action)){
this.newsTypeSave(request, response);
}else if("backList".equals(action)){
this.newsTypeBackList(request, response);
}else if("delete".equals(action)){
this.newsTypeDelete(request, response);
}
}
private void newsTypePreSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsTypeId=request.getParameter("newsTypeId");
Connection con=null;
try{
con=dbUtil.getCon();
if(StringUtil.isNotEmpty(newsTypeId)){
NewsType newsType=newsTypeDao.getNewsTypeById(con, newsTypeId);
request.setAttribute("newsType", newsType);
}
if(StringUtil.isNotEmpty(newsTypeId)){
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("新闻类别管理", "新闻类别修改"));
}else{
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("新闻类别管理", "新闻类别添加"));
}
request.setAttribute("mainPage", "/background/newsType/newsTypeSave.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 newsTypeSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con=null;
String newsTypeId=request.getParameter("newsTypeId");
String typeName=request.getParameter("typeName");
NewsType newsType=new NewsType(typeName);
if(StringUtil.isNotEmpty(newsTypeId)){
newsType.setNewsTypeId(Integer.parseInt(newsTypeId));
}
try{
con=dbUtil.getCon();
if(StringUtil.isNotEmpty(newsTypeId)){
newsTypeDao.newsTypeUpdate(con, newsType);
}else{
newsTypeDao.newsTypeAdd(con, newsType);
}
request.getRequestDispatcher("/newsType?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 newsTypeBackList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con=null;
try{
con=dbUtil.getCon();
List<NewsType> newsTypeBackList=newsTypeDao.newsTypeList(con);
request.setAttribute("newsTypeBackList", newsTypeBackList);
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("新闻类别管理", "新闻类别维护"));
request.setAttribute("mainPage", "/background/newsType/newsTypeList.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 newsTypeDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String newsTypeId=request.getParameter("newsTypeId");
Connection con=null;
try{
con=dbUtil.getCon();
JSONObject result=new JSONObject();
boolean exist=newsDao.existNewsWithNewsTypeId(con, newsTypeId);
if(exist){
result.put("errorMsg", "该新闻类别下有新闻,不能删除此新闻类别");
}else{
int delNums=newsTypeDao.newsTypeDelete(con, newsTypeId);
if(delNums>0){
result.put("success", true);
}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();
}
}
}
}
UserServlet
package com.java1234.web;
import java.io.IOException;
import java.sql.Connection;
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 com.java1234.dao.UserDao;
import com.java1234.model.User;
import com.java1234.util.DbUtil;
public class UserServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
UserDao userDao=new UserDao();
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("login".equals(action)){
login(request,response);
}else if("logout".equals(action)){
logout(request,response);
}
}
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter("userName");
String password=request.getParameter("password");
HttpSession session=request.getSession();
Connection con=null;
try{
con=dbUtil.getCon();
User user=new User(userName,password);
User currentUser=userDao.login(con, user);
if(currentUser==null){
request.setAttribute("error", "用户名或者密码错误!");
request.setAttribute("password", password);
request.setAttribute("userName", userName);
request.getRequestDispatcher("/background/login.jsp").forward(request, response);
}else{
session.setAttribute("currentUser", currentUser);
request.setAttribute("mainPage", "/background/default.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 logout(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession().invalidate();
System.out.println(request.getContextPath()+"/background/login.jsp");
response.sendRedirect(request.getContextPath()+"/background/login.jsp");
}
}
LinkServlet
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 net.sf.json.JSONObject;
import com.java1234.dao.LinkDao;
import com.java1234.model.Link;
import com.java1234.util.DbUtil;
import com.java1234.util.NavUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
public class LinkServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
LinkDao linkDao=new LinkDao();
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("preSave".equals(action)){
linkPreSave(request,response);
}else if("save".equals(action)){
linkSave(request,response);
}else if("backList".equals(action)){
linkBackList(request,response);
}else if("delete".equals(action)){
linkDelete(request,response);
}
}
private void linkDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
String linkId=request.getParameter("linkId");
Connection con=null;
try{
con=dbUtil.getCon();
JSONObject result=new JSONObject();
int delNums=linkDao.linkDelete(con, linkId);
if(delNums>0){
result.put("success", true);
}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();
}
}
}
private void linkBackList(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
Connection con=null;
try{
con=dbUtil.getCon();
List<Link> linkBackList=linkDao.linkList(con, "select * from t_link order by orderNum");
request.setAttribute("linkBackList", linkBackList);
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("友情链接管理", "友情链接维护"));
request.setAttribute("mainPage", "/background/link/linkList.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 linkPreSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String linkId=request.getParameter("linkId");
Connection con=null;
try{
con=dbUtil.getCon();
if(StringUtil.isNotEmpty(linkId)){
Link link=linkDao.getLinkById(con, linkId);
request.setAttribute("link", link);
}
if(StringUtil.isNotEmpty(linkId)){
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("友情链接管理", "友情链接修改"));
}else{
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("友情链接管理", "友情链接添加"));
}
request.setAttribute("mainPage", "/background/link/linkSave.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 linkSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con=null;
String linkId=request.getParameter("linkId");
String linkName=request.getParameter("linkName");
String linkUrl=request.getParameter("linkUrl");
String linkEmail=request.getParameter("linkEmail");
String orderNum=request.getParameter("orderNum");
Link link=new Link(linkName, linkUrl, linkEmail, Integer.parseInt(orderNum));
if(StringUtil.isNotEmpty(linkId)){
link.setLinkId(Integer.parseInt(linkId));
}
try{
con=dbUtil.getCon();
if(StringUtil.isNotEmpty(linkId)){
linkDao.linkUpdate(con, link);
}else{
linkDao.linkAdd(con, link);
}
request.getRequestDispatcher("/link?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();
}
}
}
}
如果也想学习本系统,下面领取。关注并回复:127jsp