My software consistently triggers the default servlet

I'm encountering an issue with calling a servlet and I could use some assistance. Here is a snippet from my web.xml file:

<servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/test/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>ajaxServlet</servlet-name>
        <servlet-class>org.finki.exercise.servlet.AjaxServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ajaxServlet</servlet-name>
        <url-pattern>/ajaxServlet/*</url-pattern>
    </servlet-mapping></servlet>

I have a jsp page for testing purposes, where I am attempting to trigger the servlet through ajax

<a href="#" onclick="loadXMLDoc('eva')">proba</a>

Ajax function

function loadXMLDoc(value1)
            {
                var xmlhttp;
                
                var url="ajaxServlet";
                if (window.XMLHttpRequest)
                {
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        
                        document.getElementById("mid_title").innerHTML=xmlhttp.responseText;
                    }
                }

                xmlhttp.open("GET", url+"?url="+value1, true);
                xmlhttp.send();
            }

The loadXMLDoc function triggers the dispatcher servlet

http://localhost:8097/mavenproject1/test/ajaxServlet
.
How can I trigger the ajaxServlet -
http://localhost:8097/ajaxServlet
?

Answer №1

ajaxServlet does not include a leading /, which means it is considered a relative path. This results in sending a request to ajaxServlet from

http://localhost:8097/mavenproject1/test/foo
being directed to
http://localhost:8097/mavenproject1/test/ajaxServlet
.

To resolve this, you must add the leading /. However, simply adding the leading slash is not sufficient. You also need to include the context path of your application (/mavenproject1). In a JSP page, you can automatically achieve this by using JSTL tag library as shown below:

var url= "<c:url value = "ajaxServlet" />";

Answer №2

The reason for the issue is that you have opted for a relative link which starts with the term ajaxServlet. A better alternative would be:

var url = "../ajaxServlet";

Alternatively, if you have access to JSTL:

var url = "${pageContext.request.contextPath}/ajaxServlet";

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

Transmitting JSON AJAX response to populate location markers on Google Maps

When a button is clicked, an AJAX response is triggered to display JSON data based on a search query. My goal is to take the all_locations variable from the AJAX response and use it to show markers on a Google map. I'm uncertain about how to achieve t ...

A way to effectively utilize a variable containing information retrieved from mongoDB is to pass it to another file, enabling access to the stored data within that variable

Is it possible to utilize a variable containing data from mongoDB in a different react component? This would allow us to access the fetched data from mongoDB stored in that variable. Here is the code for the file where data from mongoDB is fetched and sto ...

A guide to traversing a class and pinpointing each element that contains a particular classlist property

Here is my code snippet that generates 4 spans within a class. When a user clicks on a span, it changes color to indicate that it has been clicked. <head> <style> .tagger1010 span { padding: 6px 10px; ...

Conceal part of the page initially, then reveal it when clicked

After integrating Rails 3 and JQuery, I encountered an issue where a div containing a rendered partial would not hide when attempting to do so using JQuery in my JavaScript. I want to be able to hide the div initially, then show it upon clicking a button. ...

Why does the method Array.concat on an extended class remove empty values in Node.js version 8.9.3?

I am experimenting with adding new functionality to Arrays in node.js without altering the original Array class to avoid any unintended consequences in other modules. During testing, I encountered some failures which led me to discover that the behavior o ...

Is SWR failing to provide outdated data?

My understanding was that SWR should display the cached data upon page load before refreshing with new information from the API. However, in my Next.js app with a simple API timeout, the "loading" message appears every time due to the 5-second delay I adde ...

Restricting the number of characters allowed for text messages and keeping track of the count

I am attempting to implement a character limiter for an html textarea using JavaScript. Additionally, I want to include a total character counter. Unfortunately, the code I have written isn't functioning as expected. Can anyone identify where my mist ...

What is the significance of declaring a constant array in JavaScript?

Does declaring an array as a constant in JavaScript prevent it from changing size, or does it mean that the values inside the array cannot be modified? handleClick(i) { const squares = this.state.squares.slice(); squares[i] = 'X'; this.setState( ...

What is the best way to transfer data from a custom method and use it in another method within my constructor function in Javascript?

As I pondered a suitable example, I devised a constructor function centered around precious metals. This function accepts the type of metal and its weight as parameters. Within this constructor, there are two methods: one to verify if the precious metal (g ...

Tips for improving the efficiency of the find_average_number(array) simple function?

I successfully completed a CodeWars challenge where I wrote the function find_average to calculate the average of numbers in an array. However, I now have some questions: 1) How can I optimize this code in terms of Big-O notation? Is there a way to reduce ...

Using Paper Checkbox to Toggle the Visibility of a Div in Polymer

Having experience with Meteor, I typically used JQuery to toggle the display of a div along with paper-checkbox: HTML: <paper-checkbox name="remoteLocation" id="remote-chk" checked>Remote Location</paper-checkbox> <div id="autoUpdate" clas ...

Deleting a segment of content from a webpage

Currently, I'm in the final stages of completing a blackjack game. However, one aspect that I haven't implemented yet is the ability for the user to play again after finishing a round. My initial idea is to use a window.confirm dialog box, where ...

Rails 4 - Functional JS errors are absent yet JavaScript functionality is not operational

Currently, I am delving into the world of Ruby on Rails and making an effort to grasp the concept of the asset pipeline. As a means to learn more effectively, I decided to construct my website using Rails and learn along the way. However, after integrating ...

Ways to retrieve the previously selected option in a dropdown list

I am working with a form that includes a dropdown list featuring options such as apple, gova, lemon, and more. Each option has an associated counter - for example, apple=5, gova=3, lemon=6. If I initially select apple from the dropdown, the count for apple ...

Tips for dynamically updating values when input numbers are modified using JavaScript

Check out this amazing tip calculator on netlify. I successfully built it using html, scss, and javascript without relying on any tutorials. Despite taking longer than expected due to being a beginner, I am proud of the outcome. Now, I need some assistanc ...

Determine the prior location of an element using jQuery

Is there a way to track the previous location of an element before it is appended? I have 50 elements that need to be appended to different targets based on a certain condition. How can I determine where each element was located before being moved? $(&a ...

I have noticed that the baseline of a Span element has shifted after updating my Chrome browser to a version that begins with

Once I updated to chrome Version 108.0.5359.94 (Official Build) (64-bit) from 107.0.5304.87 (Official Build) (64-bit), the behavior of the span element changed drastically. It shifted its baseline when multiple spans were stacked on top of each other. Exp ...

How to toggle the selection of all checkboxes in an AngularJS ng-repeat object array

Check out this Fiddle Example In my table, each row contains a checkbox and there is also a checkbox to check all rows and send the ID of the selected/all rows as JSON object. I have an array of objects from the server-side response (with pagination enab ...

NodeJS - The function app.listen is not defined in this context

I've come across a similar question before, but the answers provided didn't help me resolve my issue. The error message I'm getting is "TypeError: app.listen is not a function"; Here's my full code below. Thank you in advance for your ...

Using a CSS style to modify a class based on another class at the same level in the hierarchy

I am working with a jQuery carousel that is adding an 'active' class to images within a div. Within the same div, there is also a span with a 'fade' class that has a CSS style of opacity: 0. Is there a way to change the CSS style of the ...