/** * 获取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(); }