Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Browser testing is essential if you have a website or web applications that users interact with. Manual testing can be very helpful to an extent, but given the multiple browsers available, not to mention versions and operating system, testing everything manually becomes time-consuming and repetitive.

To help automate this process, Selenium is a popular choice for developers, as an open-source tool with a large and active community. What's more, we can further scale our automation testing by running on theLambdaTest cloud-based testing platform.

Read more through our step-by-step tutorial on how to set up Selenium tests with Java and run them on LambdaTest:

>> Automated Browser Testing With Selenium

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

1. Introduction

Extracting payload data from the request body effectively is crucial for Java servlets, which act as server-side components handling incoming HTTP requests.

This guide explores various methods for extracting payload data in Java servlets, along with best practices and considerations.

2. Understanding the Request Payload

Post requests are primarily used to send data to the server over HTTP requests. These data can be anything from form data containing user inputs to structured data like JSON and XML, or even binary files. These data reside in the request body, separate from the URL. This allows more extensive and secure data transmission. We can identify different types of data using a Content-Type header in the request.

Common content types include:

  • application/x-www-form-urlencoded: used for form data encoded as key-value pairs
  • application/json: used for JSON-formatted data
  • application/xml: used for XML-formatted data
  • text/plain: used for sending plain text
  • multipart/form-data: used for uploading binary files along with regular form data

3. Approaches for Retrieving POST Payload Data

Let’s explore different ways to extract data from the POST request payload.

3.1. Using getParameter() for Form-UrlEncoded Data

We can use the getParameter() method provided by the HttpServletRequest interface to retrieve specific form data using the parameter name submitted through the POST request. It takes the parameter name as an argument and returns the corresponding value as a String.

Let’s illustrate this with an example:

@WebServlet(name = "FormDataServlet", urlPatterns = "/form-data")
public class FormDataServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
        String firstName = StringEscapeUtils.escapeHtml4(req.getParameter("first_name"));
        String lastName = StringEscapeUtils.escapeHtml4(req.getParameter("last_name"));

        resp.getWriter()
          .append("Full Name: ")
          .append(firstName)
          .append(" ")
          .append(lastName);
    }
}

This method can handle key-value pairs, but it isn’t suitable to handle complex data structures.

We have used escapeHtml4() method of StringEscapeUtils class from apache commons text library to sanitize input by encoding special character. This is helpful in preventing XSS attacks. We can use this library by adding following dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.10.0</version>
</dependency>

3.2. Reading the Raw Payload String

For more flexibility, we can access raw payload data using the getReader() method from the HttpServletRequest interface.

This method returns a BufferedReader object, which allows us to read data line by line:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws IOException {

    StringBuilder payload = new StringBuilder();
    try(BufferedReader reader = req.getReader()){
        String line;
        while ((line = reader.readLine()) != null){
            payload.append(line);
        }
    }

    resp.getWriter().append(countWordsFrequency(payload.toString()).toString());
}

Important considerations:

  • We must be careful while handling large payloads to avoid memory issues
  • We might need to handle character encoding differences based on the request

3.3. Parsing Structured Data Formats (JSON, XML)

Structured data formats like JSON and XML are widely used for exchanging data between clients and servers. We can use dedicated libraries to parse payloads into Java Objects.

For parsing JSON data, we can use popular libraries like Jackson or Gson. In our example, we’ll be using Gson. For that, we need to add another dependency:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>

We can read the JSON payload from the request body using the BufferedReader object to get it as plain text, and then we can use Gson to parse it into a Java object.

Here’s the example code to parse JSON data:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws IOException {
    String contentType = req.getContentType();
    if (!("application/json".equals(contentType))) {
        resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, 
          "Invalid content type");
        return;
    }
    try (BufferedReader reader = req.getReader()) {
        Gson gson = new Gson();
        Product newProduct = gson.fromJson(reader, Product.class);
        resp.getWriter()
            .append("Added new Product with name: ")
            .append(newProduct.getName());

    } catch (IOException ex) {
        req.setAttribute("message", "There was an error: " + ex.getMessage());
    }
}

We should always validate the content type before parsing to prevent unexpected data format and security issues.

For parsing XML data let’s use the XStream library. We’ll use the following dependency in our code:

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.20</version>
</dependency>

To parse XML from the request body, we can read it as plain text, the same way we did for JSON payload, and use XStream to parse it as a Java object.

Here’s the example code to parse XML payload from a POST request:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws IOException {
    String contentType = req.getContentType();
    if (!("application/xml".equals(contentType))) {
        resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, 
          "Invalid content type");
        return;
    }
    try (BufferedReader reader = req.getReader()) {
        XStream xStream = new XStream();
        xStream.allowTypesByWildcard(new String[] { "com.baeldung.**" });
        xStream.alias("Order", Order.class);
        Order order = (Order) xStream.fromXML(reader);

        resp.getWriter()
            .append("Created new Order with orderId: ")
            .append(order.getOrderId())
            .append(" for Product: ")
            .append(order.getProduct());
    } catch (IOException ex) {
        req.setAttribute("message", "There was an error: " + ex.getMessage());
    }
}

3.4. Handling multipart/form-data

The multipart/form-data content type is crucial when dealing with file uploads. This is specifically designed to handle forms that include binary data, such as images, videos, or documents, along with regular text data.

To handle multipart/form-data, we must annotate our servlet with @MultipartConfig or configure the servlet in our web.xml.

@MultipartConfig provides various parameters to control file upload behavior, such as location (temporary storage directory), maxFileSize (maximum size for a single uploaded file), and maxRequestSize (maximum size for the entire request):

@MultipartConfig(fileSizeThreshold = 1024 * 1024, 
  maxFileSize = 1024 * 1024 * 5, 
  maxRequestSize = 1024 * 1024 * 5 * 5)
public class FileUploadServlet extends HttpServlet {

In a servlet, we can retrieve the individual parts of the multipart/form-data request using the getPart(String name) method for a specific part or getParts() to retrieve all parts. The Part interface provides methods to access details like the file name, content type, size, and input stream.

Let’s illustrate an example of uploading a file with a POST request payload:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {

    String uploadPath = getServletContext().getRealPath("") + 
      File.separator + UPLOAD_DIRECTORY;
    File uploadDir = new File(uploadPath);
    if (!uploadDir.exists()) {
        uploadDir.mkdir();
    }

    Part filePart = request.getPart("file");
    if (filePart != null) {
        String fileName = Paths.get(filePart.getSubmittedFileName())
          .getFileName().toString();
        if(fileName.isEmpty()){
            response.getWriter().println("Invalid File Name!");
            return;
        }
        if(!fileName.endsWith(".txt")){
            response.getWriter().println("Only .txt files are allowed!");
            return;
        }

        File file = new File(uploadPath, fileName);

        try (InputStream fileContent = filePart.getInputStream()) {
            Files.copy(fileContent, file.toPath(), 
              StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            response.getWriter().println("Error writing file: " + 
              e.getMessage());
            return;
        }

        response.getWriter()
            .println("File uploaded to: " + file.toPath());
    } else {
        response.getWriter()
            .println("File upload failed!");
    }
}

Key Security Considerations:

  • Path traversal Protection
  • File type validation
  • File size limit
  • Safe file writing

4. Best Practices and Common Pitfalls

4.1. Content-Type Validation

We should always validate the content type of incoming requests to ensure the server processes requests correctly. This helps prevent unexpected data formats and potential security vulnerabilities.

For example, if our servlet expects JSON, we should check the Content-Type header to be of type application/json before processing:

String contentType = req.getContentType();
if (!("application/json".equals(contentType))) {
    resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, 
      "Invalid content type");
    return;
}

Or, we can do something more robust with Apache Tika.

4.2. Error Handling

We should always implement proper error handling when reading and processing payload data. This ensures that our application can gracefully handle unexpected situations.

We should also provide meaningful messages with HTTP status codes, which can help tell both the developer and the user what went wrong.

4.3. Performance Optimization

Handling very large payloads can impact the performance. To optimize, we should consider limiting the size of incoming requests, streaming data processing, and avoiding data copying. Libraries like Apache Commons IO can be helpful for efficient payload handling.

Also, we should ensure our servlet is not performing blocking operations, which could block the request handling.

4.4. Security

Security is a critical consideration when handling POST request payloads. Some key practices include:

  • Input Validation: always validate and sanitize input data to prevent injection attacks
  • Authentication and Authorization: ensure that only authorized users can access certain endpoints
  • CSRF Protection: implement Cross-Site Request Forgery (CSRF) tokens to prevent unauthorized commands from being transmitted from a user that the web application trusts.
  • Data Encryption: use HTTPS to encrypt data in transit, protecting sensitive information
  • Limit Upload Size: set limits on the size of uploaded files to prevent denial-of-service attacks

OWASP Top Ten provides detailed information on various vulnerabilities and recommended practices for securing web applications.

5. Conclusion

In this article, we’ve seen various methods for accessing and processing POST request payload in servlets, considering various formats from simple form data to complex JSON or XML and Multipart files. We’ve also discussed best practices that are essential for creating secure and efficient web applications.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)