wood burning stoves 2.0*
The moose likes JSP and the fly likes A Design Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "A Design Question." Watch "A Design Question." New topic
Author

A Design Question.

Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Hi Folks!
I have some JSP pages which display data from DB based on some criteria. From the browser i need to save them in HTML and CSV files. Reconstructing the URL and saving in a different content-type will alone won't work for me. Because i need to save exactly the same data.
What could be the efficient way to achieve this?
regards
Sudharsan
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I'm having difficulty understanding exactly what you want to do.
You have a JSP which is generating some HTML and/or CSV data, which you can see in you browser, yes? And you need to save this output to disk?
How is what you want different from just using "save as" from the browser "file" menu?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Is your problem the possibility of changes in the data taking place between when the client views it to when the client saves it? If this is the case then you could always just shove the content into the user 's session and retrieve it when they need to save (assuming the amount of data is relatively small).
I guess we need more information to go on before we can really help.
Sandep Chaturvedi
Ranch Hand

Joined: Aug 20, 2002
Posts: 49
What you can do is:
Create javabean wrapper for all your data. From jsp, use this javabean to retrieve the data in html format. Provide a link to a servlet where you read from the bean and write to the response (after setting the content type). Caution to be taken is: this works correctly for tabular data. else does not make sense.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Chris!
You are right. The differences in the DB data at the time of the 'page request' and 'User Save' should not be visible to the user. I cant save them in sessions because the amount of data would be huge and varying..
Frank,
There are 2 reasons why i cant use the 'File/Save As' menu.
1. I need to save in 'CSV' format also
2. The page itself will have buttons to save them in HTML and CSV format. Is there any way to access the 'File/Save As' menu from Javascript?
Sandep!
The data will not be in tablar format always. They are actually 'Reports'. So, some complex db queries are involved.
regards
Sudharsan
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Sudharsan G
JavaRanch has a Naming Policy Following it is a requirement for entry to the Book Giveaway. Please change your Display Name to comply.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Sudharsan G:
Chris!
You are right. The differences in the DB data at the time of the 'page request' and 'User Save' should not be visible to the user. I cant save them in sessions because the amount of data would be huge and varying..
Frank,
There are 2 reasons why i cant use the 'File/Save As' menu.
1. I need to save in 'CSV' format also
2. The page itself will have buttons to save them in HTML and CSV format. Is there any way to access the 'File/Save As' menu from Javascript?
Sandep!
The data will not be in tablar format always. They are actually 'Reports'. So, some complex db queries are involved.
regards
Sudharsan

So, we can't just do a re-query using the same parameters, because the database state may change between query invocations. So, a "snap-shot" of the database needs to be maintained for this report. I would recommend an XML solution. You could cache the results of the query in an XML document somewhere (file system or in the database itself). When the user wants to view the report online, just style the XML as HTML. When they want to save it, style it as CSV. The big issue here is cleaning out this XML cache. You have to "expire" the results somehow. I would suggest somehow scheduling (a chron job perhaps) a cleanup procedure which uses either a timestamp in the database or the file creation time for the file system alternative. In EJB 3.0, there's going to be a scheduling service (I believe that's what it's called) available for just this purpose, but for now we have to rely on the not-so-portable solutions.


James Carman, President<br />Carman Consulting, Inc.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Originally posted by James Carman:

The big issue here is cleaning out this XML cache. You have to "expire" the results somehow. I would suggest somehow scheduling (a chron job perhaps) a cleanup procedure which uses either a timestamp in the database or the file creation time for the file system alternative. In EJB 3.0, there's going to be a scheduling service (I believe that's what it's called) available for just this purpose, but for now we have to rely on the not-so-portable solutions.


Could he use an HttpSessionListener? It could call the database or whatever was being used to clear out the XML documents associated with the user when their session dies...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
That sounds like a nice solution, also. In the long run, the whole process would have to be monitored and tweaked to make sure you're not overusing valuable system resources. But, your suggestion provides a quick and easy alternative to a scheduled job. Thanks!
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Hi!
I'm now using exactly what James and Nathan have specified. But i'm storing it as a HTML disk file instead of an XML file. I dont understand what do you mean by styling an XML to HTML or CSV. I can go for this solution if i'm more clear on it.
And i'm now implementing HttpSessionBindingListener for the clean up process. The valueUnbound(HttpSessionBinding Event) method will contain the code for clean-up. I thought this would be more advantagesous than a HttpSessionListener. As implementing a HttpSessionListener needs an entry in the web.xml. But for using the HttpSessionBindingListener, the object which implements HttpSessionBindingListener needs to be stored in the session and it's valueUnbound method will be executed on invalidation of the session.
regards
Sudharsan
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Sudharsan G:
Hi!
I'm now using exactly what James and Nathan have specified. But i'm storing it as a HTML disk file instead of an XML file. I dont understand what do you mean by styling an XML to HTML or CSV. I can go for this solution if i'm more clear on it.
And i'm now implementing HttpSessionBindingListener for the clean up process. The valueUnbound(HttpSessionBinding Event) method will contain the code for clean-up. I thought this would be more advantagesous than a HttpSessionListener. As implementing a HttpSessionListener needs an entry in the web.xml. But for using the HttpSessionBindingListener, the object which implements HttpSessionBindingListener needs to be stored in the session and it's valueUnbound method will be executed on invalidation of the session.
regards
Sudharsan

The reason that I suggested XML in this situation is that you basically have a situation where the same set of data needs to be displayed in two different ways. So, the data can be embodied in an XML document and that document can be transformed (via XSLT or XSL-FO) into the desired format. I guess I meant transformed instead of styled, really. Sorry. :-)
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
When i try to create an XML/HTML file for the DB snapshot, i feel the content and presentation are very tightly woven. The whole point of using a JSP itself becomes invalid. Is this a good design?
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
I am a bit confused. How does creating an XML document couple the content with the presentation? The point James was trying to make is that you could create an XML representation of your data and then use XSLT to transform the XML into various presentations. I don't necessarily agree with this approach for your particular problem (I think it is overkill) but it is definitely a valid solution and in no way ties content to presentation.
[ August 28, 2002: Message edited by: Chris Mathews ]
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
As i need to show the content in browser and store it in a file also, what i do now is append everything into a string buffer like
sbout.append("<TD> " + strVal + " </TD");
And then i will write the string Buffer into the out.write(sbout.toString()) and to the file like
fout.write(sbout.toString()). But the JSP page which contains this code becomes highly un-readable.
Otherwise i'm okay with James' idea of storing in XML and using XSLT to transform it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Design Question.