Unsuccessful Invocation of Servlet by Ajax Function

I have a situation where I am trying to trigger an Ajax javascript function from my jsp file, with the intention of loading a servlet for further processing. The issue I am facing is that even though I am able to pass values from the jsp to the ajax function successfully, the servlet is not being called as expected. Despite conducting thorough research online, I have been unable to identify the missing piece of the puzzle.

Below is a snippet of my jsp code where I call the ajax javascript function:

<display:column title="Merge Print"><a href="#" onClick="printMerge('arg1', 'arg2')">Click Here</a></display:column>

In a separate ajax.js file, here is the code for my printMerge function:

function printMerge(arg1, arg2) {
alert('In printMerge '+arg1, arg2);
new Ajax.Request('servlet/PrintMerge', {
    method: 'post',
    parameters: { arg1: arg1.value, arg2: arg2.value },
onSuccess: function(transport) {
    var response = transport.responseText || "no response text";
    if(response =='success') {
          alert('RESPONSE: SUCCESS');
          reloadPage();
    } else {
          alert('RESPONSE: ERROR');
    },
onFailure: function() { alert('FAILURE'); }
});
}

While the initial alert displays the arguments correctly, indicating that the jsp is effectively calling the function and passing the parameters, the process seems to halt there without progressing to the 'PrintMerge' servlet for further actions.

This is how the PrintMerge servlet looks like:

<servlet>
     <servlet-name>PrintMerge</servlet-name>
     <servlet-class>com.servlet.PrintMerge</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>PrintMerge</servlet-name>
     <url-pattern>/servlet/PrintMerge</url-pattern>
</servlet-mapping>

Although similar configurations work for other scenarios, I believe there might be a crucial step that I am overlooking since the ajax function fails to reach the servlet. Your insights or suggestions on this matter would be greatly appreciated. Thank you for your help in advance.

Answer №1

If you're encountering issues, try calling the full qualified name of the target servlet instead of servlet/PrintMerge. Give it another shot with a URL like

http://[::1]/my_ctx/servlet/PrintMerge

In certain situations, it may be necessary to specify the response content-type when working with the servlet, as shown below.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain;charset=UTF-8");
      //String resp = serviceCall;
      try(PrintWriter out=response.getWriter()){out.write(serviceCall);}
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

retrieve the value of a text form using jQuery and its corresponding id

I need help with a basic HTML form that includes jQuery. My goal is to: 1) Retrieve the value into a JavaScript variable. 2) Send it as JSON to a server. Here is the form: <div data-role="main" class="ui-content"> <data=role "form" ...

Ensure to verify the `childProperty` of `property` within the `req.checkBody

When working with Node.js, a common practice is to use code like the following: req.checkBody('name', 'Group name is required.').notEmpty(); In a similar fashion, I have implemented something along these lines: req.checkBody('pa ...

Setting a JavaScript value for a property in an MVC model

I am currently working on an asp.net mvc application and I am in the process of implementing image uploading functionality. Below is the code for the image upload function: $(document).ready(function () { TableDatatablesEditable.init(); ...

Using enzyme mock function prior to componentDidMount

When it comes to mocking a function of a component using Jest, Enzyme, and React, the process typically involves creating a shallow wrapper of the component and then overloading the function as needed. However, there seems to be an issue where the componen ...

Trouble encountered while refreshing a <div> element using AJAX functionality

My web page is being dynamically generated by scriptA.php. Every 5 minutes, the content of the #WatchContainer div element needs to be updated. This content is created by scriptB.php. I have included scriptB.php in scriptA.php using "include" and a variabl ...

The utilization of jQuery's ajax feature is resulting in an unexpected full page refresh when using FireFox browser

Currently, I am facing a bit of a problem with an ajax call using jQuery. It seems to be working perfectly fine in Internet Explorer 7, however, FireFox 3 always initiates a full page refresh whenever the call is made. This ajax call is set to POST to an A ...

Transmitting JSON information to a Spring Controller

I am experiencing an issue while attempting to transmit JSON data from an Angular POST request to a Spring controller. The console shows a 404 error: POST http://localhost:8080/app/orderDetails/saveOrder/[object%20Object],[object%20Object] 404 (Not ...

I aim to prevent users from liking a post multiple times within Firebase

I came up with this code snippet to implement the Like functionality: /*Incrementing by 1 every time a user submits a like*/ db.collection("post").doc(postId).update({ likes: increment }) /*Collecting the UID of the user who submitted the l ...

Tips for consolidating outputs from three different APIs using JavaScript and AJAX? [Pseudo code example]

For my school project, I am working on an e-commerce aggregator site where I need to combine product data from 3 different APIs (like Aliexpress and Amazon) into one homepage. Although I can retrieve results from each API individually, I'm facing chal ...

Retrieve the decimal separator and other locale details from the $locale service

After reviewing the angular $locale documentation, I noticed that it only provides an id (in the form of languageId-countryId). It would be helpful to have access to more specific information such as the decimal separator character. Is there a way to retri ...

The Google reCaptcha reply was "Uncaught (in promise) null"

When using reCaptcha v2, I encountered an issue in the developer console showing Uncaught (in promise) null message regardless of moving the .reset() function. Here is the console output: https://i.stack.imgur.com/l24dC.png This is my code for reCaptcha ...

The Owl carousel animation fails to work in Chrome browser

I am currently customizing an HTML5 template that utilizes the Owl Carousel 1.3.2. My goal is to incorporate a smooth fade animation when transitioning between slider images. The code snippet below works perfectly in the Mozilla Browser, however, I'm ...

Issue Alert: Inconsistencies with Google Scripts spreadsheets

Objective I have been working on a script that will make consecutive calls to an API (with a JSON response) and input the data into a Spreadsheet. Issue: When I debug the script, everything runs smoothly without any major problems. However, when I try r ...

Is Axios phasing out support for simultaneous requests?

According to the current axios documentation, there is a section that is not very well formatted: Concurrency (Deprecated) It is advised to use Promise.all instead of the functions below. These are helper functions for managing concurrent requests. axio ...

Ever tried asynchronous iteration with promises?

I have a specific code snippet that I am working on, which involves registering multiple socketio namespaces. Certain aspects of the functionality rely on database calls using sequelize, hence requiring the use of promises. In this scenario, I intend for t ...

The AJAX call in MVC 6 is receiving a 502 HTTP Error code

Recently, I've been experimenting with MVC6 controllers and action methods. One thing that caught my attention is the change in behavior when returning JSON results - there seems to be no JsonRequestBehaviour anymore. While Ajax Posts are functioning ...

Attempting to grasp the sequence in which setTimeout is ordered alongside Promise awaits

I've been puzzling over the sequence of events in this code. Initially, I thought that after a click event triggered and Promise 2 was awaited, the for loop would resume execution once Promise 1 had been resolved - however, it turns out the outcome is ...

Saving Information to a Specific Location on the Client Side in a CSV File

I am currently working on a static application that uses Angular JS technology. My goal is to write data into a CSV file at a specific path in order for another API to read the data from that location. Despite searching extensively on the internet, I hav ...

Divide information in the chart

<table id="tab"> <tr><td class="sub">mmmmm</td><td class="sub">jjjjj</td></tr> <tr><td class="sub">lllll</td><td class="sub">wwwww&l ...

Is it possible to identify images within a message sent by users to the server and provide a response accordingly

Apologies for my not-so-great English I am currently learning JavaScript and I am trying to detect an image in a message sent by users from the server, and reply with that image embedded in a bot message. However, message.content is not working for this p ...