简体   繁体   中英

Is possible to exchange the http requests and responses on a Java Servlet environment?

I'm developing a server control to avoid duplicate processing of same request type on a Java server side. The main goal is avoid that an user could flood my server processor by just clicking on an hard processing request repeatedly. I'm developing a Servlet Filter to control the flow of requests, but I can just "abort" the second request by returning a HTTP 204 status if there is a prior request that is still processing. From the server point of view, it is OK. But the web browser will handle just that "aborted" request, not the first one.

Browser sends request "A"
Server starts to process the request "A"
Browser sends request "B"
Server aborts request "B" (HTTP 204)
Browser receives response "B" (Aborted)
Server finishes the request "A"
Browser does not displays / receives the request "A"

So, finally the question. Is possible to change the response of a request? Thus, I could avoid the duplicate processing on server side, and the browser could display the requested content.

Can you suggest another approach to solve that issue?

Browser sends request "A"
Server start processing request "A"
Browser sends request "B"
Server "holds" request "B"
Server finishes the request "A"
Server forwards the request "B" to the the response "A"
Server aborts the request "A" (http 204)
Browser displays / receives the request "B" with the response "A" content / headers

Thanks in advance.

Instead of using this approach, you can use micro-caching , that is caching content only small durations, like 1s,10s. This way instead of http204, you return cached content. Your users would be more happy with this approach. You can synchronize your servlet code so that only one request will processed in your servlet. But be aware that synchronized servlet is not a good design .

I will do a cache based on this Stack Overflow answer of @BalusC. How to read and copy the HTTP servlet response output stream content for logging . I can't exchange the response of other request, but I can cache the previous response by a short time. The subsequent requests can receive the cached response, thus it will avoid the processor flood. Thank @eduyayo for the suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM