Hi everyone welcome to studywithdemo. Today im very much interested to tell about how to create random jquery progress bar using ajax and servlet because this is the first tutorial i have done after learning jquery. Jquery is one of the favorite subject to me. Using jQuery most of the work becomes easy and it makes UI more interactive and rich look.

Before going into the example I want to tell one thing here am not covering any file upload and download progress bar only showing how progress bar will works on server side. This example completely not suitable for everyone but definitely creates a basic idea, using this you can modify and implement code according to your requirements. To create random progress bar I have used ajax and servlet as serverside technology.

Random Progress bar

Each time ajax pass a value to servlet and servlet increases number randomly and sends the response back this will continue untill the progress bar will complete 100% thats it just simple logic no need to worry look at the code. We can also increase or decrease the progress bar speed by changing the setTimeout value and easily customize the progressbar label text.

 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Random;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 public class ProgressBarServlet
   extends HttpServlet
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException
    PrintWriter out = response.getWriter();
    Random r = new Random();
      int pval = Integer.parseInt(request.getParameter("pval"));
      float randomval = r.nextFloat();
      int seedval = (int)(10.0F * randomval);
      out.print(pval + seedval);


<html lang="en">
  <meta charset="utf-8" />
  <title>jQuery UI Progressbar Random</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  .ui-progressbar {
    position: relative;
  .progress-label {
    position: absolute;
    left: 50%;
    top: 4px;
    font-weight: bold;
    text-shadow: 1px 1px 0 #fff;
  var pval=0;
  $(function() {
    var progressbar = $( "#progressbar" ),
      progressLabel = $( ".progress-label" );
      value: false,
      change: function() {
        progressLabel.text( progressbar.progressbar( "value" ) + "%" );
      complete: function() {
        progressLabel.text( "Complete!" );
function progress() {
 var xmlhttp;
if (window.XMLHttpRequest)
  xmlhttp=new XMLHttpRequest();
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
      pval=xmlhttp.responseText;//getting result from servlet
      progressbar.progressbar( "value",parseInt(pval));//converting string result to integer
    xmlhttp.open("GET","ProgressBarServlet?pval="+pval,true);//value sending to servlet
     var val=parseInt(pval);
     if ( val < 100 ) {
        setTimeout( progress, 1000 );
 <div id="progressbar"><div class="progress-label"></div></div>