DWR (Direct Web Remoting) provides an amazingly convenient approach to AJAX and the best thing is that it is so easy to implement.
When I was using DWR, I got a little confused about maintaining session, since most of the examples available used POJO/bean to implement java code which was then sent back to javascript.
In my application, an arraylist (formList) was maintained in session. I had to use DWR to show the details about this arraylist on the JSP.
In the java class being used by DWR, I needed to pass the session from calling JSP. DWR gives this in a surprisingly easy way!
JSP snippet:
<button onclick="getProperties(id);">Click! </button>
In the javascript:
function getProperties(id) {
DwrUtility.getDetails(paneId, function(data) {
dwr.util.setValues(data);
}); }
In the Java class:
public String getDetails(String menuId, HttpSession session){
int paneId=Integer.parseInt(menuId);
ArrayList formList=(ArrayList)session.getAttribute("FormsList");
String paneDetails = "Number of forms: "+formList.size();
return paneDetails;
}
Did you notice that we did not pass session parameter from getProperties() function in javascript, but we used that in getDetails() method in java class. This is the magic that DWR does here. If you mention HttpSession as one of the parameters, DWR automatically passes the session from calling JSP to the class.
See I said, that is so simple!
When I was using DWR, I got a little confused about maintaining session, since most of the examples available used POJO/bean to implement java code which was then sent back to javascript.
In my application, an arraylist (formList) was maintained in session. I had to use DWR to show the details about this arraylist on the JSP.
In the java class being used by DWR, I needed to pass the session from calling JSP. DWR gives this in a surprisingly easy way!
JSP snippet:
<button onclick="getProperties(id);">Click! </button>
In the javascript:
function getProperties(id) {
DwrUtility.getDetails(paneId, function(data) {
dwr.util.setValues(data);
}); }
In the Java class:
public String getDetails(String menuId, HttpSession session){
int paneId=Integer.parseInt(menuId);
ArrayList formList=(ArrayList)session.getAttribute("FormsList");
String paneDetails = "Number of forms: "+formList.size();
return paneDetails;
}
Did you notice that we did not pass session parameter from getProperties() function in javascript, but we used that in getDetails() method in java class. This is the magic that DWR does here. If you mention HttpSession as one of the parameters, DWR automatically passes the session from calling JSP to the class.
See I said, that is so simple!
Comments