Tips for managing extended HTTP requests

I am dealing with a lengthy http request that involves a lot of computation on the back-end.

Currently, everything is synchronous, which means that while the server computer is processing, the browser does not receive any output or result. Eventually, the connection times out and a timeout error is shown in the browser.

Is there a way to send some information to the browser immediately and then have it wait for the final result? How can this be achieved?

It's important to mention that the Java back-end operates synchronously. Therefore, any solution would likely involve implementing a workaround in the servlet/front end, possibly requiring javascript requests.. ?

Answer №1

If you're looking to create a real-time feed-like experience in your web application, consider implementing the COMET pattern instead of traditional AJAX. The COMET pattern involves long-lasting HTTP requests that allow the server to push updates to the client when new data is available. You can learn more about it here.

By sending out a request and having the server hold onto it until there's relevant information to send back, you can create a continuous stream of updates that mimics a live feed.

For example, your server could initially respond with "working on it" followed by status updates like "still processing 10% complete" until finally signaling completion with "done."

Technologies like node.js excel at handling this type of functionality, but even with a synchronous Java backend, you can achieve similar results by incorporating progress indicators, possibly through a database system.

Answer №2

To avoid a timeout, simply write anything to the output stream (even just spaces) and remember to call flush().

Delaying a request can be beneficial when generating a lengthy report that will be displayed to the user as it is being created.

Instead of leaving the user waiting indefinitely, consider returning a temporary page with periodic AJAX requests to check if processing is complete. A loading screen without any updates does not provide a positive user experience.

Answer №3

To effectively manage timeouts, utilizing AJAX requests is the recommended approach. If an ajax request times out, you have the option to resend it using JavaScript (such as displaying a button). This method is considered best practice for handling timeouts.

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

Utilizing Javascript to load a vast amount of data onto the browser, followed by initiating an XML

Looking for a solution to send XML requests containing values and content from files obtained by filling out an HTML form. With 5 large files, some exceeding 70 MB, I have implemented JavaScript functions to load file contents and assemble the XML request. ...

How can we display HTML elements using a JSON object? What is the process for rendering HTML content?

Excited to finally contribute my own questions here in the pursuit of knowledge. I need some help with a jQuery GET method issue involving pulling a JSON object through an API. Here's my code: var div = document.getElementById('myData'); ...

Triggering OnClick() more than once may result in the function failing to execute as intended

My dilemma involves a table of cells where clicking a cell triggers an event. I need to dynamically add cells, so I plan to call OnClick again on all rows. But when I do so for the second time, cells that have already had OnClick called twice stop firing a ...

jQuery is producing an incorrect HTML string that includes `="` instead of just `"`

Currently, I am in the process of developing a math task web page and I am facing an issue with setting up dynamically generated buttons. The problem lies in my code generating '=" instead of " on the HTML page. function generateButton(id){ var b ...

Can ExpressJs be combined with HapiJs in a NodeJS application?

I'm currently working with an older application that is built using the HapiJs Framework. I am curious if it would be possible for me to incorporate Express for routing while still utilizing HapiJs moving forward? ...

Attempting to alter the background hue using a variable

Can't Change Color with a Variable, Console Works but Clicking on Square Doesn't Trying to use a variable named 'Color'. Even tried using an actual string value that didn't work. <!DOCTYPE html> <html> <head& ...

`Understanding Unique Identifiers in HTML: Questions on Element Naming`

Is it problematic to use a space character in an element ID? For example, like this: <li id='something with spaces'>Test123</li> I typically avoid using spaces in IDs, but I've encountered a situation where it might be necessar ...

Prevent repetitive alerts after adding two strings to an array?

Why does the program print "Enter Strings" multiple times when more than one string is entered on a single line? For instance, entering "One Two" triggers the prompt to appear twice. If I input "One Two Three," it appears three times, and so forth. Is th ...

Tips for creating a two-tier selection filter system

I'm having an issue with this code. My objective is to create two filters for this table. The select element with id="myInput" should determine which rows appear in the table and apply the first filter. Here is the JavaScript code: function myFunctio ...

Utilizing an Async API call from a separate page and passing it as a component input

I recently set up an asynchronous API fetch in one of my .JS files and then invoked it from another JS file to output the result using console.log. (Is there a more efficient method for achieving this?) Now, my aim is to utilize the fields of the response ...

The functionality of window.history.pushState does not automatically refresh the pages

When using window.history.pushState() to change the URL of a page and add it to the history list, everything works smoothly. However, I am encountering an issue. For instance, if I execute an operation (ajax) on a page that updates values in the database a ...

The Vue3 counterpart of vNode.computedInstance

I'm currently in the process of upgrading my app from Vue2 to Vue3, but I've hit a roadblock when it comes to dealing with forms. For my form elements, such as FormInput, FormTextArea, and FormCheckbox, I have been using Single File Components ( ...

Transform JSONB arrays from Postgres into a JavaScript array containing individual JSON elements

Having some challenges with postgresql(9.4) and javascript. I'm attempting to convert a jsonb[] datatype into an array/list of JSON values in javascript, containing only one element(json) for simplicity. var s = { "player": "{\"{\\&bso ...

Having problems with the MongoDB save() function when it comes to an array of objects

This piece of code is designed to handle post requests for sending messages between users. It works well when the message is not part of an existing message string (i.e., typeof existingMessageIndex === 'undefined'). The error ("error saving outg ...

Tips for dynamically loading JSON data in Pug and JavaScript by implementing scroll functionality

This is my pug file for specificBike in allBikeData .bikeshop-card.border.border-dark.bg-white.m-3.p-4 li img.shop-image.border.border-dark(src=specificBike.image, onerror="this.onerror=null; this.src=&a ...

Guide on selecting the radio button using Selenium WebDriver

I am encountering an issue where I cannot click on or get the text of the radiobutton using xpath and id simultaneously. Can someone please assist me with performing the above operations? <section class="col-md-12 mb5"> <div class="well well ...

(developing) The issue with dynamic URLs is that they are unable to properly display the desired data

I am so close to getting it to work, but I am still struggling to send the data to myself. My data component is set up to display a list of blog posts and my router is targeting the index and title //Blockblock.js const blogData = this.state.blogDa ...

How come my express POST endpoint delivers a 404 error when the Content-Type is specified by the requester?

My express server configuration is: import express from "express"; const app = express() const port = 3000; app.use(express.json()) app.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); res.h ...

Show a compact graphic in the upper-right-hand corner

Hey, I have this interesting idea but CSS isn't my strong suit. Any thoughts on how to achieve it? I'm looking to create a new class that, when applied to an item (like a div), displays a small clickable pre-defined image in the Top-Right corne ...

Saving data before the activity is destroyed and then retrieving it afterwards

Display ActivityA with two buttons. Each button will open ActivityB, but with a different fragment that contains an EditText. When switching between fragments in ActivityB, it is necessary to return to ActivityA and press the other button. My goal is to s ...