How to do Redirecting?
- The HttpServletReponse interface has a method, sendRedirect(), that sends the response to another path.A Redirect example:
- This method can accept relative URLs.
- The servlet container must convert the relative URL to an absolute URL before sending the response to the client.
- If the location is relative without a leading '/' the container interprets it as relative to the current request URI.
- If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.A simple Servlet Redirect program:
- The Servlet above will redirect the request to a url, given as a request parameter, using the response sendRedirect() method.The html startup file, index.html, for the browser can be like this:The form will look like:
- When the user press the submit button he will be redirected to selected option url.
- All source code examples in this session is part of a complete example, ServletResponseRedirect, that you can download.