Normaly que se ofrecen y parámetros POST en un servlet la misma manera:
request.getParameter("cmd");
Pero sólo si los datos POST es encoded como pares de clave y valor de tipo de contenido: "application/x-www-form-urlencoded" como cuando se utiliza un formulario HTML estándar.
Si se utiliza un esquema de codificación diferente para sus datos de correo, como en su caso antes de enviar un flujo de datos JSON, es necesario utilizar un decodificador personalizado que puede procesar el flujo de datos en bruto de:
BufferedReader reader = request.getReader();
Json ejemplo el procesamiento posterior (utiliza org.json paquete)
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) { /*report an error*/ }
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...
}
Este es un método simple para obtener datos de solicitud 'request.getReader(). Lines().recoger (Collectors.joining()) ' –
lo anterior arroja secuencia ya excepción cerrada – Pat
Si utiliza el' getReader() 'la corriente conseguirá cerrada, ya que inicialmente sólo se puede leer de una vez. Hay una serie de alternativas en las implementaciones de Wrapper para permitir múltiples llamadas a 'getReader()' –