JavaScript Function Causes Applet's URLConnection.connect to Fail

My website features a Java Applet that needs to connect to the server. Interestingly, it successfully connects in JApplets @Override init(), but when called by JavaScript, it fails to do so.

final URL url = new URL(SERVER_URL + action);
System.out.println("url:" + url);
System.out.println("postString: " + postString);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(!postString.isEmpty()) {
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", Integer.toString(postString.getBytes().length));
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    System.out.println("connecting...");
    connection.connect(); // same with connection.getOutputStream();
    System.out.println("connected");
    ....

The setup on my website:

<a href="javascript: document.applet.update();" title="update from server">Update</a>
<applet id="applet" archive="/public/Antucation-0.1.0.jar?random=3765332307555812156" code="de.antucation.controller.Controller.class" width="100%" height="800px">
<param name="colonyId" value="1">
</applet>

The output shows:

url:http://localhost:9000/applet/showCode
postString: colonyId=1
connecting...

I have tried putting a try-catch block around it with a System.out call, but no luck there either. However, calling

@Override public void init() { update(); }
works perfectly fine.

And yes, the applet is also hosted at http://localhost:9000/

How can I resolve this issue or find a workaround?

Answer №1

Consider trying a similar approach:

private void executeJavaScriptCall(final String parameter) {
    AccessController.doPrivileged( new PrivilegedAction<Void>() {
        public Void run() {
            // Add code here to establish the connection.
            return null;
        }
    });
}

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

The AJAX POST function is not functioning properly when clicking on contextmenus

Can someone please assist me? I am having trouble sending data via ajax post to my nodejs server from contextmenus. It is not functioning as expected. Although the ajax request does not give any error alert, the data is not being sent successfully. I hav ...

Using Regex to replace special characters in TypeScript

I need assistance in removing the characters "?" and "/" from my inner HTML string. Can you guide me on how to achieve this using regex? For example, I would like to replace "?" with a space in the following content. "Hello?How are you?<a href="http:/ ...

Styling a dynamically-injected div using an AJAX request (xhrGet)

Hello everyone, currently I am using the code below to fetch updated content after receiving a "push" event from a server. The new content is then used to replace the existing div/content. However, I'm facing an issue where none of my styles from the ...

When running scripts, Protractor is unable to perform a click action in Safari, even though it works perfectly in

Currently, I am in the process of developing an angular application and utilizing directconnect for Chrome and Firefox. All my test scripts are functioning as expected, however, a new requirement has been introduced to run these same tests on Safari. To ...

The property 'get' cannot be accessed because the axios module of vue__WEBPACK_IMPORTED_MODULE_0__ is undefined

Having some trouble with Vue and Axios - encountering the following error message: [Vue warn]: Error in created hook: "TypeError: can't access property "get", vue__WEBPACK_IMPORTED_MODULE_0__.default.axios is undefined" Here's the code snippet: ...

Exploring the process of searching for an id within an array of objects received through axios in Vue 2

I am currently working on verifying whether the user is included in the list that I retrieve using axios. However, I am facing an issue where the FILTER option I have used consistently returns undefined or [], even when the user does exist in the array. A ...

Wordpress-inspired navigation on the left

Currently, I am in search of a JavaScript library, preferably built with jQuery, that can replicate the functionality of the left menu bar in WordPress when a user is logged in. This menu features images with text, the ability to expand and collapse, a ho ...

Troubleshooting the issue of array filtering not functioning properly in Async.js

When attempting to utilize async's filter method, I am not receiving the expected result. Could someone point me in the right direction? async.filter([1, 3, 5], function (item, done) { done(item > 1); }, function (results) { con ...

Understanding the syntax for matching files and paths in Node/JavaScript using glob, including the use of wild

I stumbled upon http://gruntjs.com/configuring-tasks#globbing-patterns and found it to be the most useful reference so far. I have come across the following statement multiple times: For more on glob pattern syntax, see the node-glob and minimatch docu ...

How to access a scope variable from a script in AngularJS

I'm currently working on a project using AngularJS and TimelineJS3, but I've hit a roadblock. In my application, I have a state called timeline, which has a partial view named timeline.html linked to a controller. This state initializes a promis ...

Tips for incorporating an if statement into embedded HTML using JavaScript in order to reveal a CSS class

How can I dynamically display different classes based on the presence of data in a JavaScript array? For example, if there is responseData.title, show the class 'grey'; otherwise, show the class 'white'. This is what I am attempting t ...

Attempting to create a school project involving pizza, but encountering some technical difficulties

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>pizza ...

The React Route Component is responsible for managing all routes that start with "/", with the exception of the "/login" route

When accessing the /login route, the Dashboard component is also rendered. However, I need to exclude the Dashboard component while on the Login page. The exact attribute isn't suitable in this case because the Dashboard has nested paths such as /das ...

Replace the text in a different Div

Hello everyone, I'm struggling with an issue in my coding. Here is the HTML: <a href="aboutme.php"> <div class='aboutus'> <div id="mbubble"> stuff inside here </div> </div> ...

How to return the same value as the input value in Vue before the action is completed

When creating a component and module for functionality X while using Vuex for state management, the code initially works fine. However, after the first insertion, the Getter function consistently returns the previous input value before the action is commit ...

JavaScript counter unexpectedly resetting to zero instead of the specified value

After gathering feedback from voters: My current issue revolves around a Java counter I created. Ideally, the numbers should start at 0 and increment to the specified number upon page load. You can see an example of this functionality here: https://codepe ...

Typescript - type assertion does not throw an error for an invalid value

When assigning a boolean for the key, I have to use a type assertion for the variable 'day' to avoid any errors. I don't simply do const day: Day = 2 because the value I receive is a string (dynamic value), and a type assertion is necessary ...

The unfortunate error of 'MODULE NOT DISCOVERED' has arisen, as the module cannot be located

Currently working on resolving the pathing issues: This is how my folder structure appears: /www/html/ /database/ databaseConnection.js /scripts/ LoginPageScript.js server.js index.html In the database js file, there is the following code snippe ...

The function is failing to provide a return value

In the Game.js file, there is a function that I am working on Game.prototype.onClick = function(event){ var x = event.x; var y = event.y; if (this.blueDeck[0].contains(x, y)) alert("Blue Deck Clicked"); } The OnClick function is ca ...

What is the best way to show the previous month along with the year?

I need help with manipulating a date in my code. I have stored the date Nov. 1, 2020 in the variable fiscalYearStart and want to output Oct. 2020. However, when I wrote a function to achieve this, I encountered an error message: ERROR TypeError: fiscalYear ...