PDA

View Full Version : JSP help


jre_prstqmp
01-23-2009, 02:27 AM
Hi all,
I am having trouble developing a web page with JSP. Actually I have a class which gathers a huge data(array of objects) once every 1 minute (data changes every time). I have to populate this data in a webpage using JSP.
I am not sure how I can make the gathered data available in JSP. Calling the class inside JSP will rerun the class (the class execution frequency or time of call should not be altered by the jsp or anything). Is there anyway I can make the data collected available inside the jsp as a global variable or something?
If required i can write the data from the class to a file as well.

Thanks for your input on this

servlet
01-23-2009, 05:20 AM
Whats the signature of your class, can you post the code here?
It seems that the Array could be static as its not bound to a particular instance. I can help you If I can see the code.

jre_prstqmp
01-23-2009, 07:15 AM
Thanks Servelet.
The class code is quite big to paste. In a nutshell the code gets the unix FS information every 60 seconds. This data needs to be populated to a page for whoever browsing it.
The class uses array of 1000+ objects containing (string, int, int, int).

BTW, I am pretty new to Java let alone JSP. Would really appreciate any help. And I am not sure how to make that data static if it is a good idea? . A simple example if you can provide/refer would be great.

servlet
01-23-2009, 07:42 AM
What I understand is, the class keeps fetching some data from some where (UNIX FS or whatever) and puts the data in a Array of objects. the objects inside the array are the actual information fetched by the class.

Now the requirement is to display this data on the web using JSP. but the execution of the class can not be altered, not even a new instance of the class should be created.

Potential solution.

The design of the class indicates that it could be a singleton (http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html)class.

Again the array can be a static variable inside the class, and you can expose it to JSP using the getter method.

Outline

//this class could be singleton
public class DataFetcher {

//array which holds the objects

private static Object[] data = null;


//method that would expose the data to a JSP

public static Object[] getData() {
return data;
}

}

shyam
01-23-2009, 03:22 PM
make your class generate a html report every time it does its stuff and simply overwrite that file each time ur class runs. and include the html report in your jsp.

ps.
btw, you don't have to post the entire code just the relevant portions that you need help with

jre_prstqmp
01-23-2009, 05:04 PM
Thanks servlet and Shyam,
If i change the data to static, not sure how to access it from JSP using getter. Any help/example pls?
Also, any data on generating 'html reports' also would be appreciated. I have no idea about it.


Here are the class code details...

public class getStats{
...//some classes here to serialize and write/read data collected to/from a file (for future use for archiving etc)
...
...
public static void main(String [] args)
{

StatusInfo[] StatusInfoArray = new StatusInfo[1500];
//here I am gathering the data and assinging them to an array of objects of class StatusInfo
}
}

class StatusInfo implements Serializable{

private String name;
private int age;
private int enq;
private int depth;

//set and get methods for each of the above

}


Now inside the JSP, I am using a loop to get the contents of each object from the array and populate the html code. But the problem is how to make the above data available here. The only restriction is to run the method to gather the data ONLY once every minute due to performance issues, but new instance can be created as long data gather method is not called)

Thanks

servlet
01-27-2009, 06:26 AM
Did u look at the singleton pattern, I mentioned in earlier post?

How to access the data from JSP:-

Make the method getStats() static in DataFetcher class.

Now inside JSP

<%
StatusInfo[] = DataFetcher.getStats()
//it returns the array.

for(int i=0; i < statusInfo.length; i++ ) {
//do whatever you want. generate HTML
}

%>


How to create the instance of DataFetcher in JSP?:-

You don't need to create the instance. as getStats() method is static, you can call it using class name.

If DataFetcher is singleton, as I said in earlier post.
You can call DataFetcher.getInstance() method to obtain the reference.


DataFetcher fetcher = DataFetcher.getInstance()

StatusInfo[] = fetcher.getStats();


If you use singleton, then you don't need static method. it's your choice.

I will prefer singleton over static.