Servlet Redirect HTTP Request Example

This tutorial shows you Servlet Redirect HTTP Request Example that demonstrate an HTTP Request from one servlet to another servlet. The sendRedirect() method of the HttpServletResponse sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. This is ninth post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.

Table of contents:
1. Permanent and Temporary Redirects in Java
2. Project structure
3. Maven Dependency
4. Servlet Redirect HTTP Request
5. Servlet Redirected HTTP Request
6. Deploy Servlet Redirect HTTP Request Example

Permanent and Temporary Redirects in Java

To perform a permanent or temporary redirect in a Servlet, set the status property of the HttpServletResponse object to either SC_MOVED_PERMANENTLY (301) or SC_MOVED_TEMPORARILY (302) and set the Location header to the target URL.

Project structure

Servlet Redirect Http Request Example

Maven Dependency

Servlet Redirect HTTP Request
The sendRedirect() method of the HttpServletResponse sends a temporary redirect response to the client. This redirect sets the status property to 302. It means that this redirect is temporary.

Servlet Redirected HTTP Request

Deploy Servlet Redirect HTTP Request Example

Deploy file war on a servlet container or application server( Tomcat 8 for example) then run with the address.
URL: http://localhost:8080/servlet-redirect/redirect

You see that the request is redirected immediately to URL: http://localhost:8080/servlet-redirect/display and response to the screen

Servlet Redirect Http Request Example

That’s all. You can also find other servlet example in the series of Java Servlet Example.

Download complete source code, click link below

Servlet-Redirect-Http-Request-Example.zip (41 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons