通常,你可以以相同的方式在Servlet中获取GET和POST参数:
request.getParameter("cmd");但仅当POST数据被编码为内容类型的键/值对时:
“ application / x-www-form-urlenpred”,例如使用标准HTML表单时。
如果你对发布数据使用不同的编码模式,例如在发布json数据流的情况下,则需要使用可以处理以下内容的原始解码器的自定义解码器:
BufferedReader reader = request.getReader();
Json后处理示例(使用org.json包)
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuffer jb = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) jb.append(line); } catch (Exception e) { } try { JSonObject jsonObject = HTTP.toJSonObject(jb.toString()); } catch (JSonException e) { // crash and burn throw new IOException("Error parsing JSON request string"); } // Work with the data using methods like... // int someInt = jsonObject.getInt("intParamName"); // String someString = jsonObject.getString("stringParamName"); // JSonObject nestedObj = jsonObject.getJSonObject("nestedObjName"); // JSonArray arr = jsonObject.getJSonArray("arrayParamName"); // etc...}


