getgoodstypeorname.java

来自「基于struts的网上商店源码」· Java 代码 · 共 120 行

JAVA
120
字号
package com.mole.servlet;

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

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

import com.mole.struts.dao.AdminSaleRecordDAO;

public class getGoodsTypeorName extends HttpServlet {

	private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
	private HttpServletRequest request;
	private HttpServletResponse response;
	private AdminSaleRecordDAO dao = new AdminSaleRecordDAO();
	private String storeID;

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

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

	/**
	 * 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 {
		request.setCharacterEncoding("UTF-8");
		response.setContentType(CONTENT_TYPE);
		this.request = request;
		this.response = response;
		String action = request.getParameter("action");
		if (action.equals("getGoodsType"))
			getGoodsType(request.getParameter("storeID"));
		else if (action.equals("getGoodsName"))
			getGoodsName(request.getParameter("goodsTypeID"));
	}

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

	public void getGoodsType(String storeID) {
		String sql = "select ID,Name from GoodsType where StoreID='" + storeID
				+ "'";
		try {
			ArrayList<Object[]> al = dao.executeQuery(sql);
			output(al);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void getGoodsName(String goodsType) {
		String sql = "select ID,Name from Goods where GoodsType='" + goodsType
				+ "'";
		try {
			ArrayList<Object[]> al = dao.executeQuery(sql);
			output(al);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void output(ArrayList<Object[]> al) {
		if (al != null && al.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < al.size(); i++) {
				Object[] obj = al.get(i);
				for (Object o : obj) {
					sb.append(o.toString() + ",");
				}
			}
			String str = sb.substring(0, sb.length() - 1);
			try {
				PrintWriter out = response.getWriter();
				out.print(str);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

⌨️ 快捷键说明

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