Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: JSP help

  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JSP help

    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

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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 class.

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

    Outline
    Code:
    //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;
    }
    
    }

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #6
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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
    Code:
    <%
    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.

    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •