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

📄 bookservlet.java

📁 该项目为网上书店
💻 JAVA
字号:
package org.myrose.servlet;

import java.io.IOException;
import java.util.List;

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

import org.myrose.beans.BookInfo;
import org.myrose.dao.BookInfoDAO;

public class BookServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BookServlet() {
		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>
	 * 
	 * 此方法根据传入的参数进行相应的操作
	 * 
	 * @parameter = new 表示查看新书操作
	 * @parameter = best 表示查看销售排行操作
	 * @parameter = detail 表示查看书的详细信息操作
	 * @parameter = search 表示查询书的操作
	 * 
	 * @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 {

		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");

		String parameter = request.getParameter("parameter");

		if (parameter.equals("new")) {
			this.getNewBook(request, response);
		} else if (parameter.equals("best")) {
			this.getBestBook(request, response);
		} else if (parameter.equals("detail")) {
			this.getBookDetail(request, response);
		} else if (parameter.equals("search")) {
			this.getBookBlur(request, response);
		}

	}

	public void getNewBook(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");

		BookInfoDAO bookDAO = new BookInfoDAO();
		List list = null;
		try {
			list = bookDAO.find5NewBooks();
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("list", list);
		request.getRequestDispatcher("newbook.jsp").forward(request, response);
	}

	public void getBestBook(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		BookInfoDAO bookDAO = new BookInfoDAO();
		List list = null;
		try {
			list = bookDAO.find10BestBooks();
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("list", list);
		request.getRequestDispatcher("sort.jsp").forward(request, response);
	}

	public void getBookDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");

		String isbn = request.getParameter("ISBN");

		BookInfoDAO bookDAO = new BookInfoDAO();
		BookInfo book = new BookInfo();

		book.setIsbn(isbn);

		try {
			book = (BookInfo) bookDAO.getBookByIsbn(book);
		} catch (Exception e) {
			e.printStackTrace();
		}

		request.setAttribute("book", book);
		request.getRequestDispatcher("book_detail.jsp").forward(request,
				response);
	}

	public void getBookBlur(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		
		String search = request.getParameter("search");
		String pnum = request.getParameter("pageNum");
		
		String keyword = null;
		if (!search.equals("") && search != null) {
			search = new String(search.getBytes("ISO-8859-1"));
			keyword = "%" + search + "%";
		}
		
		BookInfoDAO bookDAO = new BookInfoDAO();
		BookInfo book = new BookInfo();

		book.setBookName(keyword);

		int count = 0;
		int pageNum = 1;
		int pageSize = 10;
		int pageCount = 0;
		
		if( pnum != null){
			pageNum = Integer.parseInt(pnum);
		}
		
		try {
			count = bookDAO.getBookBlurCount(book).intValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List list = null;
		
		try {
			list = bookDAO.getBookBlur(book, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(count % pageSize == 0){
			pageCount = count / pageSize;
		}else{
			pageCount = count / pageSize + 1;
		}

		request.setAttribute("search", search);
		request.setAttribute("pageNum", pageNum+"");
		request.setAttribute("pageCount", pageCount + "");
		request.setAttribute("list", list);
		request.getRequestDispatcher("search_deal.jsp").forward(request,response);
	}

	/**
	 * 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 {

	}

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

}

⌨️ 快捷键说明

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