Writing Ajax code in JAVA - java

I want to write ajax code in java i.e. i want use the functionality of ajax without using ajax. I'm looking for some API of JAVA which can do so.
Like we post data of a web page through JAVA program similarly I want to perform operation of ajax through JAVA program.
Please suggest.

Google Web Toolkit is Java-only framework for writing AJAX aplications.

Echo is an alternative to gwt

You can use jQuery for this. In jQuery you have the great form plugin which unobtrusively changes an existing form into an ajaxform.
HTML (in JSP):
<form id="myform" action="myservlet" method="post">
<input type="text" name="foo">
<input type="submit">
</form>
<div id="message">${message}</div>
JS ((in)directly in JSP):
$('#myform').ajaxForm({
success: function(message) { $('#message').text(message); }
});
Java ((in)directly in doPost() method of the Servlet behind myservlet):
String foo = request.getParameter("foo");
String message = "You entered 'bar': " + ("bar".equals(foo) ? "yes" : "no");
if ("XMLHttpRequest".equals(request.getHeader("x-requested-with"))) {
// Ajax request.
response.getWriter().write(message);
} else {
// Normal request.
request.setAttribute("message", message);
request.getRequestDispatcher("page.jsp").forward(request, response);
}
If you want to get some steps further, you can use Gson in Servlet to convert complete Java objects to Javascript object notation (JSON). This way you can access the data the javabean-like way in Javascript.

If your application is running on browser and its a web application, you can go with GWT. If your application is a core java application.. you can simply create a HttpURLConnection and use it.

JavaServer Faces (JSF) 2.0 should be able to do partial updates of a page in place, using AJAX under the covers. It sounds to me as if that is what you need.
I think the easiest way to get a JSF 2.0 capable server running right now, is using the latest version of Glassfish.

There are plenty of Java based AJAX frameworks out there.
Apache Struts [complex AJAX-tags (by integrating DOJO-toolkit)]
Direct Web Remoting is a framework for calling Java methods directly from Javascript code.
Guiseā„¢ Framework - elegant server-side component architecture that doesn't require developers to write HTML or JavaScript
GWT - Java software development framework that makes writing AJAX applications
JAXCENT - Just-Java Framework and API for AJAX
IT Mill - Ajax framework for developing web applications for with Java language at server-side
and even more JAVA AJAX Frameworks

I suggest you take a lok at DWR - Easy Ajax for Java:
DWR is a Java library that enables
Java on the server and JavaScript in a
browser to interact and call each
other as simply as possible.
DWR is Easy Ajax for Java
It enables you to ajax-ify your web application with minimal effort.
It is not a complete new web-framework; it focuses solely on ajaxification, allowing you to keep and use your existing web framework.
If you want to use a "heavier" web framework like JSF, there exist ajax-ready JSF frameworks like IceFaces and RichFaces with provide ajax out-of-the-box.

you can submit form to servlet, when servlet response, you using jquery or prototype javascript framwork to get data from server by using Ajax call function.
I tried it and it run smoothly!
Good luck!

The quick answer is GWT. However, why would you want to do this? JavaScript is similar to Java. If you know Java, then you can easily write JavaScript code. The advantage of using JavaScript would be that you would be more versatile and would not be locked into a single tool. Using GWT to generate JavaScript (AJAX) code is not natural.

Related

Need advice on how to make an Angular app and Java as backend

I'm a not a programmer but a programming enthusiast, I have learned java (specifically - basics, some of the advanced stuff, JDBC, collections, servlets, etc. I haven't learned any frameworks such as swing, spring, applets, etc. I have created a web app as my project after I learned java with servlets, HTML, CSS, jQuery. I used no frameworks of any sought. I started learning Angular 7 and I'm in the middle of it. I used servlets to manage my sessions for client-side and was very easy to talk to using post request using jquery and receive JSON obj in return, I used JSP to render the HTML code I wanted. So I could add a bit of code in every JSP page like so...:
<%
String school=(String)session.getAttribute("school");
if(school == "" || school == null){
response.sendRedirect("index.jsp");
}
%>
...and redirect the user if the session is null. I'm wondering what a similar solution would be in Angular since angular is independent of the server.
You could manage your sessions in a handful of ways. The go to way at writing this seems to be through the use of JWT (JSON Web Token). You could setup an API service in angular that handles this using HttpClient. When it comes to redirecting there's a Router class for that in Angular.
I understand this might be a bit overwelming, don't hesitate to ask if you need any more information. :)

How to write(enable) Java code in HTML while using GWT 2.5?

I've started taking advantage of GWT 2.5. The coolest feature of GWT 2.5 was the ability to write Java code directly in our HTML pages like so:
<script type="text/java">
String helloWorld = "Hello world";
Window.alert(helloWorld);
</script>
The trick is to use a ServletFilter on the server side. This ServletFilter uses the GWT compiler to compile this java code into Javascript and inject it into the HTML returned to the client.
I am unable to figure out how to filter this in web.xml or how to pass information to the compiler. How can I do this?
Here is the Google I/O 2012 - The History and Future of Google Web Toolkit you are referring too. And here are the slides posted by Ray Cromwell.

Send notifications from Java servlet to HTML pages

I have a Java servlet from which I need to send few notifications to the HTML page before I send the final response (which is a PDF file).
Basically I need to keep the user updating on steps I am performing while generating the PDF for them.
I guess, one way could be set various attributes using HttpSession and have them printed using JS. But I don't know how to do that dynamically without loading the page again.
Any idea on how can this be done? I am kind of new to JSP and servlets.
There are two ways:
AJAX polling - you send ajax requests periodically, and the server responds. The page does not get refreshed. Check jQuery for an easy way to make ajax calls
Comet - that's an umbrella term for what you are trying to do. Usually it means keeping an open channel with the server. There are many ways to implement comet (including the option above), but in Java, on the server side, there are two things to check:
Servlet 3.0 asynchronous support
Atmoshphere - chooses the best underlying mechanism provided by the container, which includes the async support above
You should probably look forward to use some implementation of COMET (with js library of choice).
If your html code is allowed to use html5 features, you could also use websockets

jQuery client-side and Java server-side - Alternatives to JSON?

I have a project in Flex (client) and Java red5 that I would like to move to jquery for the client. I would like to reuse some parts of the Java server-side code, and use Java's jQuery support.
Are there other solutions that I could use for the communication with the server?
Yes, I've read that http://www.playframework.org is an excellent framework for building Java-based RESTful web services. This way, you can easily integrate jQuery and JSON requests with your Java web service.
This site, http://coffeebean.loicdescotte.com/2011/01/how-to-export-objects-to-json-with-play.html, has a great example of how to export content as JSON.
As for the jQuery itself, check the doc's or start with something like this:
$.ajax({
url: "your-web-service.php",
context: document.body,
dataType: "json",
success: function(data){
// do something with data
$(this).addClass("done");
}
});
JSON is probably your best bet, very simple and well supported in both Java and JQuery.
JSON keeps things simple but you could also use XML as your data interchange format. jQuery plays well with both JSON and XML. If you were hoping to do something like SOAP I suppose it could be done but I'm pretty sure jQuery doesn't exactly do SOAP out of the box.
As far as moving to PHP goes, in your situation I wouldn't bother. If your app server is running securely, solidly, and fast enough then why even consider a change?

Architecture of a Java EE web application

I want to realize the following scenario:
The frontend is based on HTML5 / CSS3 / JavaScript (with a JavaScript framework like ExtJS or jQuery). I have full HTML pages with forms, buttons, text fields, which have submit buttons which should send the data to the webserver. On the other hand I also have a website in this web application which get/send the data via a JSON webservice.
The backend should be realized with Java EE (MySQL database, Hibernate as ORM, ...). But which basic technology should I use? JavaServer Faces? Or JavaServer Pages?
What is the best method to handle JSON and HTTP GET/POST requests?
Thank you in advance & Best Regards.
Take a look at Play Framework, which has excellent JSON support, built-in JPA support and jQuery included.
Spring MVC has a very good support for rendering JSON responses and REST-like URLs. So you can use it for both your standard views and your JSON responses.
RESTEasy is a REST framework by JBoss. With it you can use anything - JSF, Wicket, etc.
Be careful not to duplicate code. Whatever solution you choose, move the code into services, and consume them from the json and regular response renderers.

Categories