Servlet redirect example - redirecting from a servlet to a JSP

Java servlet redirect FAQ: Can you provide an example of how to perform a Java servlet redirect?

Some time ago I wrote about forwarding from a servlet to a JSP. Depending on the circumstance I might rather redirect from a servlet to a JSP. When I need to to do a redirect instead of a forward I use this code:

private String LOGIN_PAGE = "login.jsp";
// other code here ...
response.sendRedirect(LOGIN_PAGE);

Of course in this example the response object is an instance of the standard HttpServletResponse class.

From a user's perspective, one of the nicest things about using a redirect instead of a forward is that the URL for a page can look a lot cleaner.

Actually, that's the reason I'm using it today. I'm working on a prototype for a client, and want them to be able to see the URL's and names of my JSP pages, so we can use those page names as part of our vocabulary. This is a non-production concern, but I always think about trying to optimize the requirements process from the domain expert's perspective, and this is a part of that effort.