首页>代码>java servlet+bootstrap+ajax实现的教师年度工作填报系统>/report_servlet/src/com/mocha/report/article/servlet/ArticleServlet.java
package com.mocha.report.article.servlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.alibaba.fastjson.JSON;
import com.mocha.report.article.service.ArticleSer;
import com.mocha.report.article.service.impl.ArticleSerImpl;
import com.mocha.report.entites.Article;
import com.mocha.report.entites.Teacher;


public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int page = 1;
	private List<Article> articles = null;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String requestURI = request.getRequestURI();
		String uri = requestURI.substring(requestURI.lastIndexOf("/"));
		if("/findByUsercode.do".equals(uri)){
			this.findByUsercode(request, response);
		}
		if("/save.do".equals(uri)){
			this.save(request, response);
		}
		if("/delete.do".equals(uri)){
			this.delete(request, response);
		}
		if("/findByAid.do".equals(uri)){
			this.findByAid(request, response);
		}
		if("/update.do".equals(uri)){
			this.update(request, response);
		}
		if("/findByCondition.do".equals(uri)){
			this.findByCondition(request,response);
		}
		if("/deleteByAdmin.do".equals(uri)){
			this.deleteByAdmin(request,response);
		}
		if("/findTotal.do".equals(uri)){
			this.findTotal(request,response);
		}
	}
	
	private void findTotal(HttpServletRequest request,
			HttpServletResponse response) {
		ArticleSer articleSer = new ArticleSerImpl();
		List<Map<String , String>> listss = articleSer.findTotal();
		String jsonString = JSON.toJSONString(listss);
		System.out.println(jsonString);
		try {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().print(jsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	private void findByCondition(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		Article article = new Article();
		String strPage = request.getParameter("page");
		String usercode = request.getParameter("usercode");
		if(usercode != null && usercode.length()>0){
			article.setUsercode(usercode);
		}
		
		ArticleSer articleSer = new ArticleSerImpl();
		
		if (strPage != null) {
			page = Integer.parseInt(strPage);
		}
		try {
			articles = articleSer.findByCondition(article, page, 10);
			int maxPage = articleSer.getMaxPage(10, article);
			request.setAttribute("articles", articles);
			request.setAttribute("page", page);
			request.setAttribute("maxPage",maxPage );
			// 转发
			request.getRequestDispatcher("/article/admin_article.jsp").forward(
					request, response);
		} catch (SQLException e) {
			response.sendRedirect("error.html");
			e.printStackTrace();
		}
				
	}


	// 根据条件查询所有的user信息
	public void findByUsercode(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获得页面的页数,以及页数的记录
		
		String strPage = request.getParameter("page");
		Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
		String usercode = teacher.getUsercode();
		ArticleSer articleSer = new ArticleSerImpl();
		
		if (strPage != null) {
			page = Integer.parseInt(strPage);
		}
		try {
			articles = articleSer.findByUsercode(usercode, page, 10);
			int maxPage = articleSer.getMaxPage(10, usercode);
			request.setAttribute("articles", articles);
			request.setAttribute("page", page);
			request.setAttribute("maxPage",maxPage );
			// 转发
			request.getRequestDispatcher("/article/article.jsp").forward(
					request, response);
		} catch (SQLException e) {
			response.sendRedirect("error.html");
			e.printStackTrace();
		}
	}
	//添加
		@SuppressWarnings("unchecked")
		public void save(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			/*
			 * 1.commons-fileupload的上传三步
			 */
			//1.创建工厂
			FileItemFactory fileItemFactory = new DiskFileItemFactory();
			
			//2.创建解析器对象
			ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
			servletFileUpload.setFileSizeMax(200 * 1024 * 1024);
			
			//3.解析request得到List<FileItem>
			List<FileItem> fileItemList = null;
			try {
				fileItemList = servletFileUpload.parseRequest(request);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			
			//4.把List<FileItem>装到RestaurantDelicious对象中
			//4.1首先把普通表单字段放到一个Map中,再把Map转换成RestaurantDelicious对象
			List<String> list = new ArrayList<String>();
			for(FileItem fileItem:fileItemList){
				if(fileItem.isFormField()){//如果是普通表单字段
					list.add(fileItem.getString("UTF-8"));
				}
			}
			
			String usercode = list.get(0);
			String aname = list.get(1);
			String publishdate = this.chooseDate(list.get(2));
			
			String uploaddate = this.chooseDate(list.get(3));
			String publishreader = list.get(4);
			String summary = list.get(5);
			
			Article article = new Article(aname,null,publishdate,publishreader,uploaddate,summary,usercode);
			
			FileItem fileItem = fileItemList.get(2);
			String fileName = fileItem.getName();
			int index = fileName.lastIndexOf("\\");
			if(index != -1){
				fileName = fileName.substring(index + 1);
			}
			fileName = Math.random()*1000 + "_" +fileName;
			
			String savePath = this.getServletContext().getRealPath("/upload/article");
			File destFile = new File(savePath,fileName);
			
			try {
				fileItem.write(destFile);//他会把临时文件重命名到指定的路径,再删除临时文件
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			
			//吧图片的路径设置给RestaurantDelicious对象
			article.setApath("/report_servlet/upload/article/"+fileName);
			
			//调用业务逻辑
			ArticleSer articleSer =new ArticleSerImpl();
			try {
				articleSer.save(article);
				response.sendRedirect("articleServlet/findByUsercode.do");
			} catch (Exception e) {
				e.printStackTrace();
				response.sendRedirect("error.html");
			}

		}
		
		public void delete(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			//获取请求数据
			 Integer aid	= Integer.parseInt(request.getParameter("aid"));
			 //调用业务逻辑
			 ArticleSer articleSer = new ArticleSerImpl();
			 try {
				 articleSer.delete(aid);
					response.sendRedirect("articleServlet/findByUsercode.do");
				} catch (Exception e) {
					e.printStackTrace();
					response.sendRedirect("error.html");
				}
		}
		
		public void deleteByAdmin(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			System.out.println("9090909090");
			//获取请求数据
			 Integer aid	= Integer.parseInt(request.getParameter("aid"));
			 //调用业务逻辑
			 ArticleSer articleSer = new ArticleSerImpl();
			 try {
				 articleSer.delete(aid);
					response.sendRedirect("articleServlet/findAll.do");
				} catch (Exception e) {
					e.printStackTrace();
					response.sendRedirect("error.html");
				}
		}
		
		public void findByAid(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			Article article = new Article();
			//获取请求数据
			 Integer aid = Integer.parseInt(request.getParameter("aid"));
			 //调用业务逻辑
			 ArticleSer articleSer = new ArticleSerImpl();
			 try {
				 article = articleSer.findByAid(aid);
					request.setAttribute("article", article);
					request.getRequestDispatcher("/article/update_article.jsp").forward(request, response);
				} catch (Exception e) {
					e.printStackTrace();
					response.sendRedirect("error.html");
				}
		}
		
		@SuppressWarnings("unchecked")
		public void update(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			/*
			 * 1.commons-fileupload的上传三步
			 */
			//1.创建工厂
			FileItemFactory fileItemFactory = new DiskFileItemFactory();
			
			//2.创建解析器对象
			ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
			servletFileUpload.setFileSizeMax(200 * 1024 * 1024);
			
			//3.解析request得到List<FileItem>
			List<FileItem> fileItemList = null;
			try {
				fileItemList = servletFileUpload.parseRequest(request);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			
			//4.把List<FileItem>装到RestaurantDelicious对象中
			//4.1首先把普通表单字段放到一个Map中,再把Map转换成RestaurantDelicious对象
			List<String> list = new ArrayList<String>();
			for(FileItem fileItem:fileItemList){
				if(fileItem.isFormField()){//如果是普通表单字段
					list.add(fileItem.getString("UTF-8"));
				}
			}
			
			String usercode = list.get(0);
			int aid = Integer.parseInt(list.get(1));
			String aname = list.get(2);
			String publishdate = list.get(3);
			if(list.get(3).contains("/")){
				publishdate = this.chooseDate(list.get(3));
			}
			
			String uploaddate = list.get(4);
			if(list.get(4).contains("/")){
				uploaddate = this.chooseDate(list.get(4));
			}
			
			
			
			String publishreader = list.get(5);
			String summary = list.get(6);
			System.out.println(summary);
			Article article = new Article(aid,aname,null,publishdate,publishreader,uploaddate,summary,usercode);
			
			FileItem fileItem = fileItemList.get(3);
			String fileName = fileItem.getName();
			int index = fileName.lastIndexOf("\\");
			if(index != -1){
				fileName = fileName.substring(index + 1);
			}
			fileName = Math.random()*1000 + "_" +fileName;
			
			String savePath = this.getServletContext().getRealPath("/upload/article");
			File destFile = new File(savePath,fileName);
			
			try {
				fileItem.write(destFile);//他会把临时文件重命名到指定的路径,再删除临时文件
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			
			//吧图片的路径设置给RestaurantDelicious对象
			article.setApath("/report_servlet/upload/article/"+fileName);
			
			//调用业务逻辑
			ArticleSer articleSer =new ArticleSerImpl();
			try {
				articleSer.update(article);
				response.sendRedirect("articleServlet/findByUsercode.do");
			} catch (Exception e) {
				e.printStackTrace();
				response.sendRedirect("error.html");
			}


		}
		public  String chooseDate(String stringDate){
			StringBuilder newStringData = new StringBuilder();
			String[] split = stringDate.split("/");
			newStringData.append(split[2].toString());
			newStringData.append("-");
			newStringData.append(split[0].toString());
			newStringData.append("-");
			newStringData.append(split[1].toString());
			return newStringData.toString();
			
		}
		
		public static void main(String[] args) {
			
		}
		
}
最近下载更多
森sdfgf  LV8 4月2日
hongdongdong  LV14 2023年6月17日
try8023  LV19 2023年4月29日
mousse  LV1 2023年3月13日
liu2022  LV14 2022年7月31日
lvhongquan  LV11 2022年6月16日
Zac141  LV3 2022年4月27日
zacoder  LV3 2022年3月26日
HONG SIR  LV8 2022年2月28日
早早早  LV1 2022年1月18日
最近浏览更多
taoshen95  LV15 6月25日
兮希熙西  LV1 4月28日
森sdfgf  LV8 4月2日
rongtao  LV1 1月9日
WBelong  LV8 2023年12月25日
186092  LV4 2023年9月20日
攻城狮aoao  LV3 2023年8月29日
wersdfs  LV1 2023年7月10日
hongdongdong  LV14 2023年6月17日
try8023  LV19 2023年4月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友