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.
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.
Share Improve this question edited Nov 10, 2009 at 7:28 Carl Smotricz 67.8k18 gold badges128 silver badges170 bronze badges asked Nov 10, 2009 at 6:52 RitesRites 2,3425 gold badges28 silver badges44 bronze badges 2- My application is a core java app. I want to submit the form by writing java code. But some of the page content is ing through Ajax. i want to that data. How can i do this with GWT? – Rites Commented Nov 10, 2009 at 9:38
- "Asynchronous Javascript" is best acplished with javascript. It's an easy tool to learn functionally, especially with a library like Prototype or jQuery. – arbales Commented Jan 12, 2010 at 23:07
9 Answers
Reset to default 8Google 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 plete 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 [plex 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 ponent 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 plete 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.