[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

jsp page AND dynamic display

here is a problem.

i have a servlet which is calling 5 threads. threads are doing individual work. servlet

showing a jsp page.i want my jsp page should indicate the status of the threads.

like this ....


jsp page
----------

thread1 ....completed // as soon as thread1 completed.
thread2.....completed. //as soon as thread2 completed.


that means when a thread will complete its task the jsp page will update/refresh itself and

display like above.




i have been able to run 5 threads inside the servlet and printing those status in the

console...but how do i show them in the jsp page dynamically? how do i carry those status to

the jsp page ?

in fact it is the problem of dynamic updation.


if it is solvable plz give a very small example code to test with.

thanks all.



N.B >my servlet is a action servlet [ struts ] and target is a jsp page. application server

is SunOne6.1

 
0
cofactor
Asked:
cofactor
  • 2
  • 2
1 Solution
 
kiranhkCommented:
0
 
aleksmezaCommented:
 Here's a solution I figured out and could be good for you. You need a class for controlling this five threads that i'm going to call ProcessPool. Use this class as a java bean in a jsp with a session scope so when the jsp ask for this ProcessPool you can then start the 5 processes or see the progress of each one.
 
  When the jsp is reloaded every 5 seconds(for example) you then ask for the ProcessPool and ask the progress for each process, and when you see that all 5 processes have been done you stop refreshing.

    public class Process {
       private String name;
       private double pctComplete = 0.0d;

       // constructors and setter and getters for attributes
        .....
        .....
      public void exectue() {
         // here's your code for creating the thread and execute all you want
      }  
    }


    public class ProcessPool {
       Process [] processes;

      public ProcessPool() {
         processes = new Process[5];
      }  
     // methos for strarting each process
     public void startProcess(int num) {
        processes[num].execute();
     }

     public double getPctComplete(int numProcess) {
        return processees[numProcess].getPctComplete();
     }


    }

   You then only have to make adjustments in the jsp to know when is the first time is executed and  when to stop refreshing. I also suggest that you se the Java Patterns: Worker Thread Pattern and Singleton Pattern for a more professional solution

   Alex Meza
   
0
 
cofactorAuthor Commented:
hi....aleksmeza ...

your solution seems easy and  interesting....

i have some query  .

1. what is  pctComplete ?

2. how u are calling from JSP page ?

do u mean to use  request.getParameter() ?

can u plz tell me how u are calling processpoll bean in the jsp page ?

how jsp page is  reloading  after 5 secs ? do i need to use javascript function setTimeout() ?


can u ellaborate something more. i have understood ur design but  have some confusion how u r calling it in jsp ?

0
 
aleksmezaCommented:
1.- pctComplete. It means percentage complete. The thred must be report to the ProcessPool object the percentage it has already done, this measure is the one that you refresh every time you refresh the jsp.

2.- In the jsp you must use the "useBean" directive like this:
 
    <jsp:useBean id="pool" class="Your.Package.ProcessPool" scope="session" />
 
     <%
            // Here is the part of the code where you access the pool
           
         // I'm adding a new methos to the class, is firstTime, with this method you tell if the process has been started or not
         // in the contructor it must be assigned to false
            if( pool.firstTime()) {
                 // 'casue of the first time we satrt the processes
                 pool.startProcess(0);
                 pool.startProcess(1);
                 pool.startProcess(2);
                 pool.startProcess(3);
                 pool.startProcess(4);
            }

            // then you print porcentage complete
            out.println("Process #1 = " + pool.getPctComplete(0));
            out.println("Process #2 = " + pool.getPctComplete(1));
            out.println("Process #3 = " + pool.getPctComplete(2));
            out.println("Process #4 = " + pool.getPctComplete(3));
            out.println("Process #5 = " + pool.getPctComplete(4));

 
      %>

  3.- for the timeout you can use javascript combined with jsp. I'm going to add a new method for the ProcessPool class, it's gonna be hasProcess() this mehtod should return true if there is at least one process running and false if all the process have been completed.

 <% if(pool.hasProcesses()) { %>
  <script languaje="javascript">
     setTimeout('reloadPage()',5000)
     
     function reloadPage() {
       window.location = 'yourPage.jsp';
     }
  </script>
  <% } %>

   With this last portion of code you would be reload you jsp every 5 seconds till hasProcesses return false


0
 
cofactorAuthor Commented:
hi aleksmeza ,

thanks a lot for those detailed explanation. i understand your code but unfortunately till now i could not make it run. i am not  experienced.  



ok let me tell you what i have done so far.

i have created  a package folder in /ROOT/WEB-INF/classes/my/pack/---directory to keep java package.



For Simplycity let us take the job of the 5 threads  are just sleeping random amount of time.


so i have  wrote a java file ( mostly copied from yours + added a little bit )

here is the file in classes/my/pack directory.







ProcessPool.java
---------------


package my.pack;
 
import java.util.*;

import java.io.*;





class CommonThread extends  Thread
{
         public void run()
         {
         Random r = new Random();
         int d = r.nextInt(50000);
         try
         {
         
         System.out.println(d);
         Thread.sleep(d);                // thread sleep for random time.
         }catch(Exception e){System.out.println(e);}
         
         }

         public void getPctComplete()
         {
         System.out.println("time="+System.currentTimeMillis()); // how much it has slept       
         }

}



       

class Process
{
       private String name;
       private double pctComplete = 0.0d;

       
       public void setName(String s){ name=s;} // process name
       public String getName(){ return name;}
       
       
       public void execute()
        {
        CommonThread t = new CommonThread();
        t.start();
        }  

}






       
public class ProcessPool
{
       
       Process [] processes;
       boolean bool;

        public ProcessPool()
        {
         processes = new Process[5];
         bool = false;
     
        }  
   
   
     public void firstTime()
     {
     bool=true;      
     }
   
     
     public void startProcess(int num) {
        processes[num].execute();
     }

     public double getPctComplete(int numProcess) {
        return processes[numProcess].getPctComplete();
     }


    boolean hasProcess()
    {
    for(int k=0;k<5;k++)
    {
    if(processes[k].isAlive()) return true;       // if any thread alive
    }
    return false;
    }


}







and here is the jsp in /ROOT directory.




myjsp.jsp
-------------

<jsp:useBean id="pool" class="my.pack.ProcessPool" scope="session" />



<%
           

            if( pool.firstTime())
            {
               
                 pool.startProcess(0);
                 pool.startProcess(1);
                 pool.startProcess(2);
                 pool.startProcess(3);
                 pool.startProcess(4);
            }

            out.println("Process #1 = " + pool.getPctComplete(0));
            out.println("Process #2 = " + pool.getPctComplete(1));
            out.println("Process #3 = " + pool.getPctComplete(2));
            out.println("Process #4 = " + pool.getPctComplete(3));
            out.println("Process #5 = " + pool.getPctComplete(4));

 
%>


<% if(pool.hasProcesses()) { %>
  <script languaje="javascript">
     setTimeout('reloadPage()',5000)
     
     function reloadPage() {
       window.location = 'myjsp.jsp';
     }
  </script>
  <% } %>







Question
_________

class is not compiling. i know some mistake there but i dont know the way out , for example isAlive() has been applied inappropriately. firstTime() method also wrong. and all the classes are in ProcessPool.java.


can u  correct me to complete this stuff ?  i want to visualize the whole thing.

the job of the threads are sleeping. this may not ( i doubt ) help me to visualize the thing, if u want, u can change it to some other jobs.


i appreciate your  help.i am in touch. sorry for late reply.

Thanks a lot.


<cofactor>

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now