kuhaku kuhaku - 2 months ago 6
Ajax Question

A way to avoid many servlets

I want to avoid having many servlets because it doesn't seem right that for each button or resource on the page there will be a special servlet for it.

What I aim for is to make different ajax requests to the same servlet, and in that servlet to be able to differentiate between them in the

doGet
,
doPost
methods.

For example:

function bar() {
$.ajax({
url: "servletName",
success: function(data) {
foo(data);
}
});
}

function baz() {
$.ajax({
url: "servletName",
success: function(otherdata) {
anotherFoo(otherdata);
}
});
}


What can I add to the request to be able to differentiate between them (with if elses or switch)?

PS: I also have an html5 request (I think) so I would like to add something to it as well to make it distinct:

$(document).ready(function () {
$("#uploadbutton").click(function () {
var formData = new FormData();
formData.append("myfile", document.getElementById("file").files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", "rooms", false);
xhr.send(formData);
document.getElementById("status").innerHTML = xhr.responseText;
});
});

Answer

The so obvious thing you have to do is, passing an extra parameter in get and post requests from your AJAX requests and checking the parameter inside servlet using request.getParameter("type"); // type is what you are sending from client to detect the ajax request.