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

📄 productmanagerlistservlet.java

📁 使用jsp+Servlet,采用MVC模式,实现了一典型小型电子商务网站的全过程.包括前台和后台的全部功能.适合于初学者学习使用.
💻 JAVA
字号:
package com.eshop.servlet.manager.product;

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

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

import com.eshop.form.ProductSearchForm;
import com.eshop.page.PageBean;
import com.eshop.page.PageBusiness;
import com.eshop.page.ProductPage;
import com.eshop.service.ProductService;

public class ProductManagerListServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public ProductManagerListServlet() {
		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 {

		doPost(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 {
		
		response.setContentType("text/html; charset=gb2312");
		ProductSearchForm productSearchForm=new ProductSearchForm();
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("gbk");
		ProductService product=new ProductService();	
		List list=product.getAllProduct();//获得所有商品分类列表
		request.setAttribute("list", list);
		//显示index.jsp页面
		
		boolean done = false;//是否还需要构造搜索条件语句
		//获得conditions参数
		String conditions = (String) request.getParameter("conditions");
		//如果用户不是第一次输入搜索条件,而是点击了next之类的连接,那么conditions参数不是空。
		if (!"".equals(conditions) && conditions != null) {
			done = true;
		}
		//没有conditions参数,说明是第一次输入搜索条件,然后需要构造conditions参数。		
		else {
			String name = (String) request.getParameter("name1");
			String category = (String) request.getParameter("category1");
			if(name==null){
				name="";
			}
			if(category==null){
				category="";
			}
			
			productSearchForm.setCategory(category);
			productSearchForm.setProductName(name);
		    //String sql="";
		    //sql="select * from product";
			//将从输入的参数是否合法
		
			if(category == null||category=="" && name == null||name==""){
				 conditions ="1=1";
			}
			if(category != ""||!category.equals(null) && !name.equals("")||!name.equals(null)){
				   conditions="category like'%"+ category + "%' and name like '%"+ name + "%' ";
              //sql +=category+"like'%"+name+"%'";
			}
			 if(category == "" && name != ""){
					conditions="name like'%"+ name + "%' ";	
			}
			 if(category != "" && name == ""){
					conditions="category like'%"+ category + "%' ";
			}
			done=true;
		}
		if(done){	 
		try {
			PageBusiness pb = new ProductPage();
			String page = (String) request.getParameter("jumpPage");
			try {
				if (page.equals("") || page.equals(null))
					page = "1";
			} catch (Exception e) {
				page = "1";
			}
			//读取数据的结果集
			PageBean pageCtl = pb.listData(page, conditions);
			Vector vector=pageCtl.getResult();
			//读取当前页的值
			String curPage=String.valueOf(pageCtl.curPage);
			//读取最大值
			String maxPage=String.valueOf(pageCtl.maxPage);
			//最大行数
			String maxRowCount=String.valueOf(pageCtl.maxRowCount);
			//第页的行数
			String rowsPerPage=String.valueOf(pageCtl.rowsPerPage);
			//最大页数
			//String maxPage=String.valueOf(pageCtl.countMaxPage());
			//将查询结果存储到REQUEST对象中
			request.setAttribute("list", vector);
			
			//把PageBean保存到request对象中。
			request.setAttribute("SearchForm", productSearchForm);
			request.setAttribute("curPage", curPage);
			request.setAttribute("maxPage",maxPage);
			request.setAttribute("maxRowCount", maxRowCount);
			request.setAttribute("rowsPerPage", rowsPerPage);
			request.setAttribute("conditions", conditions);
//			request.setAttribute("page", pageCtl);
		} catch (Exception e) {
			out.println("发生异常:!" + e.getMessage());
			out.println("<a href=" + request.getRequestURI() + ">返回</a>");

		}
	
	RequestDispatcher requestDisptcher=request.getRequestDispatcher("../manager/product/productList.jsp");
	requestDisptcher.forward(request, response);
	}}
	/**
	 * 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 + -