Monday, November 21, 2011

Download a file without Server trip - HTML + JSP

Hi,
Many times we as a developer has data on client side for the end user and on his request wants to prompt user (Save As dialog box) to download that data.
The solution to download local/client-side contents via javascript is not straight forward. I have implemented one solution using SmartClient, Html and JSP.

Being a developer and not a good philosopher 'm coming straight to the solution:

Problem:
I am in the project in which client side is implemented in SmartClient8. We need to download/export data of a grid (a table like structure). We were using RESTish web services to serve the data from Server side. So I want to ignore the server call for the same data to download that I am showing in the grid.

Solution:
What I did is made two JSPs namely: blank.jsp and export.jsp.
File blank.jsp is literally blank, absolutely no code. Now I need to export the grid data that I already have on client side in JSON format [JavaScript Object Notation - you can google it :)].
Now when ever user asks to export the grid data (by clicking a button on UI), I do below:
    a. Open a new window with url blank.jsp
    b. using document.write I create a form in it with one field name text in it and set data to export inside it.
    c. Now POST that form to export.jsp present in same hierarchy.
    d. I am pasting code of export.jsp and is self explanatory.

// -------------------------- code start -------------------------------------

<%@ page import="java.util.*,java.io.*,java.util.Enumeration"%>
<%
response.setContentType ("text/csv");
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename=\"data.csv\"");
String contents = request.getParameter ("text");
if (!(contents!= null && contents!=""))
contents = "No data";
else
contents = contents.replaceAll ("NEW_LINE", "\n");

//Open an input stream to the file and post the file contents thru the
//servlet output stream to the client m/c

InputStream in = new ByteArrayInputStream(contents.getBytes ());
ServletOutputStream outs = response.getOutputStream();

int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
//System.out.println("" +bit);
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>



<%script type="text/javascript">
try {window.close ();} catch (e) {alert (e);}
<%/script>



// -------------------------- code end -------------------------------------

Above functionality is cross-browser and working fine on IE, FF, Safari, Chrome.
This code is tested, deployed and working in production environment.


Thanks shaILU

Tuesday, September 6, 2011

How to: Skip entering password for every WinCVS action

Well,


Before making some R&D in this area I tried much of googling but no luck.  
The original motivation for me to do this tweak (yes, its a tweak) was annoyance.  Actually my m/c was changed and I have to install new WinCVS client (tortoise), which is asking for WinCVS password on every action.  


After keeping passions for almost more than 3 weeks; by this time I entered password every time Mr. Tortoise asked me to do so :( ; I decided to search how should I skip entering password every now and then.  Suddenly by doing an action; a secret :); I got an idea to try.


First, let me tell you what were the project conditions.


My old machine was going nuts in which my recent project code was synced from WinCVS.  And in new machine instead of syncing complete code I simply copied it from previous machine.  In old machine at the time of installing WinCVS and configuring I opt the option to save password and I was happy (yes, this is one of the way).  But on new machine as I already had a copied code with me and I need to find another way.


Here is the way:


Note: Before applying below changes be sure that you password would be visible at the time of WinCVS action.  Do this at your own risk.


1. If you have copied the code from one machine to another then there would be hidden CVS folder inside each sub-folder with name "CVS".  This "CVS" folder is by default hidden and contains below files:



  • Entries
  • Entries.Extra
  • Entries.Extra.Old
  • Entries.Old
  • Repository
  • Root
Here we are interested in file "Root".  The content of this file would be some thing like 
      :ssh:[username]@[IP_Address]:/usr/local/cvsroot


change it to
      :ssh:[username]:[password]@[IP_Address]:/usr/local/cvsroot
here [password] should be WinCVS password obviously.

Do above changes on the root folder of your source code.  No need to make changes in all sub-folders.

2. Hope this solves your problem, but if not, perform below additional tasks:
    We are adding your password into CVS client, I am taking example of WinCVS client tortoise.
    Go to the root of your repository and using right click of mouse button go to CVS -> checkout

    
     


Now you will find some thing like this:

            



Here in CVSROOT field change value to 

      :ssh:[username]:[password]@[IP_Address]:/usr/local/cvsroot
and in User name field
      [username]:[password]

and select "OK" button.  This will restart syncing your code which you can cancel.  

After these steps your tortoise i.e. WinCVS client would never ask you your CVS password to proceed.  

You can revert these steps in the same sequence to undo above effect.

I have done this and its working fine on my machine.


Thanks
Shailendra














Comparision: Prototype, Dojo, jQuery, mootools and SmartClient


Before starting to build new UI for our new project, I made some research on different AJAX RIA Frameworks. Below are my findings on the same:


1. Prototype framework favorable links:
2. Dojo framework favorable links:
3. jQuery framework favorable links:
4. Speed performance test of different RIA frameworks can be found at: http://mootools.net/slickspeed/#

Few more comparasions:

Out of all these findings I started using SmartClient 5. Initially we faced some issues but as SmartClient matures I find it interesting in many terms: 1. APIs doc help and examples 2. Flexible controls 3. Forum

Today I am working on SmartClient 8 and few on my GUIs are in production running successfully. Actually the great help with SmartClient is that you find every thing at one place. No need to dug many other sites that is hard to do for any other open source RIA framework.

So my choice is no doubt SmartClient.

Thanks
Shailendra