作者主页:夜未央5788
简介: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项目:否
技术栈
1. 后端:servlet
2. 前端:JSP+css+javascript+bootstrap+jQuery
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中util/DBUtil.java配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/jsp_xiaoshuo_site
运行截图
用户角色
管理员角色
相关代码
ChangeFiledServlet
package servlets;
/**
*
* 此类用于处理基本资料的更改
*
*
*/
import java.io.IOException;
import java.io.PrintWriter;
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 bean.User;
import dao.UserDao;
@SuppressWarnings("serial")
public class ChangeFiledServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/plain;charset=utf-8");
PrintWriter out=resp.getWriter();
String type=req.getParameter("type");
String value=req.getParameter("value");
UserDao ud=new UserDao();
HttpSession session=req.getSession();
if(type==null&&value==null)return;
if(type.equals("changeusername")){
User user=ud.getUser(value);
if(user==null){
User us=(User)session.getAttribute("user");
if(us==null){
out.print("{\"state\":1}"); //session 过期
}else{
us.setUser_name(value);
ud.update(us);
out.print("{\"state\":0}");//成功
}
}else{
out.print("{\"state\":2}"); //用户已存在
}
}else if(type.equals("changenickname")){
User us=(User)session.getAttribute("user");
if(us==null){
out.print("{\"state\":1}"); //session 过期
}else{
us.setNickname(value);
ud.update(us);
out.print("{\"state\":0}");//成功
}
}else if(type.equals("setsex")){
User us=(User)session.getAttribute("user");
if(us==null){
out.print("{\"state\":1}"); //session 过期
}else{
us.setSex(value);
ud.update(us);
out.print("{\"state\":0}");//成功
}
}else if(type.equals("setbirthday")){
User us=(User)session.getAttribute("user");
if(us==null){
out.print("{\"state\":1}"); //session 过期
}else{
us.setBirthday(value);
ud.update(us);
out.print("{\"state\":0}");//成功
}
}else if(type.equals("changeemail")){
User us=(User)session.getAttribute("user");
if(us==null){
out.print("{\"state\":1}"); //session 过期
}else{
us.setEmail(value);;
ud.update(us);
out.print("{\"state\":0}");//成功
}
}else if(type.equals("changepassword")){
User us=(User)session.getAttribute("user");
if(us==null){
out.print("{\"state\":1}"); //session 过期
}else{
String[] v=value.split(",");
String old=v[0];
String New=v[1];
if(us.getPassword().equals(old)){
us.setPassword(New);
ud.update(us);
out.print("{\"state\":0}");//成功
}else{
out.print("{\"state\":2}");//密码输入错误
}
}
}
}
}
GetUserDetailServlet
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
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.json.JSONArray;
import org.json.JSONObject;
import bean.Collection;
import bean.Comment;
import bean.Post;
import bean.User;
import dao.CollectionDao;
import dao.CommentDao;
import dao.PostDao;
import dao.UserDao;
@SuppressWarnings("serial")
public class GetUserDetailServlet extends HttpServlet{
@SuppressWarnings("unused")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/plain;charset=utf-8");
String type=req.getParameter("type");
String uid=req.getParameter("uid");
int user_id=0;
if(uid!=null){
user_id=Integer.parseInt(uid);
}
PrintWriter out=resp.getWriter();
if(type==null)return;
HttpSession session=req.getSession();
User user=(User)session.getAttribute("user");
if(user==null){
return;
}
boolean isSmall=false;
if(user_id==user.getUser_id())
isSmall=true;
if(type.equals("getuserpost")){
UserDao ud=new UserDao();
User u=ud.getUser(user_id);//当前浏览的用户
if(u==null)return;
PostDao pd=new PostDao();
List<Post> ps=pd.getPosts(u.getUser_id());//用户发表过的帖子
JSONArray jsonArray=new JSONArray();
/*time
* pid
* title
* isSamll*/
for(Post p:ps){
JSONObject json=new JSONObject();
json.put("time", p.getPost_time());
json.put("pid", p.getPost_id());
json.put("title", p.getTitle());
if(isSmall){
json.put("isSmall", "true");
}else{
json.put("isSmall", "false");
}
jsonArray.put(json);
}
out.print(jsonArray.toString());
}else if(type.equals("delpost")){
String pid=req.getParameter("pid");
if(pid==null)return;
int post_id=Integer.parseInt(pid);
PostDao pd=new PostDao();
Post p=pd.getPost(post_id);
if(p==null)return;
pd.delPost(post_id);
CollectionDao cld=new CollectionDao();
cld.delCollectionByPostId(post_id);
CommentDao cd=new CommentDao();
out.print("ok");
}else if(type.equals("delcollection")){
String clid=req.getParameter("clid");
if(clid==null)return;
int cl_id=Integer.parseInt(clid);
CollectionDao cld=new CollectionDao();
cld.delCollectionById(cl_id);
out.print("ok");
return;
}else if(type.equals("getusercollection")){
if(uid==null){return;}
CollectionDao cld=new CollectionDao();
List<Collection> cls=cld.getCollectionByUserId(user_id);
if(cls.size()==0)return;
PostDao pd=new PostDao();
UserDao ud=new UserDao();
JSONArray jsonArray=new JSONArray();
for(Collection c:cls){
Post p=pd.getPost(c.getPost_id());
int uuid=pd.getUserIdByPostId(p.getPost_id());
User u=ud.getUser(uuid);
JSONObject json=new JSONObject();
/*time
* cl_id
* pid
* title
* isSamll
* uid
* unickname
* pid time tile isSmall
*/
json.put("cl_id",c.getCollection_id());
json.put("pid", p.getPost_id());
json.put("title", p.getTitle());
json.put("time", c.getTime());
json.put("uid",u.getUser_id());
json.put("unickname", u.getNickname());
if(isSmall){
json.put("isSmall", "true");
}else{
json.put("isSmall", "false");
}
jsonArray.put(json);
}
out.print(jsonArray.toString());
}else if(type.equals("getusercomment")){
if(uid==null){return;}
CommentDao cd=new CommentDao();
List<Comment> cs=cd.getCommentByUserId(user_id);
JSONArray jsonArray=new JSONArray();
for(Comment c:cs){
PostDao pd=new PostDao();
String isExist="false";
Post p=pd.getPost(c.getPost_id()); //发表的帖子
JSONObject json=new JSONObject();
if(p!=null){
isExist="true";
UserDao ud=new UserDao();
User u=ud.getUser(p.getUser_id()); //创建人
json.put("uid", u.getUser_id());
json.put("title",p.getTitle());
json.put("unickname",u.getNickname());
json.put("pid",p.getPost_id());
}
json.put("cid", c.getComment_id());
json.put("time",c.getTime());
if(isSmall){
json.put("isSmall", "true");
}else{
json.put("isSmall", "false");
}
json.put("content", c.getContent());
json.put("isExist", isExist);
jsonArray.put(json);
}
out.print(jsonArray.toString());
/*cid time isSmall content uid title unickname pid isExist*/
}else if(type.equals("delcomment")){
String cid=req.getParameter("cid");
if(cid==null)return;
int comment_id=Integer.parseInt(cid);
CommentDao cd=new CommentDao();
cd.delCommentById(comment_id);
out.print("ok");
}
}
}
HandleServlet
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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 bean.Collection;
import bean.Comment;
import bean.Post;
import bean.User;
import dao.CollectionDao;
import dao.CommentDao;
import dao.PostDao;
/**
*
*
* 此类用于处理用户点赞,收藏,顶贴
* @author acer
*
*/
@SuppressWarnings("serial")
public class HandleServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/plain;charset=utf-8");
PrintWriter out=resp.getWriter();
String type=req.getParameter("type");
HttpSession session=req.getSession();
User user=(User)session.getAttribute("user");
if(user==null){
out.print("session");
return;
}
if(type.equals("agree")){
String cid=req.getParameter("cid");
List<Integer> agree=(List<Integer>) session.getAttribute("agree");
int comment_id=Integer.parseInt(cid);
if(agree==null){
CommentDao cd=new CommentDao();
Comment c=cd.getCommentById(comment_id);
c.setAgree(c.getAgree()+1);
cd.update(c);
agree=new ArrayList<Integer>();
session.setAttribute("agree",agree);
agree.add(c.getComment_id());
out.print(c.getAgree());
return;
}
if(agree.size()==0){
CommentDao cd=new CommentDao();
Comment c=cd.getCommentById(comment_id);
c.setAgree(c.getAgree()+1);
cd.update(c);
agree.add(comment_id);
out.print(c.getAgree());
}else{
boolean isAgain=false;
for(Integer i:agree){
if(i==comment_id){
isAgain=true;
break;
}else {
continue;
}
}
if(isAgain){
out.print("agree");
return;
}
CommentDao cd=new CommentDao();
Comment c=cd.getCommentById(comment_id);
c.setAgree(c.getAgree()+1);
cd.update(c);
agree.add(comment_id);
out.print(c.getAgree());
return;
}
}else if(type.equals("collection")){
int pid=Integer.parseInt(req.getParameter("pid"));
CollectionDao cld=new CollectionDao();
List<Collection> cs=cld.getCollectionByUserId(user.getUser_id());
boolean isAgain=false;
if(cs.size()!=0){
for(Collection c:cs){
if(c.getPost_id()==pid){
isAgain=true;
break;
}else{
continue;
}
}
}
if(isAgain){
out.print("again");
return;
}
Collection c=new Collection();
c.setPost_id(pid);
c.setUser_id(user.getUser_id());
Date now=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=sdf.format(now);
c.setTime(time);
cld.insert(c);
out.print("ok");
return;
}else if(type.equals("hot")){
String pid=req.getParameter("pid");
int post_id=Integer.parseInt(pid);
List<Integer> hot=(List<Integer>)session.getAttribute("hot");
if(hot==null){
PostDao pd=new PostDao();
Post p=pd.getPost(post_id);
p.setHot(p.getHot()+1);
pd.update(p);
hot=new ArrayList<Integer>();
hot.add(post_id);
session.setAttribute("hot", hot);
out.print("ok");
return;
}else if(hot.size()==0){
PostDao pd=new PostDao();
Post p=pd.getPost(post_id);
p.setHot(p.getHot()+1);
pd.update(p);
hot.add(post_id);
out.print("ok");
}else{
boolean isAgain=false;
for(Integer i:hot){
if(i==post_id){
isAgain=true;
break;
}else {
continue;
}
}
if(isAgain){
out.print("again");
return;
}
PostDao pd=new PostDao();
Post p=pd.getPost(post_id);
p.setHot(p.getHot()+1);
pd.update(p);
hot.add(post_id);
out.print("ok");
return;
}
}
}
}
如果也想学习本系统,下面领取。关注并回复:115jsp