2

I want to redirect a page in server side (using spring), but the URL should remain the same.

For ex: if user tries http://www.example.com/page1, I want to render content of http://www.example.com/page2 in browser but the URL should still point to http://www.example.com/page1.

I tried 301, 302, 307 redirects, but all page URLs are changing to http://www.example.com/page2.

Is there anyway to achieve this?

2

1 Answer 1

2

It's a problem of terminology. What you're looking for is forward rather than redirect. If you're interested you may want to look that up e.g. here: http://www.javapractices.com/topic/TopicAction.do?Id=181.

There are at least two ways of doing this:

Traditional, RequestDispatcher can be used outside a Spring WebMVC application, too.

public class MyController extends AbstractController {

    @Override
    protected void handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        request.getRequestDispatcher("/new/path").forward(request, response);
    }
}

Spring WebMVC notation:

public class MyController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        return new ModelAndView("forward:/new/path");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why such old Spring version?
Thanks. It works. Is it possible to forward requests to some external site? For ex: from example.com/page1 to yahoo.com but the URL still example.com/page1?
@SaravananShanmugam - no

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.