'request.getParameter() returns null when making a PUT ajax request in a Java servlet

I just started learning JSP and Java servlets and I stumbled upon an issue. I will use a simple example to show this unexpected behavior.

I have a .jsp file which has a button and makes an ajax request when clicking the button:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
    <button class="btn">Test button</button>
</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    $(".btn").click(function () {
            $.ajax({
                url: "hello-servlet",
                method: "GET",
                data: {action: "action", arg: "someData"}
            })
        }
    );
</script>
</html>

And this is the servlet that the above .jsp file references:

package com.example.demo;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {

    public HelloServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        String data = req.getParameter("arg");
        System.out.println(action + ", " + data);
    }
}

And there is no issue. It works. If I run it, or use the debugger to test it, I get the expected behavior. It prints action, someData so the parameters of the request were successfully retrieved in the servlet.

Now, I will change just this: the method argument in the ajax call from GET into PUT and the method I override in the servlet from doGet into doPut:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
    <button class="btn">Test button</button>
</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    $(".btn").click(function () {
            $.ajax({
                url: "hello-servlet",
                method: "PUT",
                data: {action: "action", arg: "someData"}
            })
        }
    );
</script>

</html>

And the servlet:

package com.example.demo;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {

    public HelloServlet() {
        super();
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        String data = req.getParameter("arg");
        System.out.println(action + ", " + data);
    }
}

I expected it to work in the same way. This does not happen. The output is null, null. The request parameters cannot be retrieved if I use PUT in the ajax call. I added the previous function doGet to the servlet to see if maybe the request doesn't make it to the PUT handler and defaults to GET. I used the debugger and I noticed that the execution does indeed enter the doPut handler, which is good, since that's what we expect to happen. It's just that at each req.getParamter() call I get null returned. I cannot retrieve the parameters if I use PUT, but I can retrieve them if I use GET (or POST for that matter, I tried it). This is weird to me and unexpected. What is going on and what can I do to solve it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source