jQuery AJAX to call Java method - java

Using jQuery AJAX, can we call a specific JAVA method (e.g. From an Action class)
The returned data from that Java method would be used to fill in some HTML code.
Please let me know if this can be done easily using jQuery (like it does in DWR)..Also for multiple data points in HTML, do we need to make multple AJAX requests?

The simple answer is you map your ajax calls to urls, which are in turned map to methods in your java code. The Ajax -> URI mapping happens on the client side (which ever js framework you are using, and the URI -> specific handler mapping happens within the java application)
What java framework are you using? There should be very clear and simple documentation on how to do this. For standard Java EE mappings (meaning you are not using any frameworks like Spring or Roo) I found this on google: http://javapapers.com/servlet/what-is-servlet-mapping/
"For multiple data points in HTML" I assume you are talking about having multiple parts of the html update. You can do this with multiple requests, or you can do it with one request. If you do the latter, the server needs to return all the data you need to update the dom appropriately.

It's not as transparent as with DWR--DWR handles making JavaScript look like Java. With jQuery you'll get JSON (or just HTML if/when it's easier that way). It's still pretty straight-forward, though. You'd send the Ajax request to a URL, rather than having it look like a local method call.
I'm not sure what you mean by "multiple data points in HTML" -- you get back whatever data you get back, and you can do with it whatever you want. If the response has all the data you need, then you wouldn't need to make multiple requests.

Related

How do I mutate the DOM of a displayed HTML page, via Java, over HTTP (from the server)?

I would like to get a reference to the DOM of a displayed webpage so that I can add and remove elements. I would like to add and remove those elements by sending HTTP requests from, say, a servlet. Further, I would like to be able to construct a DOM, node-by-node, in the browser, via java, from http messages coming from said servlet.
Suppose someone requests a url (say): http://helloWorld.xhtml
I would like to render the content by sending HTTP messages, which, node by node, build the displayed document. Then, if I need to change something in the document, I would like to be able to remove html node-wise, or add html in the same manner; I would like to make partial changes to the html document via http.
Does anybody know of specific way(s) to accomplish this?
I don't want to use JavaScript, unless it means a utility method to accomplish the communication mechanism only. I don't want to use a client-side applet either, but that might be a way of getting around the limitations of http (i.e., sending back java object responses via jaxb).
I don't want to use javascript because I just want to use an html browser as a displayer of elements that relay the occurence of events back to the server. Then, I want to be able to add or remove elements without redrawing the page. Also, I would like to use XSLT to generate HTML, but again, I would like a way of updating the web page without using hidden elements and without redrawing the whole page.
I thought of something else also. I might use a JApplet embedded in an HTML page, and then just communicate with the JApplet via JAXB? I am assuming that you can do that.. (maybe it does not work that way). Then, in the JApplet, just using a means of displaying an HTML page; there might be a better option amongst the components than the JLabel.
I think that the best option would either be using the HTTP mechanism (if possible) or else, using some sort of middle layer that would enable me to send Java back and forth between the webpage and the server.
I've thought about this a minute, and perhaps, if browsers would change their update policy, then, we would be able to use http to facilitate a view that is updateable on a per-element basis, via a browser behavior that supports additions or subtractions of elements?
Why you don't want to use JS is beyong me since this is THE language for editing the dom.
The easiest way to accomplish your scenario would be using Jquery.ajax.
For parsing/building Json i would recommend Google's Gson.
You can look here for a small sample.

How can I override the render strategy for a specific page in wicket

I have an application which needs to accept a POST request from an outside server, to confirm payment. I don't want to break the default wicket render strategy (REDIRECT_TO_BUFFER) which is serving to give the users a nicer experience than ONE_PASS_RENDER would, however, the external service is not happy with the 302, and keeps retrying until it gives up.
Is there some sensible way that I can tell wicket to use ONE_PASS_RENDER for only the specific page that handles this request?
Try using a Resource instead of a Page to handle this request. That way, you can return whatever response you want (both HTTP headers and payload) to keep the external service happy.
It's a lower level API, though. If you need to respond with a rendered page, you may need to render it yourself (with lots of println() calls), or hack some way to Wicket to do it.
But since it is a response to a external service, I assume it will expect some kind of simple text, XML or JSON response, which are easy enough to do by hand.

Can you read the flash from javascript?

Play's flash is a "temporary storage mechanism that is kept for the duration of the next request", using cookies.
Is it possible to read the flash from javascript?
I want to implement a generic client-side notification mechanism, and also access that mechanism from the server. What I imagine is for any controller to do:
flash.put("notification", "You have been notified");
And then for my main.js to check if the "notification" flash exists, and if it does display it above (similar to Stack Overflow's notification mechanism).
However, I suspect that flash might be encrypted in a way that the client side cannot read it. I haven't found any code samples that show how to read it from the client side JS (not in a template). Is this possible directly?
As I'm writing this question, I'm actually coming up with a way to implement this: I can put a hidden div in the main template that all my views extend, and access that div from JS. Does it make sense? Is there a simpler approach?
I don't think it's possible to use JavaScript to read flash, even via the cookie. That cookie is actually encrypted with a security seeds (application.secret) configured in your application.conf.
However it is possible to implement what you want.
Create a tag to export flash variables into Javascript object.
Call that tag in your view.
Your main.js use that exposed variable to do whatever it want.
For me you have two solutions
In your view call a javascript function with the value as an argument, then in javascript you can store it in an object for later use
The flash cookie isn't encrypted, it is sign, so you can read it but you can't modify. Use a tool like firebug to see the content of your flash cookie

Fetch JSON data from a third-party and getting around the SOP

The Same Origin Policy is preventing me from fetching the JSON data I need from another web site (with permission). I saw one person who was working around this with JsonpRequestBuilder, but I'm not sure if that's going to be the best solution for me. The only other option that comes to my mind would be to have an intermediary servlet on my server.
What's my best bet here? I have concerns with both methods. With an intermediary servlet, I worry about the delay that would introduce. With the JsonpRequestBuilder, it looks like I have to create a complete JavascriptObject for each method I'll call from the other site, even though I only need to pull out a single value from some of those methods.
I don't use Java, but JSONP is what I usually implement when I need cross-domain chatter, and I'm sure someone will have made a Java library that unwraps it. It requires a change on the third-part's site, but it is a very simple change.
EDIT: Sounds like that is what that library does, sorry... but still... it's the way to go :)
Check out the CORS Specification. We are using this to successfully circumvent the SOP using our own server with GWT's devmode Jetty.
You don't have to "create a complete JavaScriptObject", a JavaScriptObject is actually just a mean to call to JavaScript from the Java world, so you only need the one getter for the value you need, and it can even return a "nested" value:
public native String getFoo() /*-{
return this.nested[0].obj.foo;
}-*/;
Whether you'll use JSONP (and JsonpRequestBuilder) or a "proxy servlet" actually only depends by the capabilities of the "service" you need to call: JSONP is JavaScript, not JSON, so the server has to return a "JSONP response script" or you won't be able to use JsonpRequestBuilder (and similarly, you won't be able to (safely) use CORS or a proxy-servlet if the server returns a "JSONP script" rather than application/json).

How to read a response of a servlet using PHP

I would like to send a request to a Java Servlet from PHP and receive the response from the same and show it on the PHP page. How should this be done?
Thanks and Regards
Abishek R Srikaanth
If all you want is to print the response of a GET request to an external resource plain vanilla into the PHP response, then you can use file_get_contents() for that.
<?php echo file_get_contents("http://example.com/someservlet"); ?>
The servlet's doGet() method will be invoked and whatever response it returns (which can even be a forwarded JSP) will be printed as string to the PHP response.
If you want a little more fine grained control, e.g. using POST or something, then head to curl() instead. The linked PHP manual contains several examples.
Regardless of the way, please note that whenever it returns HTML, that you should ensure that you end up with valid HTML. For example, nesting <html> tags is illegal. Pass the PHP page through the w3 validator if you're unsure. Otherwise you'd better have to parse the HTML to extract the <body> pieces of interest or to use an <iframe> instead.
<iframe src="http://example.com/someservlet"></iframe>
If I'm understanding you correctly you want to read the response of a servlet in php and then output it from php?
You can use file_get_contents to the url (Probaly not the best way, but for simplicities sake it the easiest) and then just echo the output.
For example:
$content = file_get_content('http://www.google.com');
echo $content;
But if you want to be able to login or use the session at the servlet side you will need think of something else. As each request to the server from php will be a new one, it does not store cookies etc.. like browsers do.
Hope that helps
This is a situation which might do well to be rethought, but, if there are no other options...
If there is a way to actually update the portlet, then I would recommend creating some form of service call -- SOAP, custom RPC, etc -- on the Java side. Technically this is the most correct way to do things.
Failing that, if this is a simple GET request, then use file_get_contents.
If it has to be a POST/PUT/DELETE, then you can use cURL. cURL also has the benefit of being able to handle simulated sessions, which means that you are then able to simulate a log in and actions following that (though not without some difficulty).
If you don't have cURL and you need to POST/PUT/DELETE, then the streams library might be able to give you what you need.
If you don't have the streams library or cURL and you need to POST/PUT/DELETE, then there are other means of accomplishing that, but maybe you should really re-rethink that situation.
If all of the above don't work, then you will need to tame the Spectral Wolf. The Spectral Wolf fears only fire. I can no longer help you, but if you master the Spectral Wolf, he will guide you. Godspeed.
If you really want to do that, you can create a Java app that takes parameters to populate request and response objects, instantiates a servlet, runs the correct method, gets the result and displays it, then call this Java app from PHP.
Or you can use the experimental and unrecommended PHP / Java Integration module.

Categories