Tips for accessing Java server side functions through HTML/JS and Android?

I am looking to embark on a personal project involving a Java-coded server, an HTML/JS client (with a framework yet to be determined), and an Android client.

My goal is to have the ability to call a Java method from both clients using only one implementation. What approach could I take to achieve this?

Initially, I considered using Websocket, but it appears that I can only send messages, requiring the server side to parse them and make method calls itself. While feasible, I am hoping to discover a more efficient solution.

Upon further research, I found that AJAX presents a similar challenge. A HTTP request is sent, necessitating a parser to interpret the request and distribute it accordingly.

Although servlets are capable of performing this task, they are often deemed outdated. I am unsure as to why this perception exists, however.

I am fully receptive to any suggestions for implementing seamless client-server communication.

EDIT: The ideal scenario would involve a system enabling direct method calls on the server from the client side. An optimal solution would entail the ability to call webservices from Javascript or utilizing a framework allowing direct remote calls to Java methods.

Answer №1

In today's technology landscape, one of the most common ways to provide access to server-side methods is through WebServices.

  1. Develop your business logic in Java programming language.
  2. Integrate a REST Webservice layer to facilitate communication between client requests and Java methods, returning the appropriate response.
  3. Utilize a REST library that offers servlet implementation for routing requests by adding annotations (Consider using Jersey REST api).
  4. The REST library will also handle the automatic conversion of Java objects into JSON format, typically with a library like Jackson.

If you want to learn more about setting up a REST Webservice, check out this helpful tutorial:

A Webservice functions similarly to a regular web URL, but instead of returning HTML, it provides JSON text. To interact with the service, send a standard Ajax request and interpret the JSON response. Using JQuery can simplify this process and ensure compatibility across different browsers.

  1. Initiate an asynchronous ajax request using $.getJSON(url, callbackFn);
  2. Create a callback function for handling the response.
  3. The defined callback function will execute once the response is received:

    function callbackFn(responseObj){ alert( JSON.toString( response ) );
    }

For a practical example, visit:

Answer №2

In the realm of distributed systems in Java, one common way to directly call a Java method is through RMI (Remote Method Invocation). However, this may not be the most suitable solution for your specific needs. The limitation of RMI is that it typically requires both the client and server systems to be Java-based, which may not always be the case. Additionally, RMI is not supported by default on the Android platform.

An alternative approach that is widely adopted by developers is utilizing Web Services. It is advisable to make use of JSON as the data transfer format between the client and server. This choice can simplify your workflow compared to using XML, especially when dealing with browser interactions.

Here's a valuable tip: consider incorporating the GSON library for handling the conversion/parse operations between JSON files and Java models. You can find more detailed information on how to utilize GSON effectively at this link: https://sites.google.com/site/gson/gson-user-guide

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 browser fails to render an EJS-generated page when responding to a Fetch request

Today, I encountered a strange issue that has left me puzzled. In summary, I have been sending a PUT request from a user form to the backend. Since forms do not natively support PUT/DELETE requests, I had to use a script to handle this task for me. let for ...

Unable to update FB Login button to display "Logout" after user logs in to FB app

As a junior developer, I'm currently working on integrating Facebook login functionality into my React application following the quickstart guide provided by Facebook developers. However, I've encountered some issues with making the necessary mod ...

Is it possible to create a progress bar in blue that is similar to the horizontal blue progress bar seen in Gmail when a user submits a form?

Currently, I am utilizing Bootstrap v3.3.5 on my website. In a particular situation, I have a form displayed within a Bootstrap modal dialog. The user fills in the data and submits the form. After submission, the form remains as it is until a response is ...

At the click of a button, the Edit Text data can be seamlessly written into a file as a data block

Hello, I am a beginner in Android Development and currently working on a simple application. I am facing an issue where I am unable to save a block of data from an Edit Text to a file upon clicking a Button. Here is my code snippet: saveBtn.setOnClickL ...

Remove the dropdown menu in real time, ensuring that only the most recent one is

Although the example may seem unusual, I am deliberately recreating a real problem to find a solution. When I click on a button, it generates 3 dynamic dropdowns based on a list of animals. My issue is that I want to delete a selected item, but it always ...

Can the generic type used for a type token (such as Class<T>) be accessed at runtime?

Neal Gafter coined the term "type tokens" which involve instances like Class<String>. Can one retrieve the generic type (String) from an instance of Class<String> at runtime? I am seeking a method akin to Method.getGenericReturnType(). ...

Ways to confirm input in an edit text field

I am seeking a way to implement verification for my text input: Validate that the name is not a number Ensure that the tel is a number Confirm that password and password2 fields are the same What would be the best approach to achieve this? if (name.isE ...

Unexpected Error in Vue JS: Array IndexOf is not a Function

Every time I've needed to verify if a number exists in an array, I rely on using .indexOf(), but for some reason, it's not working this time around. Vue method showSeat(seat) { if( !this.selectedSeats.length ) { this.selectedSeats. ...

Create an Ajax request function to execute a PHP function, for those just starting out

I came across a JS-Jquery file that almost meets my needs. It currently calls a PHP function when a checkbox is clicked, and now I want to add another checkbox that will call a different PHP function. My initial attempt was to copy the existing function a ...

What could be causing Typed.js to not function properly in my situation?

Having an issue with typed.js not functioning properly. Here is the code snippet: <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="js/jquery-1.11.3.min.js"></script> <!-- Include all compiled plugins ...

I am currently exploring the ExpectedConditions class in search of the onChange method

During testing of the application I am currently working on, I have observed that when I click a submit button, a grid refreshes. There is a row count available to me in a span element, and my intention was to achieve something like: (new WebDriverWait(dr ...

NodeJS closes the previous server port before establishing a new server connection

During my development and testing process, whenever I make changes, I find myself having to exit the server, implement the updates, and then start a new server. The first time I run the command node server.js, everything works perfectly. However, when I m ...

What happens when 'grid' property is undefined in modal?

I encountered an issue with my modal where I wanted to display pre-selected rows, but kept getting a 'cannot read 'grid' of undefined' error. The UI Grids are defined within the modal, and I have declared two Grid APIs with different na ...

Ways to extract values from a JSON output using comparisons

I am dealing with a JSON data structure that contains information about various teams. My task is to compare the values in the teams array with the values stored in html_content.position[i]. In this comparison, the index i needs to be dynamically set withi ...

Can the state and city be filled in automatically when the zip code is entered?

I have a form where users can enter their zip code, and based on that input, the corresponding city and state will automatically populate. These three fields are positioned next to each other within the same form. Here is an example of my form: $(' ...

Modal closes automatically after being clicked twice on data-bs-dismiss

Having an odd issue with Bootstrap 5.0.2 In order to close the modal, I have to double-click the button (data-bs-dismiss) It's worth noting that I use multiple modals on the page, but they only open when needed. <div class="modal" id="open-rat ...

Generating a New File Using a String Instance

I am currently working on a socket program that sends and receives strings. My goal is to use the received strings to create a file sent from the other side. I have successfully achieved this with text files, but when it comes to image files, the process f ...

Scroll through a particular HTML element using jQuery animation

Can an object be animated from one position to another following a specific path? For instance, if moving from pos1 to pos2, and encountering a div with the class "no-ok" in the way, the object should avoid it and find the nearest div named OK either to th ...

Exploring the power of jQuery's deferred method and utilizing the ajax beforeSend

Using the deferred object in $.ajax allows for: Replacing the success-callback with the deferred-method done() Replacing the error-callback with the deferred-method fail() And replacing the complete-callback with always() When using: var jqxhr = $.ajax ...

Error in React Router: 'Home' is undefined

UPDATE: A huge shoutout to all you awesome individuals! The key here is using import/export syntax to ensure everything runs smoothly. I'm encountering an issue where I keep seeing Uncaught ReferenceError: Home is not defined while trying to implemen ...