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

Outputting a series of exceptions from a invoked function

Currently, I am developing a Java compiler type checker. The typeChecking class is equipped with methods that throw RuntimeExceptions in case of semantic errors found in the input file (program). As of now, whenever an error is detected, the exception is ...

Sending a jQuery form to a node.js connect-form involves passing the form data through AJAX to

On the server side, I have implemented the following function as an expressjs middleware: function objCreation(req, res, next){ req.form.complete(function(err, fields, files){ if (err) { next(err); } else { // Set params in request ...

Is there a node.js equivalent to Turbolinks available?

One of my favorite features in Rails is Turbolinks, as it enhances the user experience by making pages appear to load faster. Is there any alternative or similar functionality for node.js? ...

Can I restrict access to all routes except one in vue-router? Is this a safe practice? Should I explore alternative methods for achieving this?

I am looking to create an online exam consisting of 5 pages, each with a countdown timer set at 120 seconds and 4 questions on each page. Once the timer runs out, users will be automatically redirected to the next page, or they can manually click the "next ...

Ajax-powered Autocomplete Functionality

I am looking to include an autocomplete feature for text in my web application. I have the data stored in a SQL server database table and searched Google to find out how to achieve this using autocomplete. Most examples show using a web service, but I am u ...

Utilizing fluent-ffmpeg in nodejs and express to effortlessly download a video

I've been recently tackling a side project that involves downloading videos from Reddit. The tricky part is that the video and audio are stored in separate files, requiring me to merge them before being able to download them onto the client's dev ...

Showcasing a JSON file in the interface using $http in AngularJS

I am a beginner in angularjs (and programming). I am attempting to showcase a json file on my view (home.html) using $http and ngRepeat, but unfortunately, it is not working as expected. Upon inspecting the angular responses, I noticed that there are numer ...

use javascript or jquery to conceal the textbox control

Looking to conceal a textbox control using javascript or jquery. I attempted the following code: document.getElementsByName('Custom_Field_Custom1').style.display="none"; Unfortunately, I received an error in the java console: document.getEle ...

Tips for acquiring an object to utilize in v-bind within a v-for loop

Consider this code snippet: <ol class="breadcrumb arr-right"> <li v-for="(url,name, index) in links" v-bind:class=" (index == (links.length -1)) ? 'breadcrumb-item active' : 'breadcrumb-item'"> <a v-bind:href ...

Navigate through the pages by selecting from the drop down menu, but the drop down seems to be missing on the second page

Check out this link. I'm interested in exploring each page linked to an item in the dropdown menu at the top of the website. Recently started using selenium, here's what I've been working on: Start by opening the driver Navigate to the w ...

Navigating an HTML table with the precision of a battleship commander: A guide

I have a simple HTML table that I want to parse line by line using JavaScript without any specific identifiers like class names. My goal is to create a mapping system similar to the following: AC000(red)(green) => cell match. As illustrated in the scr ...

Step-by-step guide on accessing values from a JavaScript object

I have a dictionary variable declared within a script tag on an HTML page. My goal is to retrieve the values associated with the "Roads" and "Intersections" keys, which change each time the page is refreshed. By capturing these values, I can then use JavaS ...

"Converting array into a string in TypeScript/Javascript, but unable to perform operations

After generating a string with the correct structure that includes an array, I am able to navigate through the JSON on sites like However, when attempting to access the array, it turns out that the array itself is null. Here is the scenario: Firstly, th ...

Having trouble removing just one element from an array in Redux. Any suggestions on how to make it work properly?

I'm currently working on creating a dynamic algorithm for removing an item from an array. Reducer: import { LOAD_PAGES, REMOVE_PAGE } from "./dynamicMenuActions"; export const initialState = { pages: [] }; export const dynamicMenuReducer = (state ...

Error: Ajax request is not receiving the expected data

After sending an AJAX request to my server in order to create a new entry in the database and retrieve the id of that entry, I am encountering an issue where the console is displaying 'response' as undefined. Can anyone provide insight into why t ...

Is it possible to handle both ajax form submissions and browser post submissions in express.js?

On my website, I have implemented a contact form using express.js (4.0). I am contemplating how to manage the scenario where a user disables JavaScript. If the last part of my routing function looks like this: res.render('contact.jade', { tit ...

Is it possible to trigger multiple button clicks using JQuery simultaneously?

Hello everyone, I am new to StackOverflow and this is my first question here. I hope it's not too silly. Thank you in advance for your help! I am trying to achieve a functionality where one button click triggers the clicks of multiple other buttons u ...

Issue found in factory and service dependencies

To retrieve user information, the factory sends a request to the API using ApiRequest.sendRequest: (function() { angular.module('isApp.user', []) .factory('UserProfileFactory', function( $log, ApiRequest, dataUrls ) { // User pr ...

Receiving a console notification about a source map error

Recently, I started receiving this warning in my console: "Source map error: request failed with status 404" resource URL: map resource URL: shvl.es.js.map" Has anyone encountered this issue before? I'm unsure of what it might be? This is my webpa ...

Tips for updating a column with just one button in AngularJS

On my list page, there is an Unapproved button. I am new to angular and struggling to figure out how to retrieve the post's id and update the corresponding column in the database to mark it as approved. Can someone provide guidance on how to accomplis ...