Sending JavaScript variables to an ArrayList in a Java servlet

For instance,

Let's say we have the following table:

<table>
<tr>
<td>john</td>
<td>doe</td>
</tr>
</table>
  • This table can be dynamically generated.

I am extracting values from the table using this code snippet:

table.find('tr').each(function(i){
    var $tds = $(this).find('td input');
    var a = $tds.eq(0).val();
    var b = $tds.eq(1).val();
    alert(a +" " b);
}

Is there a way to store these values in an ArrayList or is it possible to use an ArrayList for this purpose?

Answer №1

let updatedArray = [];

table.find('tr td').each((index, item) => {
    updatedArray.push(item.val());
})

After creating a new array in JavaScript, you have the option to send the values to the server side using $.post method.

Answer №2

A crucial distinction to make is that while JavaScript operates on the front-end triggered by events or during loading (based on its design), Java functions and executes tasks on the server side (back-end). This means that when Java completes its processes, JavaScript has not yet commenced.

The primary role of JavaScript is to engage with users where Java falls short.

To convert to an ArrayList, access to the server is imperative. This can be facilitated through Ajax.

Below are some useful links pertaining to Ajax:

  1. http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

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

What is the process for transmitting information from an express server to a client in Next.js?

I am working on a Next.js web application that uses Express. My goal is to post data to the webpage, fetch it on the server, and then pass it on to my Next.js page. Since I am relatively new to Next.js and Express, I have included my server.js code below: ...

Mapping JSON data with an elastic search cluster can be challenging, especially when dealing with a dynamic number of fields

I am currently developing an application where I need to map JSON data for storage in Elasticsearch. The challenge is that the number of fields in the JSON data is dynamic. How can I handle mapping in this scenario? Mapping Snippet var fs = uploadedFiles ...

What are the steps to incorporate the latest version of Apache Tomcat in setting up a new server?

Following the steps outlined in Jaime's Ideas to add a new Tomcat server version 7 for setting up a new server. Unfortunately, the recently added Tomcat server version 7 is not showing up when trying to configure the new server. Seeking assistance i ...

The remote object does not support the RMI method: encountering a java.rmi.UnmarshalException due to an unrecognized method hash

Encountering an unusual issue in my Bank-Client application utilizing RMI (Remote Method Invocation). The Bank class implements IBank extending Remote interface. This class includes methods like checkClient and getBankAccount. While the checkClient metho ...

Issue with inline Javascript not functioning correctly when partial is rerendered in Ruby on Rails version 3.1

I am facing an issue in my project where inline JavaScript in a partial, using some instance variables, does not run when the partial is rerendered after a successful ajax call. Could someone please provide guidance on how to solve this problem? For exam ...

Finding the Modular Reciprocal with JavaScript

I am attempting to find the value of d by solving the equation ed ≡ 1 mod((p-1)(q-1)), similar to the RSA algorithm. Given e = 5 and (p-1)*(q-1) = 249996 I have experimented with various Javascript code snippets, such as: function calculateModInverse( ...

Isolated directive with two-way binding in Angular, but the property is not defined?

Confusion arises when trying to understand the concept of two-way data binding. Let's take a look at the code snippet below: .directive('mupStageButtons', function() { return { transclude: true, template: '<span ...

What is the best way to trigger ajax when the user clicks the back button to return to the previous webpage?

Check out my code snippet below: HTML Code <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="body"> <div class="dropdown_div"> <select id="q_type" class="dropdown" ...

Automatically save user input in form fields with the help of jQuery and AJAX technology

I'm working on a form that has various input fields, and I need the data entered by the user to be automatically stored in the database every minute. After the user submits the request, it will be processed in a struts file where database interactions ...

The function queryDatabases is not supported in the DocumentDB JavaScript API

Currently, I am developing a service for Azure Functions using JavaScript/Node.js. However, I encounter an error when trying to access the function DocumentClient.queryDatabases. Despite having the correct references installed in Visual Studio Code and bei ...

Best practices for handling http requests in each individual route of AngularJs

Just starting out with AngularJs and attempting to set up multiple routes with different $http requests. I'm facing an issue where the page content loads late after a route change. I've come up with a workaround, but I feel like there might be a ...

The body in Express is set to "Cannot GET [route]" during the execution of asynchronous code

I am currently working on an express application that includes some articles stored in a Mongo database. As I wait for the mongoose model Article to load, the body of the request gets changed to: <!DOCTYPE html> <html lang="en"> < ...

Struggling to locate the element using xpath on a JavaScript enabled website in Selenium with Chrome

I have been attempting to extract data from a website that utilizes Javascript. Despite researching similar inquiries on xpath in Selenium, I have not found a solution. I initially tried using requests, but as the JavaScript doesn't load completely, I ...

Encountered a glitch while trying to test the application on an Android device following the connection to Android Studio

After following the steps outlined in the tutorial on connecting to wifi, I successfully linked my phone to my desktop wirelessly through the router. However, while I can test most applications wirelessly, I am encountering issues with those that require J ...

Adding a clickable button to execute code within a LeafletJS marker!

I'm currently experimenting with adding a button inside a pointer that will log a message to the console. This is simply a test to see if I can execute a method on the marker, but so far I haven't been able to display any text. const marker = L.m ...

Preventing a user from accessing the login page if they are already logged in using Reactjs

I need assistance with implementing a "Login Logout" module in Reactjs using the nextjs framework. My goal is to redirect users to the "dashboard" page if they are logged in (email set in cookie). However, I am encountering an error with the following co ...

Creating a dynamic word cloud in D3: Learn how to automatically adjust font sizes to prevent overflow and scale words to fit any screen size

I am currently utilizing Jason Davies' d3-cloud.js to create my word cloud, you can view it here 1. I'm encountering an issue where the words run out of space when the initial window size is too small. To address this, I have a function that cal ...

Is there a way to dynamically import a JSON file within an ECMAScript module?

Currently, I am attempting the following in my code: let filePath = '../../data/my-file.json' import inputArray from filePath assert { type: 'json' } The outcome of this operation is as follows: file:///.../script.mjs:5 import inputArr ...

Arranging the table based on the values of the key

I have already discovered that using the sort function is necessary to sort the table onClick. My goal is to sort by the key so that I can avoid repeating the code to sort the function by key. I am trying to keep my code DRY. How can I achieve this efficie ...

Is it possible to include the description from the name link using a CSS selector?

Here is the updated code. I have made changes to the output format as requested: {  "name": "XYZ",  "date": "29/11/2021",  "description": "XYZ xyz xyz" } package com.company; impo ...