First, the process:
1. Accessing the startup Task Servlet
2. Start the task servlet to open the task and jump off the progress bar display page
3. The page is ready to call (the progress parameter gets the servlet), and then the progress bar shows the page is called periodically until it is passed over to 100%.
4. Get the parameter servlet takes the task out of the session and gets the progress parameters in the task. Returns the progress parameter to the progress display page.
Here are the individual file codes, works for Web engineering.
1. Progress bar Schedule.css File:
body { margin:0; padding:0; Font-family: "The song Body"; font-size:12px; line-height:1.8em; color: #392b60; height:100%; }/* the outermost div style */. progressoutside { border:red 1px dotted; /* border 1 pixels of red dashed */ width:350px; /* width is 350 pixels */ position:relative; /* relative body position */ margin-left:-175px; /* the entire div width is 350,-175px or 175 pixels to the right. Just centered .*/ left:50%; /* the left border of the browser is half the screen. */ margin-top:150px; /* the top of the frame is relative to the body 35%*/}/* Title div style */.progresstitle { border:greenyellow 1px solid; line-height:30px; /* Set line height padding:0 15px 0; /* inner fill Top :0, right: 15 pixels, under: 0*/}/* content div Style */.PROgresscontent { border:purple 1px double; padding : 15px; height:40px !important;} /* Progress bar Box */.progress { background: #f0f4f9 url (.. /imgs/2.gif) repeat-x top; border:yellow 2px solid; height:15px; margin:10px 0 0 0 !important; margin : 35px 0 0 0;} /* Progress bar entity */.progressschedule { border:red 1px double; background:url (.. /imgs/1.gif) repeat-x left top; width:3%; height : 15px;} /* Progress Tip box */.progresspercent { text-align:center; border:green 1px solid; height:15px; position:absolute; display:block; left:125px; margin-top:-17px; width:100px;} /* percent hint box */.textprogress { border:orange 1px solid; font-weight:bold; font-family:tohama;}
2. The paging file invoke.jsp used jquery.
3. Ajax get.js files used
$ (document). Ready (function () {getpercent ();}); Function getpercent () { $.post ("process", {userName: ' ab '},function (data) { styleId.style.width=data; $ ("#bfb"). Text (data); if (data!= ' 100% ') { window.settimeout (getpercent,100); }else{ if (Confirm ("finished?")) { window.location.href= "/servlet1/index.html"; } } });} 4. Start thread servlet Index.java
protected void dopost (HttpServletRequest request, httpservletresponse response) throws servletexception, ioexception { Progress p = (Progress) request.getsession (). getattribute (" Progress "); if (P!=null) { System.out.println ("thread running!"); throw new runtimeexception ("Thread is occupied!"); }else{ progress progress = new progress (); request.getsession (). SetAttribute (" Progress ", progress); progress.start (); } response.sendredirect (Request.getcontextpath () + "/invoke.jsp");}
5. Thread task Class-the task to be done is written in this inside Progress.java
public class progress extends thread{ logger logger = logger.getlogger (Process.class); //getter method omitted private String percent = ""; //getter method omitting private boolean over = false; public void run () { for (int i=1;i<=100;i++) { try { thread.sleep (1*100); } catch (interruptedexception e) { e.printstacktrace (); } percent = i+ "%"; } if (logger.isdebugenabled ()) { logger.debug ("thread run over!"); } over = true; }} 6. Take parameter servlet--> This servlet is a call to be timed by the page, when Process.java
is called, the current task thread is fetched, the progress parameters in the current task thread are fetched, and the progress parameters are passed to the page.
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Progress p = (Progress) request.getsession (). getattribute ("Progress"); if (p!=null) {String percent = p.getpercent (); Request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). write (percent); } if (P.isover ()) {request.getsession (). RemoveAttribute ("Progress"); }}