Java: How do we call java methods from Javascript - javascript

For most of time I have worked on the server side using Java, Spring, Hibernate and databases. But now I have decided to learn front end programming using HTML5, CSS3 and Javascript.
I have a simple and elementary question here. How do we invoke java methods from java script ? For example on click of a button or to perform some validations on the server side.
I understand that we can perform ReST calls using Jersey. Are there any other ways to make a call to Java from JavaScript. Since I am a beginner here, I don't want to use Ajax, Servlets or JSP for now.
I have already built a simple application using SWT: SWT: Table Resizing Issue. I am thinking of using this opportunity to learn web programming.
I have already tried searching for answer on StackOverflow but couldn't find one. I hope that I am not duplicating any question.
Thanks.

If you are building a web application that run on the browser (because JavaScript can be also used to build desktop application: NodeJS, Electron…), so everything going from the browser to the backend server should be a http request, this is how the web work. Server Side framework is just doing the job of behalf of you by transforming backend code (Java, .NET, PHP …) to Browser language (html, css and JavaScript) and then performing server call using Http methods (Get, Post …).

Related

HTML Javascript and Java runtime communication

My group has a project that is upposed to connect a web page to a databse. the webpage is written in html, css, and javascript, and the database is written in mysql. we know there are issues with accessing databases from javascript, so we built a driver class from java that is supposed to control communication between the two.
the problem is that we do not know how to connect javascript with java during runtime. the way it is built, the javascript simply needs to call a java function and get the results. is there any simple way to do this? perhaps a certain library? we have little to no knowledge of other similar tools (i.e. jsp, xml) and would like to stick to the languages listed above.
You can create a Java HTTP JSON server and then make a call to it from Javascript by using AJAX calls.
See here: Java simple HTTP JSON server
In order to provide easy access to java code(including static/dynamic web content) over HTTP interface you've to use one of the (servlet) containers - jetty, tomcat, spring-boot, etc.
In my opinion, since you've minimal experience developing/deploying java web applications jetty might be good option as it have good documentation to get started. Spring Boot is popular choice for Enterprise web development using Java - it might be more than you want to chew for the moment.
The Servlet(web) and JDBC(database) APIs what you will end up using on Java side.

Use Java Library in Javascript

I am planning a project where I will develop bots for Agar.io by using the NEAT algorithm, which is implemented in Java (this is called ANJI). I will use the open-source Agar.io server called Ogar, which is implemented in Javascript. The Javascript implementation should communicate with the Java library to evolve the bots while the game is running, but I have no previous experience of how to make Java and Javascript communicate. I have considered using Rhino, but it does not seem like it is possible to use in a browser. So, are there any way to let a Javascript implementation like Ogar use a Java library?
using the Rhino / Nashorm is not relevant in this case and as for performance focus its should be much hard to run nodeJS based application via Java JS engine.
the Agar.io server is a NodeJS server that expose a WebSocket API and the client solution need to communicate this way.
Using spring websocket client for example
have you seen Agar.io-bot ? its seems like the way they implement the BOT is javascript browser extension and communicate via the browser.
In common, Javascript and Java library communicate using Rest and Json, Java provide Rest API and Javascript send Json request to this API.
This is list of java library: for developing Java Rest API and working with JSON. I recommend using RESTEasy.

Is JavaScript able to substitute php?

I have a friend who started learning website development and he's into php now. I, on the other side started studying Javascript. He asked me if it's possible to use JS instead of php code entirely?
Well, I started learning JS because someone told me, it can be used for both frontend, backend (via NodeJS) and the database (via MongoDB). Though, since I' m a beginner in this field, I'm not sure in the answer, and I'm interested anyway, so I ask you: is it possible?
He asked me if it's possible to use JS instead of php code entirely?
Yes, it's entirely possible. There are several server-side JavaScript options. Probably the most well-known is NodeJS (usually, but not necessarily, plus Express). But Microsoft has always supported JScript (their dialect of JavaScript) in classic ASP, you can use JavaScript in the Java JVM (via Rhino or Nashorn) and thus use JavaScript in servlets, for years there was SilkJS (built on Google's V8) and now there's its successor DecafJS (built on the JVM), etc.

Single Page Application - Should I use MVC Framework on the server side?

I'm developing a SPA application and I can't decide, whether to use server-side MVC framework or not.
I have chosen AngularJS as the client-side framework. I was thinking I could use Symfony 2 on the server side, but I also considered using pure OOP oriented PHP. Something like this - database class, model classes (User, UserDbManager, ...). I would use something like Slim to create the REST API.
It seems counterproductive to basically write the same code twice, once on the client side and once on the server side, if I could just use the server to serve the data from the database.
If anyone had done similar decision, I'd be happy, if you'd share your thoughts and tips about it.
I would decide depending on the complexity of the requirement.
If it's a very simple I don't see a huge difference in using a framework or plain old php.
But if it's a complex project, I would definitely go with a framework considering the fact that most of the core functions one might need are already there and quicker development.
Again, it would come down to personal preference too.
Codeigniter has been said easy to learn for beginners in framework.
Symfony and zend are more commercially used (I am not saying others are not, but I see more vacancies asking for symfony or zend development experience).
My suggestion will be if you are having angular js as clientside framework. Then take advantage of it and use some api on server side to just return some json kind of response. Why send html on network when you have angular js on clientside to work with dom. just send data from server to client.
I have also done same. I have jquery for clientside and i am using spring mvc to send response as json.

rapidly create webpage front end

Exposition:
I'm familiar with C/C++/Scheme & OpenGL. I have this client/server program written in Clojure that serves as a todo-list/wiki/forum. The server is written in Clojure. The client is written in Clojure and uses SWT.
Question:
I want to rapidly create a web client version of this. What is the best way to do this? (I Don't use anything complicated, like OpenGL rendering in SWT; just standard widget stuff) I just want something accessible via Safari/Firefox -- and no, I don't want to run it as a Java Applet.
Thanks!
Take a look at Ruby on Rails, allows you to create very basic CRUD applications quickly. Personally I am looking more a Grails a very similiar framework but for a Java platform
Ruby On Rails - http://rubyonrails.org/
Failing either of those PHP is a good alternative and very quick to get started with
Adobe Flex/Actionscript is a pretty good approach for applications that require rich user interaction.
Google Web Kit allows writing of Java code (since server is in Clojure) to create AJAX api.

Categories

Resources