aspose file tools*
The moose likes Beginning Java and the fly likes Simple Question - AJAX and JAVA, Is It Possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Question - AJAX and JAVA, Is It Possible?" Watch "Simple Question - AJAX and JAVA, Is It Possible?" New topic
Author

Simple Question - AJAX and JAVA, Is It Possible?

James Bukowska
Greenhorn

Joined: May 10, 2007
Posts: 9
Hello all,
my first post...

I have been asked to implement an AJAX application and have run a few tests using JavaScript which works as I expect... See below...



Is it possible to do the above solely in Java using HttpServlet or something similar? e.g. completely eliminate the need for JavaScript and do everything on the server?

Thanks for help! Much Appreciated!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
Welcome to JavaRanch.

Of course you could submit a form to the server (where it is handled by servlets or some other server-side technology), and receive a page back. That's how the classical web app works.

But the point of AJAX is that the complete page is not reloaded. Rather only some parts of it are updated, and this happens through JavaScript and XmlHttpRequest. Without that - no partial updates, and no AJAX.


Ping & DNS - my free Android networking tools app
James Bukowska
Greenhorn

Joined: May 10, 2007
Posts: 9
Thanks, I haven't worked with AJAX yet so querying the methods I could use... The server is SOAP so I know that is one implementation.

Since live partial updates are not required at this stage, I'll start by going down the JAVA route...

I'll bear this in mind for the future (next week)

Thanks again
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
Since live partial updates are not required at this stage, I'll start by going down the JAVA route...


There's a disconnect here somewhere. Java is involved on the server side, for which it doesn't matter whether the access is a plain old HTTP form submit, or a JavaScript XmlHttpRequest. Servlets can handle both scenarios.

Unless, of course, you're talking about Java on the client -i.e., applets-; but I don't think you are.
James Bukowska
Greenhorn

Joined: May 10, 2007
Posts: 9
Hi, sorry, I wasn't being very clear...

I thought I should reply and say thanks, in doing so I didn't really think about what I was saying...

My employer wants everything done using JAVA, a form posts to a jsp page which then calls various functions in several java classes, setProperties and so on...

I'm not really sure about the best way of going about this so just experimenting...

If JavaScript is out of the question then that would completely exclude AJAX as a solution as far as I can see.

That leaves? Forgive my ignorance on this subject, I can pick up most things without any examples (no one else here seems to know enough for this type of thing) I'm stabbing in the dark with the knowledge I have and learn through trial and error.

I thought I would be able to directly request the page using JAVA passing the relevant querystring using the Socket class or something similar, and then receive the result as XML and use something like SAXBuilder to read the received stream and convert to manageable XML.

Something like this to read in the response, BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

Am I wrong in that conclusion?
[ May 10, 2007: Message edited by: James Bukowska ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
I thought I would be able to directly request the page using JAVA passing the relevant querystring using the Socket class or something similar, and then receive the result as XML and use something like SAXBuilder to read the received stream and convert to manageable XML.


I'm confused now - you have Java code that accesses a URL? Where does this code run - is it part of a servlet or an applet?

You can get an InputStream to read from a URL in the way you suggest, or like this:
James Bukowska
Greenhorn

Joined: May 10, 2007
Posts: 9
Its part of a servlet, and when I first posted the original question I hadn't tried any of this...



The above seems to do what I want which is basically what you suggested... Is there a better way to do this?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That covers things pretty well. I've used a utility to call web services that looks an awful lot like that. Retrieving content from another site is just like this, too.

So you come down to an architecture / user experience choice.

1) You can do a normal GET or POST from the browser to your app server, run the code you showed on the server and return your own content mixed in with whatever you retrieved from the other site. The user sees the whole page refresh on the browser.

2) You can submit an XMLHttpRequest from the browser to the other site, get back HTML or whatever they return, merge that into the HTML the user is viewing, all in browser-side JavaScript. The user sees one part of the page rather magically change without refreshing the whole page.

#1 is more conventional, uses Java which has great tools like Eclpise. #2 is sexier but uses JavaScript which has some cross browser issues and some people just hate working in it. Libraries like Prototype and Dojo ease some of these issues.

Does that give you enough to make a choice? Or ask another question?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
James Bukowska
Greenhorn

Joined: May 10, 2007
Posts: 9
Cool thanks, I've decided to continue with my read stream attempt, it seems to work, unless someone can tell me a good enough reason why i shouldn't

No Ajax since my employer doesn't wna tto risk people not having javascript enabled and complaining

Thanks to both of you
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by James Bukowska:
No Ajax since my employer doesn't wna tto risk people not having javascript enabled and complaining


:roll:

It is 2007 you know. Might mention that to your employer.


GenRocket - Experts at Building Test Data
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Do you know what the acronym AJAX stands for?

Asynchronous JavaScript And XML.

Take out the JavaScript and it isn't AJAX anymore.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Question - AJAX and JAVA, Is It Possible?