There is now a requirement to communicate between the browser and the server based on WebSocket. Since I had not used WebSocket before, I did a rough search for WebSocket integration solutions. The solutions we look for on the Internet are as follows: 1. Implementation based on Spring, add Spring-WebSocket dependency, add annotations, configure corresponding interceptors and paths; 2. 2. Based on Spring Boot, essentially based on Spring; 3. Based on J2EE application server, it seems that Tomcat7 has provided WebSocket support, which can be realized directly with @WebSocketEnd, which is provided by Servlet package;

One feature of them is that they are implemented based on servlets (the third option has not yet been proven).

However, due to various reasons, I needed to implement this process based on Struts2, that is, based on Filter. After a long search, I found that the solution was: create a path in Struts2 interceptor using metacharacters, and then intercept it again. I failed!

<constant name="struts.action.excludePattern" value="/ws/.*,ws://.*"></constant>

Finally, the WebSocket scheme is implemented by Spring + annotation.

For other reasons, it is inevitable to move the spirngmvc (servlets) based solution to struts (Filer). Therefore, after a lot of thinking and practice, I finally found the corresponding solution, which is recorded here in order to help my colleagues to solve the corresponding needs:

First, use the stack trace to find the execution flow of the websocket servlet:

The stack is as follows:

Then, combined with the execution process of Spring MVC, the analysis is carried out:

After analysis, we can move the whole model logic from the servlets to the corresponding Filter, but in the face of many configurations, how to smooth the migration is a problem, I dealt with this way, on the MVC side of the execution, put a breakpoint, look at the corresponding memory variable types, and then manually construct. The original code looked like this:

In practice, those many configurations and @Configuration are used for Spring MVC Configuration, as well as local parameter initialization here. So I could have built one by hand.

The code I created is as follows:

The rest of the logic is based directly on Struts. After practice, it is feasible and easy to use. No other problems have been found yet.

Application case renderings