JSP Servlet dynamic change page content via ajax - java

Is there a way to let my page using ajax update a certain part content on my jsp webpage when the database change?
Currently I have:
1. db.java - a class to connect to database and query out the data I want
2. admin.jsp - a jsp page to display those data I want
My question:
How to using JSP and servlet automatically and dynamically update a page content while database changes, I know how to use JQuery ajax call servlet, but to achieved this, the only way I can get is set a time interval and loop repeating call ajax function, is this way workable ? or there are others better methods to do it?

I have been doing similer thing using jquery .load().. I have a struts2 action which is returning a VO ,which is binded with JSP view . i am calling same action via jquery .load() and updating JSP div.
I am using a polling timer which is handled in javascript to trigger this .
http://api.jquery.com/load/
Regds,
Amit

Related

Session Attribute Not getting updated in .gsp Page after Ajax Call

I am using session attributes in my .gsp page like below:
<g:set var="maxValue" value="${session?.MY_VAR}"/>
I am making an AJax call from Javascript to the Groovy controller ( MyController.groovy ) and setting it like below:
session.setAttribute("MY_VAR", "abc");
After Ajax call returns back to the GSP. The value of session variable is not updated. It still stores the old value from the previous load.
Any ideas on how to solve this ?
Your GSP is generated on the server and the resulting HTML is sent to the browser. Your Ajax call does not force a page refresh, so the GSP along with the line below is not reevaluated and the variable maxValue is not changed.
<g:set var="maxValue" value="${session?.MY_VAR}"/>
You should consider render as JSON in your controller to return maxValue and using JavaScript to update the DOM after the Ajax response is received. See render for more info.

How do I handle Http request race conditions in a Front Controller web application?

I have a web application, which is an implementation of the Front Controller Pattern, as described by the image below. Specifically, I have followed the code example of the Front Controller Pattern, as described in this question's answer:
Design Patterns web based applications
I'm having updating issues with one of my .jsp pages. This page in particular has a popup-editor, which allows the user to add or remove links to sets of data from an external database. If the user hits a save-button, it sends a request, which is intercepted by the Front Controller. Then, the correct action for saving or deleting a reference link (many to many ID numbers) is executed on the systems own database. After, the page should be reloaded, which triggers a chained command, which loads data from both databases, before redirecting to essentially the same page, without the popup.
However, the page is not reloaded, due to some form of caching that I do not understand. I've tried every solution I could find, to prevent loading cached data, from adding a unique value to the URL, to adding meta-tags on the jsp-page, to prevent it from caching at all. I've confirmed that the data is loading on server side. A simple F5-click updates the page like it should be doing automatically.
I've also implemented an onClose-function on the popup, that calls a window.location.reload(). However this call interrupts the Front Controller, and it stops everything it is currently doing, to handle the newest request. This creates a race condition, where the data is sometimes able to be inserted/deleted, and sometimes not, before the Front Controller starts reloading the page.
Should I make a queue?
Is there a good reason not to do it this way?
Should the Front controller's execution method, or the class itself implement some kind of Synchronization?
Your popup-editor should be placed inside an iframe so that it will not reload the window when it submits. The response that returns from request sent by the iframe should trigger the window.location.reload(). For example:
<html>
<script type='text/javascript'>
window.onload = function() {
window.location.reload();
};
</script>
</html>
Another option is to send the update with an Ajax call and then reload the page when the Ajax call returns. Or nicer, update the page dynamically without a reload but with JSON data returned by the Ajax call.

Javascript not executing on JSP forward action

Hi I have one jsp page and I have to update two html select boxes with the data of an inf file. On selecting the first combo(initially also not loading the second combo), the second will have to change accordingly. Since java script variable is not accessible from java, I had sent the value of first combo to an another jsp by ajax. And then return it to first jsp itself.Plaese help me.
Maybe I'm getting it wrong but looking at your code it seems that you are doing AJAX request on selectbox change event, but you are not doing anything with the response.
So basically what happens is you are doing an initial render to your jsp and everything is fine. Now you are changing selectbox option and AJAX request is sent to dummy.jsp. dummy.jsp is receiving it and redirecting the maker of the request to User.jsp. User.jsp-s content is sent back but nothing is done on the client side AJAX call with this response.
What you should do is have your logic at dummy.jsp(or even better at some servlet) which based on your AJAX call request parameters returns for example a JSON with data that you can process in jQuery.ajax success event. In there you can use this JSON to readjust your selectboxes and other data with javascript.
Check the examples at the end of jQuery.ajax manual: http://api.jquery.com/jquery.ajax/

JSP page values are not getting populated after getting values from JSP popup window

I am passing JSP values to JSF's backing bean. Once I get the values in bean, then I am trying to assign values to inputText fields with setters method, something like this.
public void testProcess(){
empBean.setEmpName(empBean.getEmpId());
}
testProcess method is called in the action of the JSP page.
My question when I set the value in the bean, my JSF page's values are not getting populated. Do I need to explicitly refresh my JSF page, if so how could I do that?
Yes, you need to refresh the main page. What happens on the server will not automatically make something happen on the client (browser).
One fairly common pattern for this type of operation is to have the popup do the form post using ajax (easy with many javascript frameworks like jQuery). See http://jquery.malsup.com/form/ for a nice example using the jQuery form plugin. In the response handler for the ajax-call, you reload the main page (using the window.opener property) and close the popup. In the example with the jQuery Form plugin, you would do something like:
<script type="text/javascript">
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
// reload main page
window.opener.location.reload();
// close popup
window.close();
});
});
</script>
The reason you want to submit the form using ajax is that you want to wait to refresh the main page until the post is completed and doing a regular post will cause the popup to reload, which in some browsers will invalidate the window.opener property, making it impossible to reload the main page.

Portlet URL Generation From AJAX Servlet

I am trying to modify a portlet to load data for a table over AJAX because the WS calls take a ridiculous amount of time to complete. The table is basically an overview with one entry per table row and a link in each row to more detailed information on the entry.
Here is how I am currently creating the URLs for each row in the table:
<portlet:renderURL portletMode="VIEW" windowState="maximized" var="showURL">
<portlet:param name="id" value="${entry.ID}"/>
</portlet:renderURL>
I have created an AJAX servlet to receive the AJAX calls and return JSON which will be added to the table dynamically using jQuery callbacks. The servlet works fine and the rows are added to the table with no real problems. The problem I am having is currently with the links that should be in the table.
My question is how do I mimic the above JSP code in the servlet to generate the correct portlet URLs?!?! I'm a bit new to portlets and their URLs seem to be a serialized mess of gibberish to me.
Take a look at this JSP page for an example of how this is done -- one good method, anyway. Look at the definition for editPortletUrl near the top of the page, then look at how it's used later.
There's a PORTLETID token embedded in the URL, which is later replaced with a real value by JS.
This portlet uses Fluid Infusion, which I recommend highly for powerful, accessible, higher-order widgets based on jQuery.
Brian said
Thanks for the idea. I tried doing what you had but I don't see how the JS will be able to sub the real ID for the placeholder. Using renderURL I get this in the JS:
var baseShowUrl = "/wps/myportal/portalname/!ut/p/c5/hY7NCoJAFEafpSe4X_Pf0hJ0Sied6M9NCEVIWS2ioqdvwo2b6H7LczhcqijsUj-aY31vrpf6TFuq9C52yJ32DAZCworYz_V0DIDRqq1fTdu8D_tOFUra0oqgZoKD-VLabBpzQPXVDVWqV9UjE6qFzGcmD1XQ-vtHz0jSQgVjmXnr5xxm2HH8uAgdnyRRKnQGJJpJMCcXLI9WSLj6wwW59Noe6NYafjr59BkNBh8sH-CA/dl3/d3/L0lDU0NsQ1FvS1VRIS9JSFNBQ0l3a1FBd3FibTZtLzRDMWI5WUF4RW1TUVVnZyEvN19ETjBNTjdSMjA4MDQ1MEk0RFJPN0pCMDAwMi92aWV3L0NPTlRSQUNUSUQ!/";
UPDATE from Drew Wills
Could you show me how your or tag?

Categories