ajax+servlet实现轮寻内存中的数据
最近的一个项目需要多个硬件设备发送带参数的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:讲道理,这些数据都得存数据库中。等有时间再改进。