servlet寫服務端API

iteye_17072發表於2010-08-02
/**
 * 獲取item
 * @author j
 *
 */
public class GetItemsServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CodeParse codeParse = new CodeParse();
		
		//定義接收資料字串
		String[] categoryId = null;
		String[] type = null;
		String[] page = null;
		String[] pagesize= null;
		String[] sortBy = null;
		String[] sortorder = null;
		//接收資料
		String readinfo = codeParse.receiveData(request.getInputStream());
		//解析xml檔案
		try {
			type = codeParse.parseXml(readinfo, "type");
			categoryId = codeParse.parseXml(readinfo, "category_id");
			page = codeParse.parseXml(readinfo, "page");
			pagesize = codeParse.parseXml(readinfo, "pagesize");
			sortBy = codeParse.parseXml(readinfo, "sort_by");
			sortorder = codeParse.parseXml(readinfo, "sort_order");
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (SAXException e1) {
			e1.printStackTrace();
		}
		//將資料轉換
		int classifyId = Integer.parseInt(categoryId[0]);
		int pageNum = Integer.parseInt(page[0]);
		int pageSize = Integer.parseInt(pagesize[0]);
		
		//獲取item
		List<Ring> ringList = new ArrayList<Ring>();
		try {
			ringList = DAOFactory.getRingDAOInstance().queryRingByClassify(classifyId, type[0], pageNum, pageSize, sortBy[0], sortorder[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//得到item總數
		int itemNum = 0;
		try {
			itemNum = DAOFactory.getRingDAOInstance().queryItemNumByClassify(classifyId);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		//計算頁數
		int allPages = itemNum%pageSize==0 ?itemNum/pageSize:itemNum/pageSize+1;
		
		//組裝返回資料
		String result = "<result version=\"1\">";
		result += " <items type=\""+type+"\" current_page=\""+pageNum+1+"\" total_page=\""+allPages+"\">";
		Iterator<Ring> iterator = ringList.iterator();
		Ring ring = null;
		while (iterator.hasNext()) {
			ring = iterator.next();
			result += "<item category_id= \""+categoryId+"\" name=\""+ring.getTitle()+"\" screenshot=\"http://xxx.xxx.xxx/screenshot.jpg\" rating=\""+ring.getScore()+"\" download=\""+ring.getNumDownload()+"\" price=\""+ring.getPrice()+"\" item_id=\""+ring.getId()+"\" purchased=\"true\" package_id=\"\" source=\"\" version=\"1.0.0\"/>";
		}
		result += "</items ></result>";
		
		//輸出資料
		codeParse.sendData(response.getOutputStream(), result);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}

 

這是一個servlet,獲取某個子目錄下的產品列表,接收的是xml資料,返回的也是xml資料
整個邏輯很簡單
1、接收資料
2、處理解析資料
3、利用解析資料進行資料庫處理
4、利用資料庫返回資料進行xml資料組裝
5、將xml資料輸出

關於資料解析的很多,這裡就不貼出來
主要提出來如何接收資料輸入和寫出資料
接收資料:

/**
	 * 接收客戶端資料
	 * @param inputStream 輸入流
	 * @return readinfo 返回讀入的資料
	 * @throws IOException
	 */
	public String receiveData(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[128];
		int len = 0;
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) >= 0) {
			bytes.write(buffer, 0, len);
		}
		String readinfo = bytes.toString();
		return readinfo;
	}

 輸出資料:

	/**
	 * 
	 * @param outputStream 輸出流
	 * @param str 需要得到的型別
	 * @throws IOException
	 */
	public void sendData(OutputStream outputStream,String str) throws IOException {
		BufferedOutputStream out = new BufferedOutputStream(outputStream);
		out.write(str.getBytes());
		out.flush();
	}

 

相關文章