How to Retrieve JSON POST Data Using HttpServletRequest?
Dec 19, 2024 pm 09:19 PMHttpServletRequest: Retrieving JSON POST Data
In your web application, you're sending a POST request to a specific URL with JSON data, and you're using HttpServletRequest to access the POST data. However, when you enumerate the request parameters, you can only find the "cmd" parameter but not the JSON data.
Typically, you can obtain GET and POST parameters in a servlet using the request.getParameter("paramName") method. This works well when the POST data is encoded as key-value pairs with a content type of "application/x-www-form-urlencoded," as seen in standard HTML forms.
In your case, since you're sending a JSON data stream, you need to employ a custom decoder to process the raw data stream accessible from request.getReader(). Here's an example of how you can handle JSON POST processing:
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... }
In summary, if you're dealing with JSON POST data, you need to use a custom decoder to process it directly from the request body, rather than relying on the built-in parameter enumeration mechanism.
The above is the detailed content of How to Retrieve JSON POST Data Using HttpServletRequest?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

Node.js 20: Key Performance Boosts and New Features

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?
