views:

376

answers:

10

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.

+8  A: 

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

Marcin
A: 

http://home.eekie.net/node/1271

Xinus
Nope, I don't think this is what he's asking for. As I understood it, the asker has a Web app that he'd like to give some Ajax capability without having to mess with JavaScript.
Carl Smotricz
A: 

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.

Ralph Stevens
My application is a core java app. I want to do submit the form by writing java code. But some of the page content is coming through Ajax. Can I do this GWT?
Ritz
Yes, GWT is easily up to this task. And I disagree with Ralph about JavaScript being similar to Java. It's a nightmare to code it for someone used to working with classes and objects.
Carl Smotricz
Java is definitely a better language overall, I can't dispute that. However, recently JavaScript has become so widespread because of AJAX and dynamic websites that it would definitely be worth learning. Technologies such as JQuery make it easier and fun to work with. Using GWT ties you down and makes you helpless when you want to do something it does not provide.
Ralph Stevens
-1 for suggesting switching languages.
Thorbjørn Ravn Andersen
I think I confused people with my answer. I am not suggesting anyone to switch languages. Exactly the opposite. AJAX is JavaScript. GWT outputs JavaScript. If you are using AJAX, you are using JavaScript. Why not learn to write the JavaScript yourself? How is that switching languages?
Ralph Stevens
Recommending to write all ajax code from scratch is a bad idea. There exist many mature ajax frameworks containing years of work wich level of quality and functionality you will never achieve on your own.
Sylar
A: 

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.

Abdel Olakara
Its a core java application. Using HttpURLConnection doesn't works properly if page content is coming through Ajax
Ritz
My application is a core java app. I want to submit the form by writing java code. But some of the page content is coming through Ajax. How can i do this with GWT?
Ritz
+1  A: 

Echo is an alternative to gwt

Maurice Perry
A: 

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.

Thorbjørn Ravn Andersen
A: 

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.

Sylar
A: 

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

TuxGeek
A: 

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!

misamap
+1  A: 

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.

BalusC