Java工作笔记-使用jquery.timer.js实现数据自动刷新
生活随笔
收集整理的這篇文章主要介紹了
Java工作笔记-使用jquery.timer.js实现数据自动刷新
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
程序運行截圖如下:
?
程序結構如下:
?
源碼如下:
HelloServlet.java
package my;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class HelloServlet extends HttpServlet {/*** Constructor of the object.*/public HelloServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {int n = new UserStat().count();String replyText = String.valueOf(n);response.setContentType("text/plain");response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();out.write(replyText);out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/plain");response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();out.write("Not Supported");out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here} }UserStat.java
package my;public class UserStat {public int count(){return (int)(Math.random() * 1000);} }timer.jsp
<%@ 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"> <html><head><base href="<%=basePath%>"><title>My JSP 'new.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"><script src="jquery/jquery.js"></script><script src="bootstrap/js/bootstrap.min.js"></script><script src="jquery/jquery.timer.js"></script></head><script >function trace( msg ){ try{ console.log(msg); } catch(err){ }}function onQuery(){jQuery.ajax({ method: "GET", url: "servlet/HelloServlet",success: function(data, textStatus, jqXHR){$("#userCount").html(data);},error: function( jqXHR, textStatus, errorThrown){trace( "error: " + errorThrown ); }});}// 頁面加載后的初始化工作$(document).ready( function(){var timerId = $.timer(1000, onQuery, false);});</script><body><div class="container"><p> 當前共 <span id="userCount"> 0 </span> 人在線 </p></div></body> </html>?
總結
以上是生活随笔為你收集整理的Java工作笔记-使用jquery.timer.js实现数据自动刷新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt文档阅读笔记|Qt工作笔记-QMut
- 下一篇: C++设计模式-继承与多态影响耦合性(最