Transferring information from a Java file to AJAX

I have a Java class that prints progress as 1%, 2%, 3% of a process. Now, I am looking to utilize AJAX requests to read this progress and update it continuously on the webpage. Unfortunately, I cannot convert the Java file into a servlet since it is being used as a scriptlet by Jasper.

The scriptlet code is as follows:

public void afterDetailEval() throws JRScriptletException{
     count = (Integer) this.getVariableValue("count"); 
     out.println(count);
     Thread.sleep(200); 
}

How can I retrieve the data printed by Java through an AJAX request?

Any assistance on this matter would be highly appreciated!

Answer №1

How can you create your own scriptlet? If you have a class like this in mind:

public class YourScriptlet extends JRDefaultScriptlet {
    public void afterDetailEval() throws JRScriptletException {
        // Add your code here
    }
}

You can enhance it by adding a constructor and a private member for another object that can store data outside of the Jasper context, like this:

An example object to keep track of certain values:

public class YourInfoObject {
    private final AtomicInteger count = new AtomicInteger();
    public int increment() {
        return this.count.incrementAndGet();
    }
    public int get() {
        return this.count.intValue();
    }
    public void set(int value) {
        this.count.set(value);
    }
}

Your updated scriptlet class with a constructor:

public class YourScriptlet extends JRDefaultScriptlet {
    private final YourInfoObject obj;
    public YourScriptlet(YourInfoObject obj) {
        this.obj = obj;
    }
    public void afterDetailEval() throws JRScriptletException {
        // Add your code here
        obj.set(count);
    }
}

Now, from anywhere else (e.g., a Servlet with a reference to your object), you can access the stored value.

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

Updating the @mui/x-data-grid table dynamically upon fetching new data

Seeking assistance regarding updating data in the DataGrid component from the @mui/x-data-grid module within a React application. Specifically, I am facing challenges in refreshing the table after retrieving data from an API using react-query. Despite succ ...

What could be the reason for a jQuery script failing to execute when included in a PHP include statement from a different PHP page?

I'm currently working with javascript and php to manage cookies across different pages. On one page, I have a script that sets a value in a cookie variable and I want to retrieve that value on another page. Let's say the first page is named page1 ...

Dynamic form submission updates webpage URL

I'm working with an ajax form that looks like this: @model Site.Models.ChangeModel @using (Ajax.BeginForm("ChangePassword", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId="result" }, new { @class = "form-horizontal", role = "form", ...

Ways to stop users from inputting incorrect characters into an input field

I need help with restricting user input in a text field to only allow letters, numbers, and dashes (-). For alphanumerics only - "The alphanumeric character set includes numbers from 0 to 9 and letters from A to Z. In the Perl programming language, the un ...

Vue seems to be experiencing some difficulty displaying the data

I am facing an issue with loading data from firestore asynchronously. Although I can see the data in the log, Vue is only displaying the initial value before the data is loaded. I have tried calling the loading method both using beforeMount() and from a c ...

A guide on retrieving real-time data from PHP using Ajax

Being new to Ajax, I am struggling to grasp how to retrieve changing variable values from php. Below is the code snippet that I have been working on: <?php $pfstatetext = get_mypfstate(); $cpuusage= cpu_usage(); ?> <div id="show"> <c ...

Choose Your Price - Price Grids

I need to incorporate two sets of pricing columns into a website. The first set is for regular car cleaning prices, while the second set is for larger cars with slightly higher prices. My idea is to have two buttons at the top – one for regular cars and ...

Experiencing recurring freezing issues with my Java JFrame when inputting text into a JTextField

I am experiencing an issue with my JFrame freezing after entering text into the "Username" and "Password" JTextFields. Can someone please review my code, identify the problem, and suggest a solution? public class Main { public static void main ...

Utilizing classes and objects to generate a unique createID

Can someone assist me with this assignment I'm working on? It is requesting to, "Prompt for and read the user's first name and last name separately. Then, create a string that consists of the first letter of the user's first name, followed b ...

What are some ways to conditionally implement dynamic imports?

Currently, I am working on a project that involves Reactjs + Nextjs. I am facing a situation where I need to dynamically import a component based on certain conditions. For example: const importMyComponent = isLiveBlog => ({ image: isLiveBlog ? i ...

Encase Regex Matches from the Inner Text of an Element in HTML Tags

I have a div element with some content inside. I am trying to use regular expressions to target specific parts of the text, wrap them in span elements with a class attribute "highlight-yellow" and add a custom attribute called my-custom-attribute="hello". ...

What is preventing Java WebDriver from opening Firefox with the latest WebDriver extension?

Encountering an issue where WebDriver in Java is causing problems while running browser tests in Firefox 28. The problem arises when Firefox launches successfully but instead of loading the first URL, it hangs on a blank page. After extensive research, it ...

Test an express + sequelize server using chai-http ping command

Currently facing challenges while setting up tests using Express and Sequelize. The testing framework being used is Mocha + Chai. Initially, only a ping test is being attempted. The code snippet from server.js: const express = require('express&apos ...

Create a function that reverses the process of removing mesh

I have a function that creates mesh when you click on another. Upon clicking, 2 or 3 meshes are created and move to their positions. Now, I want to implement the reverse function: when the meshes are deployed and you click on them again, the previously cre ...

I am dynamically generating table rows and populating them with data. However, I encountered an issue with retrieving the data by their respective IDs

Creating a table row dynamically by populating it with data. However, encountering an issue with retrieving the data by their respective id. if(xmlhttp.status == 200) { var adminList = xmlhttp.responseJ ...

Learn how to incorporate a HTML page into a DIV by selecting an Li element within a menu using the command 'include('menu.html')'

I have a website with both 'guest' and 'user' content. To handle the different menus for logged-in and logged-out users, I created menuIn.html and menuOut.html files. These menus are included in my MainPage.php using PHP logic as sugges ...

Combining Material UI with React Form Hook to handle multiple checkboxes with pre-selected defaults

I am working on creating a form with grouped checkboxes using react-form-hook and Material UI. The checkboxes are generated dynamically through an HTTP Request. I am aiming to set the default values by providing an array of object IDs: defaultValues: { ...

The iisnode encountered an issue with HRESULT 0x6d resulting in a status code of 500 and a substatus of 1013

Despite trying multiple solutions, none seemed to work for my Node.js website hosted on Windows IIS with iisnode. Everything was running smoothly until today when I encountered an interesting situation. Let's say my domain is cdn1.site.com and it&apos ...

Utilize features from both angular-strap and ui-bootstrap to enhance your project

Is it possible to efficiently utilize components from both angular-strap and ui-bootstrap without encountering conflicts that could potentially disrupt the application's functionality? For instance: Opt for a customized version of ui-bootstrap that ...

Insert fixed text before or after the input value without altering the value itself

Is it possible to prepend or append additional text to an input value? I am looking to customize the display of a value without altering the actual value itself. For instance, if the value is 1000, I want it to still return 1000 when accessed. <input ...