The servlet is successfully receiving a JSON post, but unfortunately, it is not being displayed

Currently, my focus is on JAVA programming.


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("TEST TOMCAT SERVLET");
    StringBuilder data = new StringBuilder();
    String s;
    while ((s = request.getReader().readLine()) != null) {
        data.append(s);
    }
    data.toString(); //Incoming Data
}

Upon initializing the application, I see the following message in the Servlet: "TEST TOMCAT SERVLET". However, I am not receiving the following:


return fetch(PUSH_ENDPOINT, {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    token: token,
    user: 'Brent'
  })
});

}

Answer №1

Simply using the toString() method won't achieve your desired result.

Try using

System.out.println(data.toString()); //Data being received
.

Answer №2

If you need to provide a response to the client, here is an example of how you can achieve that:

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

      // Set the content type for the response
      response.setContentType("text/html");

      // Actual processing logic goes in this section
      PrintWriter out = response.getWriter();
      out.println("<h1>Hello World</h1>");
   }

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

Exploring the integration of React with Spotify's Web Playback SDK

There is an exciting new beta feature on Spotify that allows for full song playback in the browser called the Web Playback SDK. The documentation showcases initializing a player immediately using script tags in the main HTML file, requiring an access token ...

Checking the current password using AngularJS and Laravel for custom validation techniques

I initiated an angular + laravel project yesterday, but I encountered an error in angular which has halted my progress. Below is the code snippet: <div class="form-group" ng-controller="CheckPawd"> <label>Current Password</label> &l ...

Is the this.props.onChange method called within the render function?

I'm having trouble grasping the concept of the assigned onChange property in this TextField component I found in the material-ui library: <TextField style = {{"padding":"10px","width":"100%"}} type = {'number'} valu ...

Error message: Suitescript encountered an unexpected issue - TypeError: The function this.handleChange is not defined

For the past year, I have been immersed in Suitescript development. In my current project, I have a client script that triggers on Save for a Journal Entry. However, upon trying to save, I encounter an error message that reads "TypeError this.handleChang ...

What is the best method for inserting dynamic HTML content (DIV) with JavaScript?

Can someone assist me in dynamically adding HTML content when data is returned from the server-side? I am currently using ajax/jQuery to handle server-side processing requirements. The success code section of my ajax function allows me to write HTML elemen ...

Having trouble locating the Selenium element on the webpage?

I am encountering an issue with Selenium during a click event WebElement btn = driver.findElement(By.xpath("//form[@name=\"addMemberForm\"]/div[14]/div/button")); System.out.print(btn); The output of the print statement [[ChromeDriver: chro ...

Encountering a Issue while implementing caching feature in AngularJS

When I load my modal HTML page for the first time, I get the proper data. However, when I try to load it a second time, an error occurs. Below is my JS code: var cache = $cacheFactory('cacheId'); var cacheData = cache.get('xml'); if ...

Numeric keypad causing issues with setting minimum and maximum lengths and displaying submit button

I designed a password pin page that resembles a POS NUMPAD for users to enter their password. I am struggling to make the condition specified in the JavaScript function properly. Rule: Hide the submit button if the minimum input is less than 4 characters ...

Simultaneously removing and inserting components with Meteor.js

I have a straightforward set of items that is displayed like this {{#each items}} {{> item}} {{/each}} My code defines that only the three most recent items are shown return Items.find({}, {sort: {timestamp: -1}, limit: 3}) Whenever a new item i ...

The concept of tweening camera.lookat in Three.js

Struggling to smoothly tween the camera.lookAt method in Three.js using Tween.js. The current implementation rotates the camera directly to the object's position. Here is the code snippet that sort of works: selectedHotspot = object; var tw ...

What is the best way to supply JSON data to the "The Wall" MooTools plugin while feeding?

I came across this amazing plugin called "The Wall" but unfortunately, neither the documentation nor the examples demonstrate how to utilize JSON objects with it. Imagine we have a JSON array like: [ { href : "/my/photo/image1.jpg", title : "Me an ...

Using Firebase Cloud Functions to Automatically Increase Counters

Can a counter be incremented using a realtime database trigger and transaction? exports.incPostCount = functions.database.ref('/threadsMeta/{threadId}/posts') .onWrite(event => { admin.database().ref('/analytics/postCount') ...

The conflict between ESLint's object curly new line rule and Prettier's multi-line formatting

I'm brand new to the world of prettier, typescript, and eslint. Even though most of the integration is going smoothly, I am facing an issue with multi-line destructuring objects. Prettier Version 1.19.1 Playground link Input: const { ciq, draw ...

Using jQuery, selectively reveal and conceal multiple tbody groups by first concealing them, then revealing them based on

My goal is to initially display only the first tbody on page load, followed by showing the remaining tbody sections based on a selection in a dropdown using jQuery. Please see below for a snippet of the code. //custom JS to add $("#choice").change(func ...

Tips for repairing a side bar and header with CSS when using a JS & jQuery scroller

I am working on a layout design and my goal is to have the left sidebar, right sidebar, and header remain fixed in place. Here is the CSS code I am using : #container { padding-left: 200px; /* Left Sidebar fullwidth */ padding-ri ...

Tips for preventing the direct copying and pasting of JavaScript functions

Repeating the process of copying and pasting functions such as loadInitialValue(), loadInitialValue2(), loadInitialValue3(), is quite monotonous. This is the repetitive code snippet that I have been working on (when you click on "Mark as Read," the title o ...

Arranging a Table with unconventional column headings using a hash map in Java

As a newcomer in the world of Java, I have been able to successfully set up a table with regular headers using a hash map. However, I now face the challenge of setting irregular headers in the table by grouping multiple columns under one main heading. //~ ...

Attempting to create a dynamic dropdown menu with animated effects triggered by a key press

I've been attempting to construct a menu that initially appears as a small icon in the corner. Upon pressing a key (currently set to click), the checkbox is activated, initiating the animation. Most of the menu and animation are functional at this po ...

Combining numerous objects into one object within an array, each with distinct keys, and displaying them using FlatList

I'm struggling to present this information in a FlatList. Array [ Object { "-N1gqvHXUi2LLGdtIumv": Object { "Message": "Aeaaeaea", "Message_CreatedAt": 1652167522975, "Message_by_Ema ...

Utilizing Firefox and Selenium with Python: A guide to dynamically accessing HTML elements

Currently, I am utilizing a combination of Python, Selenium, Splinter, and Firefox to develop an interactive web crawler. The Python script provides the options, then Selenium launches Firefox and executes certain commands. At this point, I am looking fo ...