My servlet is not providing any response to Java

Here is the code on my JSP page:

<button data-ng-click="login()">Fetch data from server</button>

In the mainController.js file:

    $scope.login = function() {
    var xmlHttpReq = new XMLHttpRequest();
    xmlHttpReq.open('POST', "http://localhost:8080/WEB-war/FES", false);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/json');

    xmlHttpReq.send(JSON.stringify($scope.strings));
    if (xmlHttpReq.status === 200) {
        alert(xmlHttpReq.responseText)
    }

}

The FileEditServlet.java file contains:

@WebServlet("/FileEditServlet")
public class FileEditServlet extends HttpServlet {

 protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    System.out.println("inside do POST");

    PrintWriter out = response.getWriter();
    out.println("response");
    response.getWriter().write("do something omg");

 }

The web.xml file configuration:

    <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"                      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <servlet>
    <servlet-name>FileEditServlet</servlet-name>
    <servlet-class>servlet.FileEditServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>FileEditServlet</servlet-name>
     <url-pattern>/FES</url-pattern>
</servlet-mapping>

</web-app>

When clicking the button to fetch data from the server and invoking the login function in the mainContrller, the output is empty. The alert shows no response, and there are two mysterious words "test test" appearing in the console which cannot be located in the code.

Answer №1

Instead of just adding comments, I will provide a clearer answer.

Based on my analysis, it seems like the issue is not related to web.xml or the JavaScript portion. The key lies in ensuring that the return string test test is present somewhere in your code. I would recommend conducting a text search within your project using tools like Eclipse.

The main problem appears to be stemming from attempting to call the getWriter method from the HttpServletResponse twice. The first instance prints a line while the second one writes to the stream. To troubleshoot this, consider removing one of these calls. Here's an illustrative snippet:

@WebServlet("/FileEditServlet")
public class FileEditServlet extends HttpServlet {

 protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    System.out.println("inside do POST");

    PrintWriter out = response.getWriter();
    out .getWriter().write("do something omg");

 }

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

An issue arises in Angular JS where a field is not displayed when the data is fetched from Salesforce

During the development of a visual force page, I utilized AngularJS to create a record under a custom object in Salesforce. The Salesforce controller returned the ID to a javascript callback function, which I tried to display in a label field. However, I e ...

What is the most efficient way to retrieve the key at a specific index within a JavaScript map object?

If I have the map object shown below: const items = new Map([['item1','A'], ['item2','B'], ['item3', 'C']]) I am trying to retrieve the key at index 2. Is there a method other than using a for ...

What is the default location for the creation of FXML files in JavaFX?

UPDATE: Revised the question for better understanding By default, where are JavaFX applications storing the FXML files? If an FXML file is created, where is it located? Is it possible to specify a specific location for the FXML file to be compiled to? I a ...

Is it necessary to employ a distinct delimiter when creating a StoredProcedure in MySql?

According to the oracle java tutorial found here: When working with MySQL, statements in a stored procedure must be separated by semicolons. However, a different delimiter is needed to conclude the create procedure statement. Having utilized stored pro ...

Having an issue with transmitting information to the database using ajax and Laravel

I am currently working on creating a drag and drop list that can be sorted. Each time an element is dropped into a new area, the order of the list should change accordingly. I am implementing this using AJAX and Laravel, but encountering an error when drop ...

"Using Jest to specifically test the functionality of returning strings within an object

Attempting to run a jest test, it seemed like the issue was with the expect, toBe section as I believed that the two objects being compared (data, geonames) were exactly the same. However, upon closer inspection, they turned out to be different. The struct ...

What is causing the malfunction in jQuery version 1.7.x?

Here is a code snippet demonstrating the issue I am facing: var $div = $('<div>'); $('span').live('click', function() { this.innerHTML = 'changed'; }); $div.append( $('<span>span</span>& ...

Adjusting webpage background with JavaScript

I've been struggling with this for the past six hours and can't seem to get it right. I've checked out various solutions on Stack Overflow, but nothing seems to work. What am I missing here?!? My html5 page doesn't have a background an ...

Regular expressions or regex can be used to match the initial letter or letters of various words

Struggling with regex? After searching for an hour, the best solution found was this one. Still unable to crack it though... Here's what I need: Have a JS array that needs to be filtered. The array looks like: [ 'Gurken halbiert 2kg', &a ...

Executing Javascript without invoking the default behavior

Recently, I've been delving into the world of JavaScript and experimenting with the prevent default command for ajax pagination. Here's the code I've come up with: http://jsfiddle.net/6pqfH/2/ $('.pagination').click(function(e){ ...

New replacement for routerState.parent feature that has been deprecated in angular2

During my work with Angular 2 rc5, I encountered the following code snippet. this.router.routerState.parent(this.route).params.forEach((params: Params) => { url = params['url']; id = +params['id']; }); I had to resort to th ...

Having difficulty processing information retrieved from an ajax request in jQuery

Upon making a request to the server and converting the retrieved data into a table format, I utilize jQuery's .html() function to display it on the webpage. However, despite the data being visible on the page, I encounter issues when attempting to man ...

Transmitting Filter Choices as an Object for Retrieving Multiple Values within an Angular Application

In my Angular application, I have a function that takes user selections for various filter types and sends a request to the API to retrieve filtered data based on those selections. Each filter type returns values in an array format, allowing users to selec ...

Coming back from the setState callback

In my code, I have a scenario where the Parent component is calling a function from the Child component using refs. Within the Child component function, there is a this.setState method with a callback function embedded in it. handleSaveProject = async () ...

Error encountered in Google's Structured Data Testing Tool

<script type="application/ld+json"> {"@context" : "http://schema.org", "@type" : "LocalBusiness", "name" : "mywebsite.com", "description": "Lorem ipsum dolor sit amet", "image" : "http://mywebsite.com/image.jpg", "telephone" : "987654321", ...

Differences between Arrays of Numbers and Objects in Typegoose

Exploring Typegoose and encountering a puzzling issue. I defined a class with a field meant to store an array of numbers like this: class A { ... some other properties ... @prop({ required: true }) public nums!: number[]; } Here's a snippet of ...

How can I designate a default value for a variable within a prop in a Vue.Js component?

I have a question regarding setting a default value for a prop using a getter. props: { userID: { type: String, default: '' } } The default value I want to set is obtained through: computed: { ...mapGetters('Auth&a ...

The verifyTrue(boolean) method is not recognized for this type of data

Hey there, everyone! I'm a beginner here, so please bear with me! In my current class, I am attempting to check if the text 'Fictitious Test Company' exists anywhere on the page. If it does, I want to click and delete that company. Otherwi ...

Error encountered during automatic form submission

My current project involves creating a web notepad that automatically saves changes every minute. The notes are stored in a field called "notas" for each user in the database. Here is some of the code I am using: <html> <head> < ...

Alter the class following modifications to the list

https://i.stack.imgur.com/QZob0.pngIn my dual list, the data is displayed in a ul li format fetched from a JSON file. Users can move items between the two lists. However, I am facing an issue where I want to apply a property that only displays content with ...