Saturday, September 2, 2017

Show placeholder page when browser receives 302 request

Leave a Comment

When a browser is receiving redirect request from server, for a fraction of second, browser shows an error page that says "Page not found", and then redirects to appropriate url.

I am looking for solution where browser, instead of displaying "Page not found" page, shows a dummy page.

I assume this is what payment gateways are doing by displaying a page that says "Do not press back/refresh button".

4 Answers

Answers 1

I believe your initial premise that when a browser receives a 302 response code it shows an error, might be incorrect.

You can see redirects happening in many websites where all the browser does is just change the address in the address bar and load the eventual page, without displaying anything in the mean time.

Moreover, the 302 HTTP status code is not intended to have a body part, and if it has one it is usually ignored. And so, I can think of only two things that may be happening:

  1. You're using some browser that has a different behavior when accepting a 302 code (older?).
  2. The server isn't sending a 302, and in fact is sending back a page with redirection code in it.

A good way to check this depends on which browser you're using, but most modern browsers have a 'developer' pane where you can see the outgoing requests and incoming responses and their headers and status codes. You can then verify what is actually happening.

If the case is the former, then I'm afraid there's probably not much you can do about it without altering the browser itself. If it is the latter, then assuming the server code is under your control, you can change whatever content is returned.

Answers 2

I assume this is what payment gateways are doing by displaying a page that says "Do not press back/refresh button"

To achieve this you can use meta tag to refresh / redirect. The following snippet is taken from this SO post

<meta http-equiv="refresh" content="3;url=http://www.google.com/" /> 

Answers 3

You can add a Custom error page

To map 302 to a static HTML file, you need to create an html file called 302.html under resources/public/error, your folder structure should look like:

src/  +- main/      +- java/      |         +- resources/          +- public/              +- error/                  +- 302.html 

Answers 4

If you are using Spring (as the tag suggests) you can have a class to handle the redirection, which redirects you immediately to the page you desire.

There are multiple different ways to handle redirection, here you have a good guide: http://www.baeldung.com/spring-redirect-and-forward

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment