Gili
Gili

Reputation: 90101

Java Servlets: How to repeat an HTTP request?

I'd like to repeat an HTTP request automatically if a database deadlock occurs; however, FilterChain.doFilter() is defined as a unidirectional chain (so I cannot reset its state).

In cases where it's safe to do so, is it possible to repeat an HTTP request without having the client re-submit the request?

UPDATE: I just discovered a problem with this approach. Even if you repeat the request, you will need to buffer the request InputStream. This means that if a user uploads 100MB of data, you'll be forced to buffer that data regardless of whether a deadlock occurs.

I am exploring the idea of getting the client to repeat the request here: Is it appropriate to return HTTP 503 in response to a database deadlock?

Upvotes: 1

Views: 2035

Answers (2)

Gili
Gili

Reputation: 90101

Answering my own question:

Don't attempt to repeat an HTTP request. In order to do so you are going to be forced to buffer the InputStream for all requests, even if a deadlock never occurs. This opens you up to denial-of-service attacks if you are forced to accept large uploads.

I recommend this approach instead: Is it appropriate to return HTTP 503 in response to a database deadlock?

You can then break down large uploads into multiple requests stitched together using AJAX. Not pretty but it works and on the whole your design should be easier to implement.

UPDATE: According to Brett Wooldridge:

You want a small pool of a few dozen connections at most, and you want the rest of the application threads blocked on the pool awaiting connections.

Just as Hikari recommends a small number of threads with a long queue of requests, I believe the same holds true for the web server. By limiting the number of active threads, we limit the number of InputStreams we need to buffer (the remaining requests get blocked before sending the HTTP body).

To further reinforce this point, Craig Ringer recommends recovering from failures on the server side where possible.

Upvotes: 1

Dev Blanked
Dev Blanked

Reputation: 8865

You can do a 'forward' of the original request like below.

RequestDispatcher rd= request.getRequestDispatcher("/originalUrl");
rd.forward(request, response);

Here request and response represent HttpServletRequest/HttpServletResponse respectively.Refer http://docs.oracle.com/javaee/5/api/index.html?javax/servlet/RequestDispatcher.html

Alternatively you can do a redirect on the response. This however will send a response to the browser asking it to issue a new request for the provided url. This is shown below

response.sendRedirect("originalUrl?neededParam=abc");

Upvotes: 0

Related Questions