?
Solved

Data on my JSP needs to be converted into CSV format!!!!!!Urgent

Posted on 2006-06-08
15
Medium Priority
?
594 Views
Last Modified: 2007-12-19
Hi Experts,
I am posting the question once again as i did not recieve any answers..Please answer as it is very urgent...
Right now  am displaying  data from a database and  I use a Java class to query a database and return a collection of data in the form of an array.  Then a JSP calls a method in the Java class(in a scriplet) to get the data and then cycle through the returned collection to display the data.
The following is the code of the jsp file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page session="false" %>
<%@ page import="java.sql.*"%>
<%@ page import="com.inat.db.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="com.inat.bo.Status"%>
<%@ page import="com.inat.bo.StatusEntry"%>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link type="text/css" rel="StyleSheet" href="../css/statustable.css" />
<title>INat::LoadStatus</title>
</head>
<body>
<h2 align=center> INAT :: Data Load Status </h2>
<form>
<%@ include file="../calendar.jsp" %>
<%
    Enumeration entries = Status.getStatus(selDate);
%>
</form>
<p><b>Status for : <%= displayDate %> <b></p>
<br>
<table class="status-table" border=0 cellspacing=1 cellpadding=2>
<thead>
<tr>
<td> BSC </td>
<%
for (int i = 0; i < 24; i++) {
    String ts = i + ":00";
%>
<td> <%= ts %> </td>
<%
}
%>
</tr>
</thead>
<%
while(entries.hasMoreElements()) {
    StatusEntry entry = (StatusEntry)entries.nextElement();
    int numDataArrays = entry.getNumDataArrays();    
%>
    <tr>
        <td class="label"> <%= entry.getName() %> </td>
        <% for (int hour = 0; hour < 24; hour++) {
               String code = "success";
               String msg = "";
               for (int i = 0; i < numDataArrays; i++) {
                   int[] data = entry.getDataArray(i);
                   if (data[hour] <= 0) {
                       code = "error";
                   }
                   msg += entry.getDataArrayName(i) + "=" + data[hour] + "<br>";
               }
        %>
         <td class=<%=code %>>
         <%= msg %>
         <a href="ShowLogs.jsp?date=<%= selDate %>&bsc=<%= entry.getName() %>&hour=<%= hour %>&type=log">Info </a>,
         <a href="ShowLogs.jsp?date=<%= selDate %>&bsc=<%= entry.getName() %>&hour=<%= hour %>&type=err">Errors</a><br>        
         </td>
        <%
         }
        %>
    </tr>
<%
}
%>    
</table>
</body>
</html>




I now want to Change the current Load Status page(Loadstatus.jsp) in CSV format as the data is required in that format for easier parsing.Do i provide a link on the current page to another jsp by including some code like this which i found on experts exchange??? or do i include it as a part of the above code..Please advice..
The code is as follows:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link type="text/css" rel="StyleSheet" href="../css/statustable.css" />
<title>Welcome to Download Page</title>
</head>
<body>
<h2 align=center> INAT :: Data Load Save</h2>
<form>
<%
String pageContent = "Inat Data Load Status Save" ;
String filename = "DataLoadFile.csv" ;
response.setContentType("text/csv; charset=ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
out.write(pageContent);
%>
</form>

</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After all this is done i need to call this JSP which contains CSV data from another java application parse the data and display the content output as response...

Please help me with the coding.................!!
0
Comment
Question by:raghuudupa
  • 7
  • 5
12 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 16869954
in above code I do not see any place where you are getting a CSV data content to be downloaded.... either I am not getting your problem correctly or missing something here... can you try to explain what actually you are trying to accomplish?
0
 

Author Comment

by:raghuudupa
ID: 16870056
Ok,
The data is not being downloaded in csv format.I had picked up the scriplet from a prev question on this website and i thought it was the approach to convert data into CSV format...Ok let me explain...
Actually the scenario is like this..
I have data being displayed on a jsp which is in tabular format.This data is being fed from a java class which performs a query from the backend in the form of a prepared statement.
Now they require me to change the databeing displayed into some format like CSV so that it is easy for parsing.So i do not know how to go about doing this...
The data looks like this:(the rows are bsc's and the columns are time)
I have  included data from one cell below and the rest of the data is something similar for different times and bsc's:
Hope this helps...

BSC         0:00  1:00  2:00  3:00  4:00  5:00  6:00  7:00  8:00  9:00  10:00  11:00  12:00  13:00  14:00  15:00  16:00  17:00  18:00  19:00  20:00  21:00  

PHBSC56  tch=174
               ho=1685
               tchsummary=1
               hosummary=1
               Info , Errors
             
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 16870170
okay now that makes more sense to me...
what you can do is to put a link on your current page which shows data in tabular format..
on clicking the link you will go to a separate page where you will call your java class back again to get the data ( I hope the data is coming in a collection or a array of some sort..)

now once you have this collection with you, use the following logic..

<%
StringBuffer data = new StringBuffer();
String filename = "DataLoadFile.csv" ;
response.setContentType("text/csv; charset=ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
while(entries.hasMoreElements()) {
    StatusEntry entry = (StatusEntry)entries.nextElement();
    int numDataArrays = entry.getNumDataArrays();    
data.add(entry.getName()+",")
out.write(data.toString());
%>

in short use the string buffer to collect all the data & then send it out to the page...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:raghuudupa
ID: 16870243
Hey Kuldeep,
  Appreciate your help mate...To be frank iam a novice in this area...Basically i require your help to go about doing the coding..If you take a look at the content of the jsp code you may get an idea of how the data is coming...Now i would be greatful if you could help me out in this matter...


0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1000 total points
ID: 16883231
Alright, lets try writing the jsp page something like following...

<%@ page session="false" %>
<%@ page import="java.sql.*"%>
<%@ page import="com.inat.db.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="com.inat.bo.Status"%>
<%@ page import="com.inat.bo.StatusEntry"%>
<%
    Enumeration entries = Status.getStatus(selDate);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 24; i++) {
    String ts = i + ":00";
sb.append(ts);
sb.append(",");
}
sb.append("\n");
while(entries.hasMoreElements()) {
    StatusEntry entry = (StatusEntry)entries.nextElement();
    int numDataArrays = entry.getNumDataArrays();    
sb.append(entry.getName());
sb.append(",");
String filename = "DataLoadFile.csv" ;
response.setContentType("text/csv; charset=ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
out.write(data.toString());
 %>

try this page & see what it generates.. there might be some syntax errors which I am sure you can fix..
0
 

Author Comment

by:raghuudupa
ID: 16884850
Thanks Kuldeep,
  I appreciate your help...I will try writing this page and see wht it generates and get back to you..
I am glad you responded...
Thanks once again..
0
 

Author Comment

by:raghuudupa
ID: 16895813
Hey Kuldeep,
  I was wondering what data.toSting() will return...What is this data?? I guess it is sb.toString() right??

One more question is...this is a jsp page that is being generated every day, meaning we have data pertaining to each day as you can see from the code in the jsp...There is a calendar tool on the top...
How many csv files will we end up creating...??
Ihope you understood my question?? If for example we decide to write it to a csv format.We click the link that we provide in our original jsp...When the user clicks we get a new jsp which has content of the original jsp written in comma seperated format...What about the file that is being generated.??(Data.csv)..
Will the file get overwritten each time we click the hyperlink ??(ie every day) or will new files be created each time??


0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 16899921
yes it should be sb.toString(), not data.toString()..

Secondly this code is not writing any CSV file..!!, its just giving the data in CSV format to the client..... ( i.e. Browser)..
it depends on the user how he/she wants to save it..

if you want to write it to a file on the server then you dont have to send the to out.write...

you will have to open a file on the server, give a name to it & then write to the file stream rather than writing to the out....

if the name of the file is hardcoded, it will get overwritten every time the page is called... this problem can be taken out by appending a timestamp at the end of the file name to make the file name unique..
0
 

Author Comment

by:raghuudupa
ID: 16901349
Thanks Kuldeep,
  I appreciate your explanation...I was under the impression that the lines..
response.setContentType("text/csv; charset=ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
  Would open a save/saveas dialog box and let the user save as a file....
Any way i appreciate your advice and i will come back to you if i need some more assistance..

0
 

Author Comment

by:raghuudupa
ID: 16962914
hey kuldeep ...
the code that you sent me is not working in the right manner..The comma that you are appending is i guess at the wrong place....

iam pasting the data  on my jsp here....
BSC           0:00  1:00  2:00  3:00  4:00  5:00  6:00  7:00  8:00  9:00  10:00  11:00  12:00  13:00  14:00  15:00  16:00  17:00  18:00  19:00  20:00  

PHBSC56  tch=174
               ho=1703
               tchsummary=1
               hosummary=1
               Info , Errors

PHBSC57  tch=123
               ho=989
               tchsummary=1
               hosummary=1
               Info , Errors

I need the data being displayed in CSV format....
The code that you have sent i guess will put a comma"," near the time and the BSC nos...
I need the out put like PHBSC57: tch=123,ho=989,tchdummsty=1,hosummary=1

I hope what i have mentioned is clear..Please suggest...
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 16967021
so do you need the time part in the output or not?? if not then you can take out the first loop

<%
    Enumeration entries = Status.getStatus(selDate);
StringBuffer sb = new StringBuffer();
//This loop is putting time & comma after each time entry..
for (int i = 0; i < 24; i++) {
    String ts = i + ":00";
sb.append(ts);
sb.append(",");
}
//Remove up to this plase if its not needed
sb.append("\n");
while(entries.hasMoreElements()) {
    StatusEntry entry = (StatusEntry)entries.nextElement();
    int numDataArrays = entry.getNumDataArrays();    
sb.append(entry.getName());
for (int hour = 0; hour < 24; hour++) {
               String code = "success";
               String msg = "";
               for (int i = 0; i < numDataArrays; i++) {
                   int[] data = entry.getDataArray(i);
                   if (data[hour] <= 0) {
                       code = "error";
                   }
                   msg = entry.getDataArrayName(i) + "=" + data[hour] ;
sb.append(msg)
sb.append(",");
}
}

Try this
0
 

Author Comment

by:raghuudupa
ID: 17003596
Hey thanks

Will get back and award your points....
This problem has taken a backseat now as i have been given another project(short term) for a few days..
Got tomonitor that...
But will surely come back and let you know once done..
I appreciate it ...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
Last month Marc Laliberte, WatchGuard’s Senior Threat Analyst, contributed reviewed the three major email authentication anti-phishing technology standards: SPF, DKIM, and DMARC. Learn more in part 2 of the series originally posted in Cyber Defense …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question