JQuery ProgressBar Implement background progress real-time functionality via AJAX requests _jquery

Source: Internet
Author: User
Tags error handling set cookie setcookie

This article mainly demonstrates the progress bar function of jquery ProgressBar. JS obtains the current progress value in real time through Ajax request to the background. The background stores the progress value in a cookie, and after each request, it increases the value of the progress bar by 2. This demonstrates the real-time display of the progress bar.

Front desk index.jsp

The JSP code is as follows

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <!--<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >--> <! DOCTYPE html>  

configuration of Struts.xml files

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public
 "-//apache Software foundation//dtd struts Configuration 2.0//en"
 "http:// Struts.apache.org/dtds/struts-2.0.dtd ">

<struts>
 <constant name=" Struts.devmode "value=" true "/>
 <package name=" front "extends=" Struts-default "namespace="/">
  <action name=" Getprogressvaluebyjson "class=" edu.njupt.zhb.test.TestAction "method=" Getprogressvaluebyjson ">
   <result Name= "Success" ></result>
  </action>
  <action name= "Testaction" Edu.njupt.zhb.test.TestAction ">
   <result type=" Httpheader "></result>
  </action>
 </package>

</struts>

background Java code ()

Package edu.njupt.zhb.test;
Import java.io.IOException;

Import Java.io.PrintWriter;
Import Javax.servlet.http.Cookie;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionSupport; * * @author: Zhenghaibo *WEB:HTTP://BLOG.CSDN.NET/NUPTBOYZHB *mail:zhb931706659@126.com *sep, 2013 Nanjing,njup T,china */public class Testaction extends Actionsupport {/** * */private static final long Serialversionuid =-86
 97049781798812644L;  /** * Gets the ProgressBar value in JSON format via AJAX * type:action/public void Getprogressvaluebyjson () {String progressvaluestring =
 GetCookie (Getrequest (), "Progressvalue");
 int progressvalue = Integer.parseint (progressvaluestring);
 if (progressvalue>100) {progressvalue = 0;
 } System.out.println ("GetCookie:---progressvalue=" +progressvalue); 
 Writejsonstring ("{\" progressvalue\ ": \" "+ Progressvalue +" \ "}"); Progressvalue += 2;
 Setcookie (GetResponse (), "Progressvalue", progressvalue+ "", 365*24*60*60); /** * Get httpservletrequest Object * @return httpservletrequest/public httpservletrequest getrequest () {Retu
 RN Servletactioncontext.getrequest (); /** * Get httpservletresponse Object * @return httpservletresponse * * protected httpservletresponse GetResponse ()
 {return servletactioncontext.getresponse (); /** * Get printwriter Object * @return PrintWriter * @throws ioexception * * protected PrintWriter getwriter () thr
 oWS IOException {return this.getresponse (). Getwriter (); /** * Write JSON format string * @param JSON */protected void writejsonstring (string json) {try {GetResponse (). Setcontentty
 PE ("Text/html;charset=utf-8");
 This.getwriter (). write (JSON);
 catch (IOException e) {e.printstacktrace (); /** * Get Cookie * @param request * @param name * @return string/public static string GetCookie (HttpServlet
Request request, String name {string value = null; try {for (Cookie c:request.getcookies ()) {if (C.getname (). Equals (name)) {value = C.getvalue ();
 A catch (Exception e) {e.printstacktrace ());
 return value; /** * Set Cookie * @param response * @param name * @param value * @param period/public static void Setcookie (
   HttpServletResponse response, String name, string value, int period) {try {Cookie div = new Cookie (name, value);
   Div.setmaxage (period);

  Response.addcookie (DIV);
  catch (Exception e) {e.printstacktrace ();

 }
 }
}

Run

After you deploy the project to Tomcat, you can see that the progress bar is gradually updated by entering the URL in the browser

SOURCE Download: Http://xiazai.jb51.net/201610/yuanma/jqueryProgressbar (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.