kuhaku kuhaku - 1 year ago 69
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


For example:

function bar() {
url: "servletName",
success: function(data) {

function baz() {
url: "servletName",
success: function(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);
document.getElementById("status").innerHTML = xhr.responseText;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download