Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Simple Question - AJAX and JAVA, Is It Possible?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Bukowska
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
James Bukowska
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what the acronym AJAX stands for?

Asynchronous JavaScript And XML.

Take out the JavaScript and it isn't AJAX anymore.
 
Honk if you love justice! And honk twice for tiny ads!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic