I'm calling my Java Servlet with an AJAX call, but I'm not able to read the input parameter from the request. I've tried two ways but with no luck:

```
var id;
$("#scan").click(function() {
    id = 1;
    $.ajax({ 
        type: "POST",
        data: id,
        url: "http://10.1.42.249:8080/test-notifier-web/RestLayer"
    });
});

```

And:

```
id = 1;
$.post('http://10.1.42.249:8080/test-notifier-web/RestLayer', {
    reqValue: id
}, function(responseText) { 
    // $('#welcometext').text(responseText);
    alert("OK!!!");
});

```

My servlet code is a simple log print of the request parameter, but the return value is always null:

```
String reqID = "";
log.info("Servlet called");
reqID = request.getParameter("reqValue");
log.info("reqID = " + reqID);

```

How can I get this working?

The only way I've found to get the code working is manually add the parameter to servlet url, like `http://10.1.42.249:8080/test-notifier-web/RestLayer?reqValue=1`
['https://Stackoverflow.com/questions/41694421', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3991150/']
i have check you code.this is my working code.

```
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript">
    var id;

    function fun() {
        alert("aaaa");
        id = 1;

        $.ajax({
            type : "POST",
            data : {
                reqValue : id
            },
            url : "/WebProject/callAjax"
        });
    }
</script>
</head>
<body>
    <button id="scan" onclick="fun()">Sacn</button>
</body>
</html>

```

//Servlet

```
@WebServlet(urlPatterns = {"/callAjax",})
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getParameter("reqValue"));
    }

}

```
```
var id;

$("#scan").click(function() {

        id = 1;

        $.ajax({ 
            type: "POST",
            data: { reqValue : id},
            url: "http://10.1.42.249:8080/test-notifier-web/RestLayer"
        });
});

```

There are different methods you need to override in servlet. Those are doPost(), doGet(), service() and etc.

I suspect you are using doGet() method that's why when you add parameter to the URL your java code is working and in other two cases as you are using `type : "POST"` java code is unable to read the data from Request Body( in post method the data will be added to Request Body).

I suggest you to use `doPost()` or `service()` methods instead doGet().