RequestDispatcher Forward to already loaded JSP - javascript

I haven't worked in server side in a while and any help is really appreciated.
I am trying to create a small application that has an index.jsp page. The page has a button which when invoked calls a public SaaS API that does something in an external server with no changes to the view in the browser (still has index.jsp loaded).When the external job is done, my callback servlet is called by the service.
Now I need to update a component of the JSP when the callback servlet is called. So, I am doing a request.forward to the already loaded index.jsp with an attribute which when found in the JSP will invoke a javascript function.
The problem is I am unable to invoke the javascript function at all. I tried document.onload or window.load or document.ready etc. and none of these functions are getting called upon the request.forward to the index.jsp, since it is already loaded.
What is the best solution for this? Any help is really appreciated.

You should model your interactions in such a way that on your button click, send the request to a servlet. From the servlet call the external saas api.
When it completes return the response to the servlet(assuming you want a blocking model). Put the data in the request and forward it to the jsp.

Related

Locals in Javascript/JQuery like Locals in Rails?

I create a Js Application and want to have English and German locals, which i can switch via button.
Is there a way to insert locals from a extra file in a .js file, like the function t'...' in Rails ?
As far as i know there is no way to do it directly and the reason is fairly simple too, erb is executed at the server side and javascript is a client side language which means its executed in your local browser, thats why if you even try to pass a variable between the two you'll have to make a request to the server, However this problem is tackled by calling an AJAX request, this AJAX request does the same thing as sending a new request to the server however it does that without refreshing or reloading the page to it gives the users the illusion that no request was made.
a guy asks a similar question here:
http://www.quora.com/Ruby-on-Rails/Can-I-pass-a-JavaScript-variable-to-a-Rails-method
and you can learn more about AJAX here:
http://www.w3schools.com/ajax/default.asp

get the rendering of an html js in a controller (symfony2) before any action has been done

Could you please help me with this issue?
I have a js file that is connected to an API... it can get information about geolocalisation.
Anyway, I need to get this information in a symfony2 controller, but before any rendering (before the first rendering), any clues please?
PS : i could try file_get_contents, but this works with remote links, but not with a local file (where i put the html js). There is something I don't understant.
Thank you in advance for any help!
I guess you can follow this scenario :
A first action of your controller calls a page where js is and colls the api
This page then calls a second action of your controller (via window.location.href for example) and sends it the information, so the action can render what you want

How do we call javascript from a codeigniter controller method

I have a controller "track" in codeigniter where I have many methods. In one of the methods I am just redirecting user to some other URL which comes from url parameter as
track.php
function click_thru()
{
redirect($this->uri->segment(3));
}
Before Redirection I want to call google tracker Javascript code. As I am not loading any view in the above method, how do I call JS before redirect?
You can't, PHP and Javascript don't communicate each other like this, you have two solutions :
Either finding a way to do your redirection in javascript after the google tracker call is called (but you have to load the entire body of the response just for one redirection, so not very cool).
Either you find a PHP API that permits you to interact with Google Analytics. Like this one : https://code.google.com/p/php-ga/

How do you make a link perform an action without reloading a page?

The clearest example of this I could think of is the Reddit Upvote/downvote buttons how when you click the button, the value for upvotes is updated, the upvote button lights up, and the page DOES NOT reload, you just stay exactly where you are on the page.
I am trying to make a feature similar to this and I can totally figure out how to do it with reloading, but I want it to not reload so the user experience isn't disrupted.
Is it possible to do this with php? or would I need to use javascript or something?
The action I would need it to perform would be a basic update query in the database.
This would be done with an Ajax call to your php script. Ajax is designed for these asynchronous updates and/or reloads.
Another way you can do this is with HTML5 WebSockets. You could have the client send a trigger to the server when the user clicks the upvote, and then the server could update and push back the data. It would, however, be a bit overfill for this.
If what you want to do is to contact a server to either send it some state or to retrieve some state from the server (or both), then you would use AJAX with javascript in order to contact the server without reloading the page. You can then also use javascript to update the state of your page after the operation. That is generally what the Reddit page you refer to is doing.
Conceptually, you'd set up your page like this:
Put the link on the page.
With javascript install an event handler so you are notified of a click on the link.
When the link is clicked, your event handler will be called.
Prevent the default behavior of the link so the browser doesn't navigate to a new page.
Then, in the event handler, send your data to the server using AJAX. You will obviously need a URL on your server and server process that can accept and process the data for you and return a value if you need to.
If you need the response from the server, then set up a callback function for when the AJAX call completes (this will be some indeterminate time in the future).
Then, if you need to change the current page in any way (like show one more upvote), then you can modify the current page with javascript to show that new state.
Ajax is easier to use with a library (like jQuery) that contains some ajax support code, but you can certainly implement it in plain javascript too.
Here's one example of ajax with plain javscript. You can find many other examples with Google.
This MDN tutorial on AJAX seems pretty helpful too to show you how it works.
You could use JavaScript to do this. Here's a quick sample:
Vote Up
Simple solution in JavaScript:
var el = document.getElementById("upvoteBtn");
el.addEventListener("click", onVoteClick);
function onVoteClick(e) {
e.preventDefault();
// do something
}
Here's a fiddle.
NOTE: I see you'd be updating the database. In that case, you would have to use AJAX in the onVoteClick function (or use XMLHttpRequest) for this. JavaScript is a client-side programming language and will not be able to communicate to the server without the use of AJAX or XMLHttpRequest. Using the jQuery library, you should be able to write AJAX pretty easy.
It's called AJAX.
With AJAX you can send a request in the background.
The easiest way is to use the jquery libary for this.
You can also output some data as JSON back to the script if you want to take some other actions depending on the result from that query.
A good tutorial is this one.
It also explains how this requests (called: XMLHttpRequest) work.
You need to use Javascript's XMLHttpRequest
You can use AJAX...
It allows you to use JavaScript (client side) to call server side functions. Here's a good example.

Call JS function before redirect

The situation is as follows:
A user on the client side enters some data and presses a command button that initiates an ajax request. Depending on the input data the JSF Bean on the server side redirects to different pages. But in addition the HTML5 features localstorage respectively sessionstorage should be updated on client side, also depending on the results from the bean. For the storage some data entered from the user is necessary, so that the java script for the storage has to be within the original page.
Now, the problem seems to be that after the response the redirect is always done first, before executing the js function and so it is not possible to access the input data from the user on the client side of curse.
I tried things for calling the js function after pressing the command button like "oncomplete" on the client side using callback parameters (redirect takes place earlier) or RequestContext.execute from the server side (but this internally also uses the oncomplete event i think).
One possible solution I could imagine is to use window.onunload on client side and a hidden input formular to get the result of the bean. But isn't there a better solution?
You need to redirect by JS instead of by JSF. Return null from action method so that it returns to the same page and then execute the following script to perform a redirect in JS.
window.location = newURL;
If you use PrimeFaces with JSF then it's possible to make global override of PrimeFaces.ajax.ResponseProcessor.doRedirect in javascript to put any custom code before actual redirect happens:
PrimeFaces.ajax.ResponseProcessor.doRedirect = function(node) {
// <<<< your code is here >>>>
window.location = node.getAttribute('url');
}
Put this code in some js file and include it in all your pages.

Categories