基于javaweb+mysql的jsp+servlet小说阅读管理系统(java+jsp+bootstrap+servlet+mysql)

基于javaweb+mysql的jsp+servlet小说阅读管理系统(java+jsp+bootstrap+servlet+mysql)

运行环境

Java≥8、MySQL≥5.7、Tomcat≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于javaweb+mysql的JSP+Servlet小说阅读管理系统(java+jsp+bootstrap+servlet+mysql)

项目介绍

项目主要包括管理员与用户两种角色; 管理员角色包含以下功能:管理员登录,主页面,分类查看,修改个人信息,上传小说,查看小说,删除小说,评论顶收藏小说等功能。 用户角色包含以下功能:用户注册,用户登录,查看销售,查看小说,发布小说,搜索小说,查看个人信息等功能。

环境需要

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
					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}");//密码输入错误
	        		  }
	        	 }
			}
			
	}
}
package servlets;

			   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){
			 String pid=req.getParameter("pid");
			 PrintWriter out=resp.getWriter();
			 if(type==null||content==null||pid==null){
				 return;
			 }
			 HttpSession session=req.getSession();
			 User user=(User)session.getAttribute("user");
			 if(user==null){
				 out.print("<script type='text/javascript'>"
				    		+ "window.location.href='/jsp_xiaoshuo_site/index.jsp'"
				    		+ "</script>");
				 return;
			 }		
			 int post_id=Integer.parseInt(pid);
			 Comment com=new Comment();
			 com.setContent(content);
			
			 com.setPost_id(post_id);
			 com.setUser_id(user.getUser_id());
			 Date now=new Date();
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			 String time=sdf.format(now);
			 com.setTime(time);
			 CommentDao cd=new CommentDao();
			 cd.insert(com);
		     out.print("<script type='text/javascript'>"
		    		+ "window.location.href='/jsp_xiaoshuo_site/jsp/postdetail.jsp?pid="+pid+"'"
		    		+ "</script>");
			 
		}
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		}
}
package servlets;

	    		 return;
	    	 }
	    	 JSONArray jsonArray=new JSONArray();
	    	 UserDao ud=new UserDao();
			 for(Comment c:coms){
				 User u=ud.getUser(c.getUser_id());  
				 JSONObject json=new JSONObject();
				 json.put("cid", c.getComment_id());
				 json.put("uid", u.getUser_id());
				 json.put("nickname", u.getNickname());
				 json.put("uicon", u.getUser_icon());
				 json.put("time", c.getTime());
				 json.put("agree", c.getAgree());
				 json.put("content", c.getContent());
				 jsonArray.put(json);
			 }
			 out.print(jsonArray.toString());
		}
	   
}
package servlets;

/**
 * 
 * 此类用于处理编辑器图片上传功能
 * 在处理有中文的url请求时没用
 * 这个问题以后再来讨论
 * 
 */

	}
    @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    			req.setCharacterEncoding("utf-8");
    			resp.setContentType("text/html;charset=utf-8");
    			PrintWriter out=resp.getWriter();
    			String type=req.getParameter("type");
    			String title=req.getParameter("title");
    			String content=req.getParameter("content");
    			if(type==null||title==null||content==null){
    				return;
    			}
    			HttpSession session=req.getSession();
    			User user=(User)session.getAttribute("user");
    			if(user==null){
    				out.print("<script type='text/javascript'>"+""
    						+ "alert('发小说成功!');"
    						+ "window.location='/jsp_xiaoshuo_site/index.jsp';"
    						+ "</script>");
    				out.close();
    				return;
    			}
    			ServletContext sc=getServletContext();
    			String path=sc.getRealPath("/WEB-INF/tie");
    			String fileid=UUID.randomUUID().toString();
    			PrintWriter outContent=new PrintWriter(new File(path+"/"+fileid),"utf-8");
    			outContent.print(content);
    			outContent.flush();
    			outContent.close();
    			PostDao pd=new PostDao();
    			Post p=new Post();
    			p.setUser_id(user.getUser_id());
    			p.setSrc("/"+fileid);
    			p.setTitle(title);
    			p.setType(type);
    			Date now=new Date();
    			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    			String formatDate=sdf.format(now);
    			p.setPost_time(formatDate);
    			pd.insert(p);
    			List<Post> ps=pd.getPosts(user.getUser_id());
    			int post_id=getMaxIdPost(ps);
    		    resp.sendRedirect("/jsp_xiaoshuo_site/jsp/postdetail.jsp?pid="+post_id);
    	 }
    	public int getMaxIdPost(List<Post> posts){
    		if(posts.size()==0)return 0;
			 if(user==null){
				 out.print("<script type='text/javascript'>"
				    		+ "window.location.href='/jsp_xiaoshuo_site/index.jsp'"
				    		+ "</script>");
				 return;
			 }		
			 int post_id=Integer.parseInt(pid);
			 Comment com=new Comment();
			 com.setContent(content);
			
			 com.setPost_id(post_id);
			 com.setUser_id(user.getUser_id());
			 Date now=new Date();
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			 String time=sdf.format(now);
			 com.setTime(time);
			 CommentDao cd=new CommentDao();
			 cd.insert(com);
		     out.print("<script type='text/javascript'>"
		    		+ "window.location.href='/jsp_xiaoshuo_site/jsp/postdetail.jsp?pid="+pid+"'"
		    		+ "</script>");
			 
		}
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		}
}
package servlets;


	@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 username = req.getParameter("username");
		String password = req.getParameter("password");
		String nickName = req.getParameter("nickname");
		String email = req.getParameter("email");
		UserDao ud = new UserDao();
		HttpSession session = req.getSession();
		String vercode = (String) session.getAttribute("vercode");
		String vr = req.getParameter("vercode");
		if (username == null) {
			if (vercode.equals(vr)) {
				out.print("{\"state\":0}");
				return;
			} else {
				out.print("{\"state\":111}");
				return;
			}
		} else {
			if (password == null || email == null || nickName == null) {
				User user = ud.getUser(username);
				if (user == null) { // 用户不存在
					String data = "{\"state\":0}";
					out.print(data);
					out.close();
				} else { // 用户存在
					String data = "{\"state\":1}";
					out.print(data);
					out.close();
				}

			} else {
				User user = new User();
				user.setUser_name(username);
				user.setPassword(password);
				user.setNickname(nickName);
				user.setEmail(email);
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String date = dateFormat.format(now);
				user.setReg_time(date);
				ud.insertUser(user);
					out.close();
				} else { // 用户存在
					String data = "{\"state\":1}";
					out.print(data);
					out.close();
				}

			} else {
				User user = new User();
				user.setUser_name(username);
				user.setPassword(password);
				user.setNickname(nickName);
				user.setEmail(email);
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String date = dateFormat.format(now);
				user.setReg_time(date);
				ud.insertUser(user);
				User currentUser = ud.getUser(username);
				session.setAttribute("user", currentUser);
				resp.sendRedirect("/jsp_xiaoshuo_site/jsp/main.jsp");
				out.close();
			}
		}

	}
}
package servlets;

	    	 String id=req.getParameter("id");
	    	 if (id.equals("1")) {
	    		 out.print("不能删除管理员账户!");
				
			}else {
				UserDao userDao = new UserDao();
				userDao.delete(id);
				PostDao postDao = new PostDao();
				List<Post> list = postDao.getPosts(Integer.parseInt(id));
				for(Post post:list) {
					postDao.delPost(post.getPost_id());
				}
				out.print("删除成功!");
			}
			
		}
	    @SuppressWarnings("unused")
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {	    	       
	    	 
		}
	   
}
package servlets;

/**
 * 
 * 登录
 * 
 * 
 * 
 */

		 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");
				 }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");
			 
		 }		 
	}
}
package servlets;

/**
 * 
 * 
 * 此类用于处理用户点赞,收藏,顶贴
 *
 */
@SuppressWarnings("serial")
public class HandleServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				 out.print(jsonArray.toString());
				 return;
	    	 }
	    	 CommentDao cd=new CommentDao();
	    	 List<Comment> coms=cd.getRangeOfCommentByDesc(start, end, pid);
	    	 if(coms.size()==0){
	    		 out.print("[]");
	    		 return;
	    	 }
	    	 JSONArray jsonArray=new JSONArray();
	    	 UserDao ud=new UserDao();
			 for(Comment c:coms){
				 User u=ud.getUser(c.getUser_id());  
				 JSONObject json=new JSONObject();
				 json.put("cid", c.getComment_id());
				 json.put("uid", u.getUser_id());
				 json.put("nickname", u.getNickname());
				 json.put("uicon", u.getUser_icon());
				 json.put("time", c.getTime());
				 json.put("agree", c.getAgree());
				 json.put("content", c.getContent());
				 jsonArray.put(json);
			 }
			 out.print(jsonArray.toString());
		}
	   
}
package servlets;

/**
 * 
 * 此类用于处理编辑器图片上传功能
 * 在处理有中文的url请求时没用
 * 这个问题以后再来讨论
 * 
 */
					String data = "{\"state\":0}";
					out.print(data);
					out.close();
				} else { // 用户存在
					String data = "{\"state\":1}";
					out.print(data);
					out.close();
				}

			} else {
				User user = new User();
				user.setUser_name(username);
				user.setPassword(password);
				user.setNickname(nickName);
				user.setEmail(email);
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String date = dateFormat.format(now);
				user.setReg_time(date);
				ud.insertUser(user);
				User currentUser = ud.getUser(username);
				session.setAttribute("user", currentUser);
				resp.sendRedirect("/jsp_xiaoshuo_site/jsp/main.jsp");
				out.close();
			}
		}

	}
}
package servlets;

/**
 * 
 * 此类用于用户修改头像 或者上传头像
 *
 */
@SuppressWarnings("serial")

//获取评论
@SuppressWarnings("serial")
public class GetCommentServlet extends HttpServlet{
		
	    @Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    	
		}
	    @SuppressWarnings("unused")
		@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 strStart=req.getParameter("start");
	    	 String strEnd=req.getParameter("end");
	    	 String strPid=req.getParameter("pid");
	    	 String hot=req.getParameter("hot");
	    	 List<String[]> agree=new ArrayList<String[]>(); 
	    	 if(strStart==null||strEnd==null||strPid==null)return;
	    	 int start=Integer.parseInt(strStart);
	    	 int end=Integer.parseInt(strEnd);
	    	 int pid=Integer.parseInt(strPid);
	    	 
	    	 if(hot!=null){
	    		 CommentDao cd=new CommentDao();
		    	 List<Comment> coms=cd.getHotComment(pid); //获取热门评论
			}
		if(filename==null)return;
    	out.print("{\"url\":\"tiezi/"+filename+"\",state:\"SUCCESS\"}");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//这个方法用于出来表单域
	@SuppressWarnings("unused")
	private void processFormField(FileItem item){
		String name=item.getFieldName();
		String value=item.getString();
	}
	
	
	//这个方法用于处理文件域
	private String processUploadFile(FileItem item,String filePath){
		String fileName=item.getName();
		System.out.println(fileName);
		int index=fileName.lastIndexOf("\\");
		fileName=fileName.substring(index+1,fileName.length());
		long fileSize=item.getSize();
		if(fileName.equals("")&&fileSize==0){
			return null;
		}
		UUID uid=UUID.randomUUID();
		String fileid=uid.toString();
		File uploadFile=new File(filePath+"/"+fileid);
		try {
			item.write(uploadFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fileid;
	}
}
package servlets;


/**
 * 
 * 
 * 发小说
 *
 */
@SuppressWarnings("serial")
public class PublishPostServlet 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/html;charset=utf-8");
    			PrintWriter out=resp.getWriter();
    			String type=req.getParameter("type");
    			String title=req.getParameter("title");
    			String content=req.getParameter("content");
    			if(type==null||title==null||content==null){
    				return;
    			}
    			HttpSession session=req.getSession();
    			User user=(User)session.getAttribute("user");
    			if(user==null){
    				out.print("<script type='text/javascript'>"+""
    						+ "alert('发小说成功!');"
    						+ "window.location='/jsp_xiaoshuo_site/index.jsp';"
    						+ "</script>");
    				out.close();
    				return;
    			}
    			ServletContext sc=getServletContext();
    			String path=sc.getRealPath("/WEB-INF/tie");
    			String fileid=UUID.randomUUID().toString();
			DiskFileItemFactory df=new DiskFileItemFactory();
			//设置向硬盘写数据时所用的缓冲区的大小
			df.setSizeThreshold(4*1024); //4K
			//设置临时目录
			df.setRepository(new File(tempFile));
			
			//创建一个上传器
			ServletFileUpload sf=new ServletFileUpload(df);
			sf.setSizeMax(4*1024*1024);
			List items=sf.parseRequest(req);
			Iterator iter=items.iterator();
			String filename=null;
			while(iter.hasNext()){
				FileItem item=(FileItem)iter.next();
				if(item.isFormField()){
					processFormField(item);
				}else{
					filename=processUploadFile(item,filePath);
				}
			}
		if(filename==null)return;
    	out.print("{\"url\":\"tiezi/"+filename+"\",state:\"SUCCESS\"}");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//这个方法用于出来表单域
	@SuppressWarnings("unused")
	private void processFormField(FileItem item){
		String name=item.getFieldName();
		String value=item.getString();
	}
	
	
	//这个方法用于处理文件域
	private String processUploadFile(FileItem item,String filePath){
		String fileName=item.getName();
		System.out.println(fileName);
		int index=fileName.lastIndexOf("\\");
		fileName=fileName.substring(index+1,fileName.length());
		}
	   
}
package servlets;

/**
 * 
 * 此类用于处理编辑器图片上传功能
 * 在处理有中文的url请求时没用
 * 这个问题以后再来讨论
 * 
 */

@SuppressWarnings("serial")
public class ImageUploadServlet extends HttpServlet{
	
	@SuppressWarnings("rawtypes")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//生成文件标识
		
		resp.setContentType("text/html;charset=utf-8");
		ServletContext sc=this.getServletContext();
		String tempFile="/temp";
		String filePath="/images/tiezi";
		tempFile=sc.getRealPath(tempFile);
		filePath=sc.getRealPath(filePath);
		PrintWriter out=resp.getWriter();
		try {
			DiskFileItemFactory df=new DiskFileItemFactory();
			//设置向硬盘写数据时所用的缓冲区的大小
			df.setSizeThreshold(4*1024); //4K
			//设置临时目录
			df.setRepository(new File(tempFile));
			
			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);
@SuppressWarnings("serial")
public class ChangeIconServlet extends HttpServlet{
		
	  @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		  	
		/*    UserDao ud=new UserDao();
		    User user=ud.getUser("tyqinjava");
		    user.setUser_icon("/face/1.png");
		    ud.update(user);*/
    }
	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 changeIcon=req.getParameter("changeIcon");
		           String iconName=req.getParameter("iconname");
		           HttpSession session=req.getSession();
		           UserDao ud=new UserDao();
		           
		           
		           if(changeIcon!=null&&iconName!=null){  //更改头像逻辑
		        	   User user=(User)session.getAttribute("user"); //session不同步问题先不讨论
		        	   if(user==null){
		        		  out.print("{\"state\":1}"); //session 过期
		        	   }else{
		        		  user.setUser_icon("face/"+iconName);
		        		  ud.update(user);
		        		  out.print("{\"state\":0}");
		        	   }
		           }else{
		        	   User user=(User)session.getAttribute("user"); //session不同步问题先不讨论
		        	   if(user==null){
		        		  out.print("{\"state\":1}"); //session 过期
		        	   }else{
		        		  user.setUser_icon(iconName); //上传头像
		        		  ud.update(user);
		        		  out.print("{\"state\":0}");
		        	   }
		           }
	}
}
package servlets;

				User currentUser = ud.getUser(username);
				session.setAttribute("user", currentUser);
				resp.sendRedirect("/jsp_xiaoshuo_site/jsp/main.jsp");
				out.close();
			}
		}

	}
}
package servlets;

/**
 * 
 * 此类用于用户修改头像 或者上传头像
 *
 */
@SuppressWarnings("serial")
public class ChangeIconServlet extends HttpServlet{
		
	  @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		  	
		/*    UserDao ud=new UserDao();
		    User user=ud.getUser("tyqinjava");
		    user.setUser_icon("/face/1.png");
		    ud.update(user);*/
    }
	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 changeIcon=req.getParameter("changeIcon");
		           String iconName=req.getParameter("iconname");
		           HttpSession session=req.getSession();
		           UserDao ud=new 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 过期
  			 	      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();
			 PrintWriter out=resp.getWriter();
			 if(type==null||content==null||pid==null){
				 return;
			 }
			 HttpSession session=req.getSession();
			 User user=(User)session.getAttribute("user");
			 if(user==null){
				 out.print("<script type='text/javascript'>"
				    		+ "window.location.href='/jsp_xiaoshuo_site/index.jsp'"
				    		+ "</script>");
				 return;
			 }		
			 int post_id=Integer.parseInt(pid);
			 Comment com=new Comment();
			 com.setContent(content);
			
			 com.setPost_id(post_id);
			 com.setUser_id(user.getUser_id());
			 Date now=new Date();
			 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			 String time=sdf.format(now);
			 com.setTime(time);
			 CommentDao cd=new CommentDao();
			 cd.insert(com);
		     out.print("<script type='text/javascript'>"
		    		+ "window.location.href='/jsp_xiaoshuo_site/jsp/postdetail.jsp?pid="+pid+"'"
		    		+ "</script>");
			 
		}
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
		}
}
package servlets;

package servlets;

@SuppressWarnings("serial")
public class VerCodeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		resp.setContentType("image/jpeg");
		OutputStream out = resp.getOutputStream();
		final char[] e = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
				'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		int height = 32;
		int width = 100;
		BufferedImage bufferImg = new BufferedImage
				(100, 32, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufferImg.createGraphics();
		Random rand = new Random();
		g.setColor(Color.white);
		g.fillRect(0, 0, width, height);
		Font f = new Font("Fixedsys", Font.PLAIN, 32);
		g.setFont(f);
		g.setColor(Color.white);
		g.drawRect(0, 0, width - 1, height - 1);
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			str.append(e[rand.nextInt(36)]);
		}
		g.setColor(Color.black);
			   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;}
						  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){

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值