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 ...

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.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.