⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addbookservlet.java

📁 一套网上书店系统采用JAVABEAN+SERVLET+JSP
💻 JAVA
字号:
package cn.dang.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

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

import cn.dang.entity.Author;
import cn.dang.entity.Book;
import cn.dang.entity.BookType;
import cn.dang.entity.Concem;
import cn.dang.service.AllService;
import file_upload.File_upload;
import ghy_db.Conn_Manager;

public class AddBookServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddBookServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AllService ref = new AllService();
		String message = null;
		String msg = "返回书籍信息页面";
		try {
			// 创建文件上传对象
			File_upload File_upload_ref = new File_upload();
			//调用begin_upload()方法获得表单中的元素值
			HashMap hm = File_upload_ref.begin_upload(request, response);
			System.out.println();
			// 如果不存在这本书就进行添加
			if (!ref.getBookService().getBookId(hm.get("bkName").toString())) {		
				//创建容器对象保存书籍信息
				ArrayList values_list_book=new ArrayList();
				// 创建容器对象保存作者名字
				ArrayList values_list_author=new ArrayList();
				values_list_author.add(hm.get("authorName"));
				//调用服务层方法进行作者表ID获取
				int aid=ref.getAuthorService().getAuthorIdByName(values_list_author);
				//创建容器对象保存出版社名字
				ArrayList values_list_concem=new ArrayList();
				values_list_concem.add(hm.get("bookConcem"));
				//调用服务层方法进行出版社表ID查询
				int cid=ref.getConcemService().getConcemIdByName(values_list_concem);
				//将书籍表的相关信息保存到ArrayList对象中
				values_list_book.add(hm.get("bkName"));
				values_list_book.add(cid);
				values_list_book.add(hm.get("ISBN"));
				values_list_book.add(hm.get("publishTime"));
				values_list_book.add(hm.get("price"));
				values_list_book.add(hm.get("dangPrice"));
				values_list_book.add(hm.get("content"));
				values_list_book.add(aid);
				values_list_book.add(hm.get("cover").toString().substring(hm.get("cover").toString().lastIndexOf("\\")+1));
				values_list_book.add(hm.get("smallType2"));
				
				message = "书籍添加成功";
				// 调用服务层添加书的信息
				ref.getBookService().setBookInfo(values_list_book);
			} else {
				message = "书籍添加失败";
			}
			// 事务成功进行提交
			Conn_Manager.transaction_commit();
			// 跳转到制定页面
			request.setAttribute("message", message);
			request.setAttribute("msg", msg);
			request.setAttribute("rightBook", "../manager/rightBook.jsp");
			request.getRequestDispatcher("../manager/message.jsp").forward(
					request, response);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			// 失败事务回滚
			Conn_Manager.transaction_rollback();
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -