最近的一個項目需要多個硬件設備發送帶參數的url,我們能在web頁面實時顯示所有參數。
如url:localhost:8080/webtest?id=1&status=2,我們需要把id以及status讀取出來實時傳遞到頁面
很明顯,url訪問servlet,但是服務器不能主動向jsp頁面推送數據,所以我們這裏用了ajax(當然你對websocket很熟悉也是可以用的,順便說一句,老哥們真的會的話麻煩給小弟一份demo)
所以簡單的做了一個java web項目,數據全部存儲在內存中。想法是jsp頁面中通過ajax輪尋訪問一個servlet a獲取靜態變量中存放的數據,所有的硬件設備訪問servlet b存儲數據
目錄結構如下
AjaxServlet.java
package servlet;
import utils.JavaParamConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedList;
/**
* @Version 1.0
* @Author Martin
* @Date 2019-04-22 00:36
*/
@WebServlet(name = "AjaxServlet", urlPatterns = "/servlet/AjaxServlet")
public class AjaxServlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("servlet init");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (JavaParamConfig.changeflag){
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
dealWithInfoList(JavaParamConfig.infoList);
JavaParamConfig.changeflag = false;
}
resp.getWriter().write(JavaParamConfig.allInfoTable.toString());
}
private void dealWithInfoList(LinkedList<String> linkedList) {
int len = linkedList.size();
for (int i = 0; i < len; i++){
String[] str = linkedList.peek().split("=");
JavaParamConfig.allInfoTable.computeIfAbsent(str[0], k -> str[1]);
JavaParamConfig.allInfoTable.put(str[0], str[1]);
linkedList.pop();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("nihao");
}
}
TestServlet.java
package servlet;
import utils.JavaParamConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Version 1.0
* @Author Martin
* @Date 2019-04-22 00:36
*/
@WebServlet(name = "TestServelt", urlPatterns = "/servlet/TestServlet")
public class TestServlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("servlet init");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
String status = req.getParameter("status");
String info = id + "=" + status;
JavaParamConfig.infoList.add(info);
JavaParamConfig.changeflag = true;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
JavaParamConfig.java
package utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
/**
* @Version 1.0
* @Author Martin
* @Date 2019-04-28 21:43
*/
public class JavaParamConfig {
//存儲所有設備ID以及狀態
public static Hashtable<String, String> allInfoTable = new Hashtable<String, String>();
//模擬消息隊列
public static LinkedList<String> infoList = new LinkedList<String>();
//是否有數據變化的標識位
public static boolean changeflag = false;
}
index.jsp
setInterval定時每4s調用一次test方法
這裏的ajax是通過jquery實現的,原生js實現的自行百度,至於jquery,其實就是一些js代碼的封裝包而已
<%--
Created by IntelliJ IDEA.
User: matian
Date: 2019-04-29
Time: 12:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>沒有BUG</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function test() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
$.get("servlet/AjaxServlet", function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$("#somediv").text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
}
</script>
<script>
setInterval(test, 4000);
</script>
</head>
<body>
歡迎登陸
<div id="somediv"></div>
</body>
</html>
至於MethodUtils.java中的內容本來是想封裝一些方法比如輸出個時間啥的,但是沒有用到
代碼到這裏就結束了。
測試結果
新開的兩個頁面只是爲了測試傳url以及參數,可以看到兩個url中的數據都被讀取出來了
ps:講道理,這些數據都得存數據庫中。等有時間再改進。