一、简介
在快节奏的生活中,记录生活点滴、感悟和思考是一种重要的方式。基于此,我设计了一个基于JavaWeb的个人日记本系统,旨在帮助用户轻松记录并管理自己的日记。该系统包括登录、首页、日记列表、写日记、日记分类管理和个人中心等功能,能够满足用户对个人日记记录的基本需求。
二、功能介绍
登录
用户可以通过用户名和密码登录系统,以便访问个人的日记信息。
首页
首页展示了用户的日记列表,可以按照日记的分类和日期进行筛选,方便用户快速查找所需的日记。
日记列表
用户可以在日记列表页面查看自己的所有日记,每篇日记包括标题、日期、内容等信息,用户可以点击查看详细内容。
写日记
用户可以在系统中撰写新的日记,包括选择日记的分类、填写标题和内容等信息。
日记分类管理
用户可以管理自己的日记分类,包括添加新的分类、编辑分类信息、删除分类等操作,以便更好地组织和管理日记。
个人中心
个人中心提供了用户的个人信息管理功能,包括修改密码、查看登录记录等。
三、SQL分析
t_diary表分析:
- diaryId:日记ID,主键,自增长,int类型。
- title:日记标题,varchar(60)类型,存储日记的标题信息。
- content:日记内容,text类型,存储日记的详细内容。
- typeId:日记类型ID,int类型,外键关联t_diarytype表的diaryTypeId字段。
- releaseDate:发布日期,datetime类型,记录日记发布的日期时间信息。
t_diarytype表分析:
- diaryTypeId:日记类型ID,主键,自增长,int类型。
- typeName:日记类型名称,varchar(30)类型,记录日记的类型信息,如工作类、生活类等。
t_user表分析:
- userId:用户ID,主键,自增长,int类型。
- userName:用户名,varchar(20)类型,存储用户的登录名。
- password:密码,varchar(50)类型,存储用户的登录密码,经过加密处理。
- nickName:昵称,varchar(20)类型,存储用户的昵称信息。
- imageName:头像文件名,varchar(40)类型,存储用户上传的头像文件名。
- mood:心情签名,varchar(200)类型,存储用户的心情签名信息。
四、程序截图
五、关键代码
UserServlet.java
package com.wishwzp.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 org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;import com.wishwzp.dao.UserDao;
import com.wishwzp.model.User;
import com.wishwzp.util.DateUtil;
import com.wishwzp.util.DbUtil;
import com.wishwzp.util.PropertiesUtil;/*** Servlet implementation class UserServlet*/
public class UserServlet extends HttpServlet {private static final long serialVersionUID = 1L;DbUtil dbUtil=new DbUtil();UserDao userDao=new UserDao();/*** @see HttpServlet#HttpServlet()*/public UserServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis.doPost(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("utf-8");String action=request.getParameter("action");if("preSave".equals(action)){userPreSave(request,response);}else if("save".equals(action)){userSave(request,response);}}private void userPreSave(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setAttribute("mainPage", "user/userSave.jsp");request.getRequestDispatcher("mainTemp.jsp").forward(request, response); }private void userSave(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/** * 首先判断form的enctype是不是multipart/form-data * 同时也判断了form的提交方式是不是post * 方法:isMultipartContent(request) */ if(ServletFileUpload.isMultipartContent(request)){ System.out.println("yes");}// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUploadFileItemFactory factory=new DiskFileItemFactory();//创建ServletFileUpload对象ServletFileUpload upload=new ServletFileUpload(factory);List<FileItem> items=null;try {items=upload.parseRequest(new ServletRequestContext(request));} catch (FileUploadException e) {// TODO Auto-generated catch blocke.printStackTrace();}//取得items的迭代器 Iterator<FileItem> itr=items==null?null:items.iterator();HttpSession session=request.getSession();User user=(User)session.getAttribute("currentUser");boolean imageChange=false;//迭代itemswhile(itr.hasNext()){FileItem item=(FileItem)itr.next();//如果传过来的是普通的表单域 if(item.isFormField()){String fieldName=item.getFieldName();if("nickName".equals(fieldName)){user.setNickName(item.getString("utf-8"));}if("mood".equals(fieldName)){user.setMood(item.getString("utf-8"));}}else if(!"".equals(item.getName())){try{imageChange=true;String imageName=DateUtil.getCurrentDateStr();user.setImageName(imageName+"."+item.getName().split("\\.")[1]);String filePath=PropertiesUtil.getValue("imagePath")+imageName+"."+item.getName().split("\\.")[1];item.write(new File(filePath));}catch(Exception e){e.printStackTrace();}}}if(!imageChange){user.setImageName(user.getImageName().replaceFirst(PropertiesUtil.getValue("imageFile"), ""));}Connection con=null;try {con=dbUtil.getCon();int saveNums=userDao.userUpdate(con, user);if(saveNums>0){user.setImageName(PropertiesUtil.getValue("imageFile")+user.getImageName());session.setAttribute("currentUser", user);request.getRequestDispatcher("main?all=true").forward(request, response);}else{request.setAttribute("currentUser", user);request.setAttribute("error", "保存失败!");request.setAttribute("mainPage", "user/userSave.jsp");request.getRequestDispatcher("mainTemp.jsp").forward(request, response);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {dbUtil.closeCon(con);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
MainServlet.java
package com.wishwzp.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 com.wishwzp.dao.DiaryDao;
import com.wishwzp.dao.DiaryTypeDao;
import com.wishwzp.model.Diary;
import com.wishwzp.model.DiaryType;
import com.wishwzp.model.PageBean;
import com.wishwzp.util.DbUtil;
import com.wishwzp.util.PaginationUtils;
import com.wishwzp.util.PropertiesUtil;
import com.wishwzp.util.StringUtil;/*** Servlet implementation class MainServlet*/
public class MainServlet extends HttpServlet {private static final long serialVersionUID = 1L;DbUtil dbUtil=new DbUtil();DiaryDao diaryDao=new DiaryDao();DiaryTypeDao diaryTypeDao = new DiaryTypeDao();/*** @see HttpServlet#HttpServlet()*/public MainServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis.doPost(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("utf-8");HttpSession session = request.getSession();String s_typeId=request.getParameter("s_typeId");String s_releaseDateStr=request.getParameter("s_releaseDateStr");String s_title=request.getParameter("s_title");String all=request.getParameter("all");String page=request.getParameter("page");Diary diary=new Diary();if("true".equals(all)){if(StringUtil.isNotEmpty(s_title)){diary.setTitle(s_title);}session.removeAttribute("s_releaseDateStr");session.removeAttribute("s_typeId");session.setAttribute("s_title", s_title);}else{if(StringUtil.isNotEmpty(s_typeId)){diary.setTypeId(Integer.parseInt(s_typeId));session.setAttribute("s_typeId", s_typeId);session.removeAttribute("s_releaseDateStr");session.removeAttribute("s_title");}if(StringUtil.isNotEmpty(s_releaseDateStr)){//s_releaseDateStr=new String(s_releaseDateStr.getBytes("ISO-8859-1"),"UTF-8");diary.setReleaseDateStr(s_releaseDateStr);session.setAttribute("s_releaseDateStr", s_releaseDateStr);session.removeAttribute("s_typeId");session.removeAttribute("s_title");}if(StringUtil.isEmpty(s_typeId)){Object o=session.getAttribute("s_typeId");if(o!=null){diary.setTypeId(Integer.parseInt((String)o));}}if(StringUtil.isEmpty(s_releaseDateStr)){Object o=session.getAttribute("s_releaseDateStr");if(o!=null){diary.setReleaseDateStr((String)o);}}if(StringUtil.isEmpty(s_title)){Object o=session.getAttribute("s_title");if(o!=null){diary.setTitle((String)o);}}}if(StringUtil.isEmpty(page)){page="1";}Connection con=null;//初始化为1,4PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));try {con=dbUtil.getCon();List<Diary> diaryList=diaryDao.diaryList(con,pageBean,diary);int total=diaryDao.diaryCount(con,diary);String pageCode=PaginationUtils.getPagation(total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));request.setAttribute("pageCode", pageCode);request.setAttribute("diaryList", diaryList);//按日志类别显示List<DiaryType> diaryTypeCountList = diaryTypeDao.diaryTypeCountList(con);//按日志日期显示List<Diary> diaryCountList = diaryDao.diaryCountList(con);session.setAttribute("diaryTypeCountList", diaryTypeCountList);session.setAttribute("diaryCountList", diaryCountList); request.setAttribute("mainPage", "diary/diaryList.jsp");request.getRequestDispatcher("mainTemp.jsp").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
LoginServlet.java
package com.wishwzp.web;import java.io.IOException;
import java.sql.Connection;import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.wishwzp.dao.UserDao;
import com.wishwzp.model.User;
import com.wishwzp.util.DbUtil;/*** Servlet implementation class LoginServlet*/
public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;DbUtil dbutil = new DbUtil();UserDao userDao = new UserDao();/*** @see HttpServlet#HttpServlet()*/public LoginServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis.doPost(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("utf-8");String username = request.getParameter("userName");String password = request.getParameter("password");String remember = request.getParameter("remember");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("user", user);request.setAttribute("error", "用户名或密码错误!");request.getRequestDispatcher("login.jsp").forward(request, response);}else {if("remember-me".equals(remember)){rememberMe(username,password,response);}session.setAttribute("currentUser", currentUser);request.getRequestDispatcher("main").forward(request, response);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 记住密码* @param userName* @param password* @param response*/private void rememberMe(String username,String password,HttpServletResponse response){Cookie user=new Cookie("user",username+"-"+password);user.setMaxAge(1*60*60*24*7);response.addCookie(user);}
}
六、联系与交流
q:969060742 完整程序、sql、项目辅导视频